[Haskell-cafe] Patterns for processing large but finite streams

Malcolm Wallace malcolm.wallace at me.com
Fri Jul 1 10:12:37 CEST 2011


Sure you can.

runningAverage :: Int -> [Double] -> [Double]
runningAverage n xs = let chunk = take n xs
                      in (sum chunk / length chunk) : runningAverage (tail xs)

Lazy lists are absolutely ideal for this purpose.
Regards,
    Malcolm

On 1 Jul 2011, at 07:33, Eugene Kirpichov wrote:

> Plain old lazy lists do not allow me to combine multiple concurrent
> computations, e.g. I cannot define average from sum and length.
> 
> 2011/7/1 Heinrich Apfelmus <apfelmus at quantentunnel.de>:
>> Eugene Kirpichov wrote:
>>> 
>>> I'm rewriting timeplot to avoid holding the whole input in memory, and
>>> naturally a problem arises:
>>> 
>>> How to represent large but finite streams and functions that process
>>> them, returning other streams or some kinds of aggregate values?
>>> 
>>> Examples:
>>> * Adjacent differences of a stream of numbers
>>> * Given a stream of numbers with times, split it into buckets by time
>>> of given width and produce a stream of (bucket, 50%,75% and 90%
>>> quantiles in this bucket)
>>> * Sum a stream of numbers
>>> 
>>> Is this, perhaps, what comonads are for? Or iteratees?
>> 
>> Plain old lazy lists?
>> 
>> 
>> Best regards,
>> Heinrich Apfelmus
>> 
>> --
>> http://apfelmus.nfshost.com
>> 
>> 
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>> 
> 
> 
> 
> -- 
> Eugene Kirpichov
> Principal Engineer, Mirantis Inc. http://www.mirantis.com/
> Editor, http://fprog.ru/
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe




More information about the Haskell-Cafe mailing list