Skip to content

Instantly share code, notes, and snippets.

@globulon
Created March 31, 2012 16:53
Show Gist options
  • Select an option

  • Save globulon/2266717 to your computer and use it in GitHub Desktop.

Select an option

Save globulon/2266717 to your computer and use it in GitHub Desktop.
Iteratee on Steroids
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