UPDATE: I’ve just released this as my first package on hackage. You can read more and write any comments here.
I just put together a library that provides a simple tree data structure to
represent the structure of files and directories in the OS. It provides some
simple IO functions like
readDirectory (analogous to
“opens” a directory, returning a DirTree of Strings in the IO monad.
The nice thing is that by defining a simple instance for Traversable (and the default instances for Foldable and Functor that we get for free) we get a whole array of nice functions which we can apply to directory structures! For example, we can combine all the text in a directory of files with:
1 2 3
(_:/t) portion ignores the base directory returned). The
also includes a constructor for handling failures. Here is the type
1 2 3 4 5 6 7
I have created a file with three examples:
in the first we simulate the command
darcs initializeto illustrate creating a directory of files by hand, and writing it to disk.
second, we show combining several different directories from around the filesystem and assembling the into a new tree structure.
lastly, we use our
readDirectoryWith(with Data.ByteString.readFile), and our Foldable instance to hash all the files in a directory structure (with Thomas DuBuisson’s pureMD5 package) and compare it to the hash of a different directory to see if the contents match exactly
There are probably bugs, and there are many useful functions I can think of to add if people think this is useful. I would be interested in hearing your thoughts on the interface, on any functionality you would like added, and anything else!
You can get the module and examples.hs with: