Last active
December 10, 2015 02:18
-
-
Save negator/4367008 to your computer and use it in GitHub Desktop.
flatMap for Enumerateess
This file contains 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
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