playframework 2.0 - Writing custom Enumeratee with Scala and Play2 -


i having hard time understanding iteratee/enumeratee/enumerator concept. looks understood how create custom iteratee - there examples that.

now i'm going write custom enumeratee. start digging code that, there not comments there lot of fold(), fold0(), foldm(), joini(). understood enumeratee made of iteratee sauce, still can't catch conception of writing own. so, if me example task give right direction. lets consider such example:

val stringenumerator = enumerator("abc", "def,ghi", "jkl,mnopqrstuvwxyz") val myenumeratee: enumeratee[string, int] = ... // ??? val lengthenumerator: enumerator[int] = stringenumerator through myenumeratee // should equal enumerator(6, 6, 14) 

myenumeratee should resample stream splitting given character flow comma , returning length of each chunk ("abc" + "def" length 6, "ghi" + "jkl" length 6 , on). how write it?

p.s. there iteratee i've wrote counting length of each chunk , return list[int]. maybe help.

the fancy thing trying here repartition characters not according preexisting iteratee input boundaries comma boundaries. after simple composing enumeratee.map{_.length}. here example using scala interpreter in paste mode. can see result1 @ bottom repartitioned strings , result2 count of each.

scala> :paste // entering paste mode (ctrl-d finish)  import play.api.libs.iteratee._ import scala.concurrent.executioncontext.implicits.global import scala.concurrent.await import scala.concurrent.duration._  def repartitionstrings: enumeratee[string, string] = {   enumeratee.grouped[string](traversable.splitonceat[string, char](c => c != ',') transform iteratee.consume()) }  val stringenumerator = enumerator("abc", "def,ghi", "jkl,mnopqrstuvwxyz")  val repartitionedenumerator: enumerator[string] = stringenumerator.through(repartitionstrings) val lengthenumerator: enumerator[int] = stringenumerator.through(repartitionstrings).through(enumeratee.map{_.length}) // should equal enumerator(6, 6, 14)  val result1 = await.result(repartitionedenumerator.run(iteratee.getchunks[string]), 200 milliseconds) val result2 = await.result(lengthenumerator.run(iteratee.getchunks[int]), 200 milliseconds)  // exiting paste mode, interpreting.  import play.api.libs.iteratee._ import scala.concurrent.executioncontext.implicits.global import scala.concurrent.await import scala.concurrent.duration._ repartitionstrings: play.api.libs.iteratee.enumeratee[string,string] stringenumerator: play.api.libs.iteratee.enumerator[string] = play.api.libs.iteratee.enumerator$$anon$19@77e8800b repartitionedenumerator: play.api.libs.iteratee.enumerator[string] = play.api.libs.iteratee.enumerator$$anon$3@73216e8d lengthenumerator: play.api.libs.iteratee.enumerator[int] = play.api.libs.iteratee.enumerator$$anon$3@2046e423 result1: list[string] = list(abcdef, ghijkl, mnopqrstuvwxyz) result2: list[int] = list(6, 6, 14) 

enumeratee.grouped powerful method group traversable things (seq, string, ...) according small internal custom iteratee define. iteratee should consume elements stream , produce element in first element in outer enumeratee , rerun on remaining input when time second outer element, , on. achieve using special helper method enumeratee.splitonceat precisely looking for, need compose simple iteratee concatenate of these chunks string returned @ end (iteratee.consume).


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -