[Haskell-cafe] Finding the average in constant space
sschuldenzucker at uni-bonn.de
Sun May 27 21:33:46 CEST 2012
On 05/27/2012 10:04 AM, Chris Wong wrote:
> I just came up with a way of executing multiple folds in a single
> pass. In short, we can write code like this:
> average = foldLeft $ (/)<$> sumF<*> lengthF
> and it will only traverse the input list once.
> The code is at: https://gist.github.com/2802644
> My question is: has anyone done this already? If not, I might release
> this on Hackage -- it seems quite useful.
This is (a special case of) the main point in the design of iteratees.
See e.g. the definition of the 'Iteratee' type in the enumeratee
library. - Looks pretty much like your 'Fold' type with an additional
state (done or not yet done).
Also, the pipe package seems to provide something similar.
More information about the Haskell-Cafe