Skip to content

Instantly share code, notes, and snippets.

@negator
Last active December 10, 2015 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save negator/4367008 to your computer and use it in GitHub Desktop.
Save negator/4367008 to your computer and use it in GitHub Desktop.
flatMap for Enumerateess
object KloutEnumeratee {
def flatMapInput[From] = new {
def apply[To](f: Input[From] => PlayPromise[Input[To]]) =
new Enumeratee.CheckDone[From, To] { //Checkdone is part of the Play Iteratee library
def step[A](k: K[To, A]): K[From, Iteratee[To, A]] = {
case in @ (Input.El(_) | Input.Empty) =>
val promiseOfInput = f(in)
Iteratee.flatten(promiseOfInput map { input =>
new CheckDone[From, To] {
def continue[A](k: K[To, A]) = Cont(step(k))
} &> k(input)
})
case Input.EOF => Done(k(Input.EOF), Input.EOF)
}
def continue[A](k: K[To, A]) = Cont(step(k))
}
}
def flatMap[E] = new {
def apply[NE](f: E => Promise[NE]): Enumeratee[E, NE] = flatMapInput[E]{
case Input.El(e) => f(e) map (Input.El(_))
case Input.Empty => Promise pure Input.Empty
case Input.EOF => Promise pure Input.EOF
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment