2

Trying to get my head around Enumerators on play 2.0.4 - I'd like to interleave one Enumerator with another, but only as long as length of the first enumerator (exclusive). So:

Enumerator("hello", "world") -> "hello" ", " "world"
Enumerator("one", "two", "three") -> "one" ", " "two" ", " "three"

The built-in interleave includes past the end of the 1st Enumerator, until the end of the 2nd Enumerator.

val commas : Enumerator[String] = {
  Enumerator(", ", ", ", ", ")
}
val words : Enumerator[String] = {
  Enumerator("hello", "world!")
}
Ok.stream(words interleave commas andThen Enumerator.eof)

produces "hello, world, , " not "hello, world"

Help much appreciated!

barnybug
  • 643
  • 1
  • 5
  • 8

1 Answers1

0

Enumerators are usually processed until consumed, so in your scenario the result is expected as once one Enumerator is finsihed the remainder of the other is processed.

What you seem to need is an Iterateethat processes the output and filters accordingly. You can find some examples in the documentation.

Another possible way is to use an Enumeratee (see documentation) to transform the output of the interleaving.

Pere Villega
  • 16,429
  • 5
  • 63
  • 100
  • Thanks, I've struggled trying with Iteratee/Enumeratee but this is essentially my question. Finding the docs are a bit difficult to wrap my head around, so any guidance appreciated. – barnybug Dec 04 '12 at 19:10