Why I love Hoogle

25/04/2009

I’m working on a module called DirectoryTree, a simple tree structure mirroring the structure of a directory tree in the outside world, with functions to hopefully make it easy to do useful things (I’m not sure if something like this already exists). Here is the data-type:


data DirTree a = Dir { name  :: String,
                       files :: [a],
                       directories :: [DirTree a]  }
               | Fail String    --file/directory name

I would like to do things like map over a DirTree of FilePaths, returning a tree (in the IO monad) of Handles… so the code involves a lot of slogging through the IO monad, which I’m not totally comfortable with yet.

I’m feeling out how to proceed with the above and begin to define this function:


-- an abstraction for this? monad?:
ioMap :: (a -> IO b) -> DirTree a -> IO (DirTree b)

…there’s something happening here but I don’t know what it is; let’s see what Hoogle can tell me about this abstraction. I take the type definition and plug it into firefox’s hoogle search plugin and voila!; I’m presented with the following matches:

Data.Traversable:
traverse :: (Traversable t, Applicative f) => (a -> f b) -> t a -> f (t b)
for :: (Traversable t, Applicative f) => t a -> (a -> f b) -> f (t b)
mapM :: (Traversable t, Monad m) => (a -> m b) -> t a -> m (t b)
forM :: (Traversable t, Monad m) => t a -> (a -> m b) -> m (t b)

I see that mapM from Data.Traversable looks like what I want, and traverse also looks useful.

I learned something, and have an idea of how to proceed, and that’s why I love hoogle :)

No Comments