Created
March 31, 2012 16:53
-
-
Save globulon/2266717 to your computer and use it in GitHub Desktop.
Iteratee on Steroids
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
| implicit def iterateesToApplicative[E]() = new Applicative[({type λ[α] = IterV[E, α]})#λ] { | |
| def apply[T](data: T) = { | |
| Done(data, EMPTY) | |
| } | |
| def flatten[T](source: IterV[E, IterV[E, T]]): IterV[E, T] = source match { | |
| case Done(Done(v, _), s) => Done(v, s) | |
| case Done(Cont(f), s) => f(s) | |
| case Cont(f) => Cont[E, T]{ | |
| s: StreamG[E] => flatten(f(s)) | |
| } | |
| } | |
| def map[T, P >: T, U](source: IterV[E, T])(f: (P) => U): IterV[E, U] = source match { | |
| case Done(t, s) => Done(f(t), s) | |
| case Cont(g) => Cont[E, U] { | |
| stream: StreamG[E] => map(g(stream))(f) | |
| } | |
| } | |
| } | |
| implicit def iterateeOnSteroid[E,A](iter: IterV[E, A]) = new { | |
| implicit val applicative = iterateesToApplicative[E]() | |
| def map[B >: A, C](f: B => C) = applicative.map(iter)(f) | |
| def flatMap[B >: A, C](f: B => IterV[E, C]) = applicative.flatMap(iter)(f) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment