Skip to content

Instantly share code, notes, and snippets.

@mpilquist
Created August 22, 2012 19:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpilquist/3428731 to your computer and use it in GitHub Desktop.
Save mpilquist/3428731 to your computer and use it in GitHub Desktop.
Example of trampolining iteratees with scalaz 7
import scalaz._
import Scalaz._
import scalaz.iteratee._
import EnumeratorT._
def counter[A, F[+_]: Pointed] =
IterateeT.fold[A, F, Int](0) { (acc: Int, a: A) => acc + 1 }
def count[A, F[+_]: Monad](e: EnumeratorT[A, F]) =
(counter[A, F] &= e).run
require (count(enumList[Int, Id](List(1, 2, 3))) === 3)
val largeStream = Stream.range(1, 100001, 1)
try {
(counter[Int, Id] &= enumStream[Int, Id](largeStream))
require (false, "Should have thrown stack overflow")
} catch {
case _: StackOverflowError =>
}
import Free._
require (count(enumList[Int, Trampoline](List(1, 2, 3))).run === 3)
require ((counter[Int, Trampoline] &= enumStream[Int, Trampoline](largeStream)).run.run === largeStream.size)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment