On Tue, Jan 1, 2013 at 2:51 AM, HamsterofDeath <
h-s...@gmx.de> wrote:
> i cannot use takeWhile in advance, because i can only check the
> stop-condition by looking at the already accumulated data.
> in haskell, i would use scan & filter which would work because scan is
> lazy - but in scala it would go over the complete collection first.
The Haskell solution is translatable into Scala using Stream or a view.
scala> Stream.from(1).scanLeft(0)(_ + _).takeWhile(_ < 100).last
res0: Int = 91
scala> List.range(1, 100).view.scanLeft(0)(_ + _).takeWhile(_ < 100).last
res1: Int = 91
Note that the view version fails in 2.9, but it does work in 2.10.
You might also think of using Iterator, since Iterator has scanLeft.
But it doesn't have `last`, so then you're stuck trying to get the
last item, and the only easy fix I can think of is `.toStream.last`.
--
Seth Tisue | Northwestern University |
http://tisue.net
developer, NetLogo:
http://ccl.northwestern.edu/netlogo/