Using Iteratees to write to a file

Hello everyone, I've been reading "Iteratee: Teaching an Old Fold New Tricks" from the Monad Reader Issue 16 and am having a problem figuring out how to use the streamToFile function. Specifically, I can't figure out how to feed the Iteratee data to write to the file. If I run ghci> runIter $ streamToFile "/tmp/stream" Then the file is created but not populated with any data - which is natural since I didn't give it any data. I have no clue how to give it data though. I'd really appreciate it if someone could show me how to write "Hello world!" or something else equally mundane to get me going. I've been banging my head on this for hours now. Thanks, Rich

On Sat, Mar 19, 2011 at 01:01:46AM -0700, Richard Wallace wrote:
Hello everyone,
I've been reading "Iteratee: Teaching an Old Fold New Tricks" from the Monad Reader Issue 16 and am having a problem figuring out how to use the streamToFile function. Specifically, I can't figure out how to feed the Iteratee data to write to the file. If I run
ghci> runIter $ streamToFile "/tmp/stream"
Then the file is created but not populated with any data - which is natural since I didn't give it any data. I have no clue how to give it data though. I'd really appreciate it if someone could show me how to write "Hello world!" or something else equally mundane to get me going. I've been banging my head on this for hours now.
I think you need to get your data from somewhere, an Enumerator, e.g. an Enumerator for a file handle. I haven't looked at the Iteratee API for a while, but I believe that an Enumerator basically is of the type Iteratee -> Iteratee, which means that something along the lines of runIter $ enumerateHandle h (streamToFile "/tmp/stream") ought to copy the content of the handle into the file. The documentation for Enumerator[1] is an excellent resource for understanding iteratees. Even if you don't plan on using the Enumerator package itself. /M [1] http://hackage.haskell.org/package/enumerator -- Magnus Therning OpenPGP: 0xAB4DFBA4 email: magnus@therning.org jabber: magnus@therning.org twitter: magthe http://therning.org/magnus Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves. -- Alan Kay
participants (2)
-
Magnus Therning
-
Richard Wallace