Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell lazy open and close files

I want to read a big graph structure from hard disk in Haskell. The graph structure looks like this:

Every node has got a definition - a file describing what are the children and how are they connected (these graphs are serialized using Data.Serialize.

Every node can have children. So if I've got file A.node I can have directory A containing children of A node (in the form of <X>.node files and directories).

I want to be able to traverse this graph using Haskell and load to memory and unserialize only needed files. So If I for example traverse the graph in the way of A->B->C (where B is child of A etc), then Haskell should read files A.node, A/B.node and A/B/C.node. The next time I traverse the graph, the files should NOT be read again, because we did it already.

How can I do this the best way?

like image 459
Wojciech Danilo Avatar asked Nov 03 '22 18:11

Wojciech Danilo


1 Answers

There are libraries that give you a pure value that represents a full directory tree, and will only read those directories and file that are actually used. By virtue of lazy evaluation, the second time you access such a file it will already by in memory.

Check out directory-tree, especially the readDirectoryWithL function.

like image 118
Joachim Breitner Avatar answered Nov 09 '22 09:11

Joachim Breitner