Last active
January 2, 2016 03:39
-
-
Save langley/8245229 to your computer and use it in GitHub Desktop.
Iteratee / Enumerator / Enumeratee code snippets
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
// Code snippets to illustrate working with | |
// Iteratee / Enumerator / Enumeratee api from Play Framework | |
// http://www.playframework.com/documentation/2.2.x/Iteratees | |
// You should create a play app and then launch the scala console from it. | |
// Then you can paste these snippets in to experiment with iteratees | |
// in the scala REPL. | |
import play.api.libs.iteratee._ | |
import play.api.libs.concurrent.Execution.Implicits._ | |
// And we'll need some extra imports to use Await.. which is really useful because | |
// Iteratees, Enumerators and Eneratees are all about asynchronous processing and futures | |
import scala.concurrent.Await | |
import scala.concurrent.duration._ | |
import scala.language.postfixOps | |
// Note: all of these examples except the last one don't really need the Await, but it's cleaner to use it | |
// otherwise you end up with odd interleaving of the printlns and the console's output. | |
// Create an Iteratee that will consume Strings and print them | |
val printlnIterateeForStrs = Iteratee.foreach[String](str => println(s"""printlnIterateeForStrs saw: $str""")) | |
// Create an Enumerator that will produce Strings | |
val strEnumerator: Enumerator[String] = Enumerator("1", "2", "3") | |
// Apply the Enumerator to the Iteratee | |
Await.result(strEnumerator(printlnIterateeForStrs), 2 seconds) | |
// Create an iteratee that requires Ints | |
val printlnIterateeForInts = Iteratee.foreach[Int](i => println(s"""printlnIterateeForInts saw: $i""")) | |
// Create an enumeratee that maps from String to Int | |
// Enumeratees are "transformers" that map from one type of Enumerator to another | |
val strToIntEnumeratee: Enumeratee[String,Int] = Enumeratee.map(s => s.toInt) | |
// Now compose the strEnumerator with the strToIntEnumeratee transformer and apply | |
// it to the printlnIterateeForInts | |
Await.result(strEnumerator(strToIntEnumeratee(printlnIterateeForInts)), 2 seconds) | |
// A different syntax for composing Enumerators Enumeratees and Iteratees | |
Await.result({strEnumerator through strToIntEnumeratee run printlnIterateeForInts}, 2 seconds) | |
// if you like traditional function call syntax this is | |
Await.result({strEnumerator.through(strToIntEnumeratee).run(printlnIterateeForInts)}, 2 seconds) | |
// or using apply | |
Await.result({strEnumerator through strToIntEnumeratee apply printlnIterateeForInts}, 2 seconds) | |
// All of the above use the strToIntEnumeratee to transform the strEnumerator to the equivalent | |
// of an intEnumerator which can be consumed by an iteratee that consumes Ints | |
// Finally, let's make an Iteratee that has a final result, a List in this case. | |
val anotherStrEnumerator = Enumerator("a one"," and a ", "two", " and a ", "three") | |
val iterateeToCreateAListOfStrings: Iteratee[String, List[String]] = Iteratee.fold[String, List[String]](List()) { (list, item) => item :: list } | |
Await.result({anotherStrEnumerator run iterateeToCreateAListOfStrings}, 2 seconds) | |
// --------------------------------------------------------- | |
// Resources: | |
// http://mandubian.com/2012/08/27/understanding-play2-iteratees-for-normal-humans/ | |
// http://www.playframework.com/documentation/2.2.x/Iteratees | |
// http://www.playframework.com/documentation/2.2.x/api/scala/index.html#play.api.libs.iteratee.package |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment