Skip to content

How do you traverse a directory (for example)? #2

Open
@mbwgh

Description

@mbwgh

Given that this library is all about using the "with idiom", the lack of an actual bracket function that works with Stream makes you wonder how you should use all this. From what I could gather by looking at the issue trackers, there were MonadResource and MonadMask instances in the past, but they have been removed. Since the documentation is not up-to-date everywhere, I have no idea how to proceed.

For instance, this is how I got a simple directory traversal to work. Trigger warning.

module Cancer where

import           Control.Exception
import           Data.IORef
import qualified Data.Streaming.Filesystem as FS -- streaming-commons
import           Streaming
import qualified Streaming.Prelude         as S

streamDir :: FilePath -> Stream (Of FilePath) IO ()
streamDir dir = do
  sRef <- lift $ newIORef (mempty :: Stream (Of FilePath) IO ())
  let go ds = do
        mfp <- FS.readDirStream ds
        case mfp of
          Nothing -> pure ()
          Just fp -> do
            modifyIORef' sRef (S.cons fp)
            go ds
  lift $ bracket (FS.openDirStream dir) FS.closeDirStream go
  str <- lift $ readIORef sRef
  str

This is obviously not how to do it. But since there is no MonadMask instance, the types of both the vanilla bracket function, and the one from exceptions, are kind of "in the way".
Should I reconsider the type of this kind of function? Does this functionality already exist? Did I miss some important piece of documentation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions