Created
August 22, 2012 19:46
-
-
Save mpilquist/3428731 to your computer and use it in GitHub Desktop.
Example of trampolining iteratees with scalaz 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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