public
Last active

Example of trampolining iteratees with scalaz 7

  • Download Gist
gistfile1.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.