Skip to content

Instantly share code, notes, and snippets.

@sadache
Created October 7, 2010 15:07
Show Gist options
  • Save sadache/615245 to your computer and use it in GitHub Desktop.
Save sadache/615245 to your computer and use it in GitHub Desktop.
def fold[S,A](seed: =>S)(f:(Function0[S],A)=>S,stream:Stream[A]):S={
stream match{
case Stream.#::(x,xs) => f(()=>(fold[S,A](seed)(f,xs)),x)
case Stream.Empty =>seed}
}
fold[Stream[Int],Int](Stream.Empty)((s,a)=>Stream.cons(a,s()),Stream.fill(10000)({println("oooo ");10000}))
outputs
oooo
res3: Stream[Int] = Stream(10000, ?)
@sadache
Copy link
Author

sadache commented Oct 8, 2010

Of course the implementation is terribly slow and leaks space!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment