Re: [Haskell-cafe] Iteratee IO examples
From: Eric Rasmussen ericrasmus...@gmail.com Hi, Examples are very helpful to me too -- thank you for sharing. I'm especially curious to see if there are any examples that allow you to use or convert non-iteratee-based functions. I have only just begun reading about iteratees and might be missing the point, but it seems like many of the examples so far rely on explicit recursion or special functions from one of the iteratee modules. You might be interested in the attoparsec-enumerator and attoparsec-iteratee packages, which adapt attoparsec parsers to work with iteratees. They're small, self-contained, and quite readable. Since attoparsec works with partial parses, it's a natural fit for iteratees. Honestly I'm quite dis-satisfied with the current state of code which depends on iteratee/enumerator. It's nearly all written in a very low-level style, i.e. directly writing 'liftI step', or 'case x of Yield - ...'. This is exactly what I would hope users could avoid, by using the functions in e.g. Data.Iteratee.ListLike. I've recently added more functions to iteratee which greatly reduce the need for this type of code. I don't know about enumerator, but I expect it isn't rich enough since most user code I've seen is pretty low-level. For some other iteratee examples, you can 'darcs get http://www.tiresiaspress.us/haskell/sndfile-enumerators/' and look at the examples directory (or browse online, of course). Is there a way to take a simple function (example below) and use an enumerator to feed it a ByteString from a file, or do you have to write functions explicitly to work with a given iteratee implementation? import qualified Data.ByteString.Char8 as B sortLines = B.unlines . sort . B.lines For this case, there's no point to using iteratees at all. Just read the file in directly to a strict bytestring. Since you're sorting, you'll need to see all the lines before results can be returned. If the file is too big to fit into memory, you'd need a more sophisticated algorithm for which you could use iteratees. In the general case, you need to write for a given iteratee implementation, but in many specific cases it's not necessary. If you want to transform each line of a file, for instance (with iteratee): import Data.ByteString.Char8 as B import Data.Iteratee as I import Data.Iteratee.Char import System.IO import Control.Monad.IO.Class transform :: (ByteString - ByteString) - FilePath - Iteratee [ByteString] IO () transform tFunc oFile = do h - liftIO $ openFile oFile WriteMode joinI $ rigidMapStream tFunc $ I.mapM_ (B.hPutStrLn h) liftIO $ hClose h rewriteFile :: (ByteString - ByteString) - FilePath - FilePath - IO () rewriteFile tFunc iFile oFile = fileDriver (joinI $ enumLinesBS (transform tFunc oFile)) iFile An unfolding version would be possible too, which would take a parameter tFunc :: (s - ByteString - (s, ByteString)) Maybe I'll add these as utilities in the next version of iteratee. John ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Iteratee IO examples
On 6/25/11 6:51 AM, John Lato wrote: Honestly I'm quite dis-satisfied with the current state of code which depends on iteratee/enumerator. It's nearly all written in a very low-level style, i.e. directly writing 'liftI step', or 'case x of Yield - ...'. This is exactly what I would hope users could avoid, by using the functions in e.g. Data.Iteratee.ListLike. I've recently added more functions to iteratee which greatly reduce the need for this type of code. I don't know about enumerator, but I expect it isn't rich enough since most user code I've seen is pretty low-level. I have a rather large suite of list-like functions for the old version of iteratee (used by a project I've been working on for a while). Once I get the time to convert the project to the newer iteratee, I'll send a patch with any you're still missing. (Though, admittedly, I'm not terribly keen on ListLike. The classes still seem too monolithic and ad-hoc. Though I'm not sure there's a way around that without something closer to ML's functor modules.) -- Live well, ~wren ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Iteratee IO examples
On Sat, Jun 25, 2011 at 10:18 PM, wren ng thornton w...@freegeek.orgwrote: On 6/25/11 6:51 AM, John Lato wrote: Honestly I'm quite dis-satisfied with the current state of code which depends on iteratee/enumerator. It's nearly all written in a very low-level style, i.e. directly writing 'liftI step', or 'case x of Yield - ...'. This is exactly what I would hope users could avoid, by using the functions in e.g. Data.Iteratee.ListLike. I've recently added more functions to iteratee which greatly reduce the need for this type of code. I don't know about enumerator, but I expect it isn't rich enough since most user code I've seen is pretty low-level. I have a rather large suite of list-like functions for the old version of iteratee (used by a project I've been working on for a while). Once I get the time to convert the project to the newer iteratee, I'll send a patch with any you're still missing. I'd greatly appreciate it. Even if they're for the old version; doing the conversion is fairly mechanical. (Though, admittedly, I'm not terribly keen on ListLike. The classes still seem too monolithic and ad-hoc. Though I'm not sure there's a way around that without something closer to ML's functor modules.) Refactoring ListLike has been a long-standing objective of mine, but I haven't put much time into it because it would cause breaking changes which I didn't think anyone else would appreciate. No way to tell except to just release it I guess. John ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Iteratee IO examples
Hi, I've been trying to learn Iteratee IO. I've made some progress by studying John Millikin's examples in the source of the enumerator package. I was surprised how confusing I found the tutorials that are available. I think that it is primarily because of the lack of concrete examples. It would be great if we could accumulate a collection of small concrete programs like John's wc.hs which show various uses of Data.Enumerator. Here's a little program I wrote to find the longest run of same characters in a file. http://hpaste.org/48255 Does anyone else have little examples like this that use Iteratee IO? Cheers, David David Place Owner, Panpipes Ho! LLC http://panpipesho.com d...@vidplace.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Iteratee IO examples
On Fri, 24 Jun 2011 15:11:59 +0200, David Place d...@vidplace.com wrote: Hi, I've been trying to learn Iteratee IO. I've made some progress by studying John Millikin's examples in the source of the enumerator package. I was surprised how confusing I found the tutorials that are available. I think that it is primarily because of the lack of concrete examples. It would be great if we could accumulate a collection of small concrete programs like John's wc.hs which show various uses of Data.Enumerator. Here's a little program I wrote to find the longest run of same characters in a file. http://hpaste.org/48255 Does anyone else have little examples like this that use Iteratee IO? Try finding packages that depend on the enumerator/iteratee packages at http://bifunctor.homelinux.net/~roel/hackage/packages/archive/pkg-list.html (which is down at the moment). Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html -- ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Iteratee IO examples
Hi, Examples are very helpful to me too -- thank you for sharing. I'm especially curious to see if there are any examples that allow you to use or convert non-iteratee-based functions. I have only just begun reading about iteratees and might be missing the point, but it seems like many of the examples so far rely on explicit recursion or special functions from one of the iteratee modules. Is there a way to take a simple function (example below) and use an enumerator to feed it a ByteString from a file, or do you have to write functions explicitly to work with a given iteratee implementation? import qualified Data.ByteString.Char8 as B sortLines = B.unlines . sort . B.lines Thanks! Eric On Fri, Jun 24, 2011 at 7:24 AM, Henk-Jan van Tuyl hjgt...@chello.nlwrote: On Fri, 24 Jun 2011 15:11:59 +0200, David Place d...@vidplace.com wrote: Hi, I've been trying to learn Iteratee IO. I've made some progress by studying John Millikin's examples in the source of the enumerator package. I was surprised how confusing I found the tutorials that are available. I think that it is primarily because of the lack of concrete examples. It would be great if we could accumulate a collection of small concrete programs like John's wc.hs which show various uses of Data.Enumerator. Here's a little program I wrote to find the longest run of same characters in a file. http://hpaste.org/48255 Does anyone else have little examples like this that use Iteratee IO? Try finding packages that depend on the enumerator/iteratee packages at http://bifunctor.homelinux.**net/~roel/hackage/packages/** archive/pkg-list.htmlhttp://bifunctor.homelinux.net/%7Eroel/hackage/packages/archive/pkg-list.html (which is down at the moment). Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/**hjgtuyl/tourdemonad.htmlhttp://members.chello.nl/hjgtuyl/tourdemonad.html -- __**_ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/**mailman/listinfo/haskell-cafehttp://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe