-
-
Save sadache/2713406 to your computer and use it in GitHub Desktop.
Funky enumerator usage
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
package controllers | |
import play.api._ | |
import play.api.mvc._ | |
import play.api.libs.ws._ | |
import play.api.libs.iteratee._ | |
import play.api.libs.concurrent._ | |
object Application extends Controller { | |
val EXPECTED_LENGTH = 15; | |
def index = Action { | |
val p1 = WS.url("http://linkedin.com").get().map{ _ => "linkedin" } | |
val p2 = WS.url("http://zenexity.com").get().map{ _ => "zenexity" } | |
val p3 = WS.url("http://news.ycombinator.com").get().map{ _ => "ycombinator" } | |
val enumerators = List(p1,p2,p3).map(p => Enumerator.flatten(p.map( r => Enumerator(r:_*)))) | |
val r = | |
Enumerator.interleave(enumerators) &> | |
Enumeratee.take(EXPECTED_LENGTH) |>> | |
Iteratee.fold(List[Char]()){ (es, e: Char) => println(e); e :: es } | |
val p = r.flatMap(_.run).map( cs => Ok(cs.reverse.mkString)) | |
AsyncResult(p) | |
} | |
} |
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
val r = Enumerator.interleave(enumerators) through | |
Enumeratee.take(EXPECTED_LENGTH) apply | |
Iteratee.fold(List[Char]()){ (es, e: Char) => println(e); e :: es } |
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
val r = Enumerator.interleave(enumerators) | |
.through(Enumeratee.take(EXPECTED_LENGTH)) | |
.apply(Iteratee.fold(List[Char]()){ (es, e: Char) => println(e); e :: es }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment