Skip to content

Commit

Permalink
Rename fromIterator to enumerate
Browse files Browse the repository at this point in the history
As @sadache suggested renamed o enumerate. Adding a parameter to the
apply is not without breaking the current API though.
  • Loading branch information
bobylito committed Jan 8, 2013
1 parent ae1cf8c commit a066b3a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -540,24 +540,6 @@ object Enumerator {
fromStream(new java.io.FileInputStream(file), chunkSize)
}

/**
* Creates an enumerator from a TraversableOnce object.
*/
def fromTraversableOnce[A](traversable: scala.collection.TraversableOnce[A])(implicit ctx:scala.concurrent.ExecutionContext): Enumerator[A] = {
val it = traversable.toIterator
Enumerator.unfoldM[scala.collection.Iterator[A], A](it: scala.collection.Iterator[A] )({ currentIt =>
if(currentIt.hasNext)
Future[ Option[(scala.collection.Iterator[A], A)] ]({
val next = currentIt.next
Some( (currentIt -> next) )
})(ctx)
else
Future.successful[ Option[(scala.collection.Iterator[A], A)] ]({
None
})
})
}

/** Create an Enumerator of bytes with an OutputStream.
*/
def outputStream(a: java.io.OutputStream => Unit): Enumerator[Array[Byte]] = {
Expand Down Expand Up @@ -597,7 +579,28 @@ object Enumerator {

}

def enumerate[E](s:Seq[E]): Enumerator[E] = apply(s:_*)
/**
* Create an Enumerator from any TraversableOnce like collection of elements.
*
* Example of an iterator of lines of a file :
* {{{
* val enumerator: Enumerator[String] = Enumerator( scala.io.Source.fromFile("myfile.txt").getLines )
* }}}
*/
def enumerate[E](traversable : TraversableOnce[E])(implicit ctx:scala.concurrent.ExecutionContext): Enumerator[E] = {
val it = traversable.toIterator
Enumerator.unfoldM[scala.collection.Iterator[E], E](it: scala.collection.Iterator[E] )({ currentIt =>
if(currentIt.hasNext)
Future[ Option[(scala.collection.Iterator[E], E)] ]({
val next = currentIt.next
Some( (currentIt -> next) )
})(ctx)
else
Future.successful[ Option[(scala.collection.Iterator[E], E)] ]({
None
})
})
}

private def enumerateSeq[E, A]: (Seq[E], Iteratee[E, A]) => Future[Iteratee[E, A]] = { (l, i) =>
l.foldLeft(Future.successful(i))((i, e) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,36 @@ object EnumeratorsSpec extends Specification {

}

"Enumerator.fromTraversableOnce" should {
"Enumerator.enumerate " should {
"generate an Enumerator from a singleton Iterator" in {
val iterator = scala.collection.Iterator.single[Int](3)
val futureOfResult = Enumerator.fromTraversableOnce(iterator) |>>>
val futureOfResult = Enumerator.enumerate(iterator) |>>>
Enumeratee.take(1) &>>
Iteratee.fold(List.empty[Int])((r, e) => e::r)
val result = Await.result(futureOfResult, Duration.Inf)
result(0) must equalTo(3)
result.length must equalTo(1)
}

"take as much element as in the iterator " in {
"take as much element as in the iterator in the right order" in {
val iterator = scala.collection.Iterator.range(0, 50)
val futureOfResult = Enumerator.fromTraversableOnce(iterator) |>>>
val futureOfResult = Enumerator.enumerate(iterator) |>>>
Enumeratee.take(100) &>>
Iteratee.fold(List.empty[Int])((r, e) => e::r)
Iteratee.fold(Seq.empty[Int])((r, e) => r :+ e)
val result = Await.result(futureOfResult, Duration.Inf)
result.length must equalTo(50)
result(0) must equalTo(0)
result(49) must equalTo(49)
}
"work with Seq too" in {
val seq = List(1, 2, 3, 7, 42, 666)
val futureOfResult = Enumerator.enumerate(seq) |>>>
Enumeratee.take(100) &>>
Iteratee.fold(Seq.empty[Int])((r, e) => r :+ e)
val result = Await.result(futureOfResult, Duration.Inf)
result.length must equalTo(6)
result(0) must equalTo(1)
result(4) must equalTo(42)
}
}

Expand Down

0 comments on commit a066b3a

Please sign in to comment.