[Haskell-beginners] Using Iteratees to write to a file

Magnus Therning magnus at therning.org
Sat Mar 19 17:29:54 CET 2011


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 at therning.org   jabber: magnus at 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110319/fa142eda/attachment.pgp>


More information about the Beginners mailing list