I have a Haskell function that operates on a single file to produce a map. I want to iterate over all files in a directory and apply this function to produce a single map.
I am trying to approach it this way:
perFileFunc :: Int -> FilePath -> IO (Map.Map [Char] Double)
allFilesIn dir = filter (/= "..")<$>(filter(/= ".")<$>(getDirectoryContents dir)
This gives me a list of all filenames in a directory except . and ..
Now when I try and do
myFunc dir n = map (perFileFunc n) <$> allFilesIn dir
It typechecks but doesn’t do anything. I was expecting a list of maps which I would join using a unionWith (+) perhaps.
This doesn’t seem to be the right way to do this.
The tricky thing to understand about Haskell is how to recognize and compose IO actions. Let’s look at some type signatures.
Now then, you said that for
myFunc:So for that function, you want the type signature
Of course, the return type can’t be just
[Map.Map String Double], because we need to perform some IO in order to evaluatemyFunc. So given anIntand aFilePath, we actually want the return type to be an IO action that produces a[Map.Map String Double]:Now then, let’s look at the IO actions we will be composing to create this function.
perFileFuncisn’t actually an IO action, but it is a function that, given aFilePath, produces an IO action. So let’s see…if we run theallFilesInaction, then we can work with that list and runperFileFunc non each of its elements.So what goes in the
???spot? We have a[FilePath]at our disposal, since we used<-to run the actionallFilesIn dir. And we have a function,perFileFunc n :: FilePath -> IO (Map.Map String Double). And we want the result to have the typeIO [Map.Map String Double].Stop…Hoogle time! Generalizing the components we have (
a = FilePath,b = Map.Map String Double), we hoogle for[a] -> (a -> IO b) -> IO [b](pretending we haven’t seen ehird’s answer yet). Lo and behold,mapMis the magical solution we were looking for! (orforM, which is justflip mapM)If you desugar this do notation, you’ll find it reduces to ehird’s answer: