Merge pull request #20125 from kelebra/19039-kelebra-cycle

#19039: Added cycle method to Source class.
This commit is contained in:
Patrik Nordwall 2016-04-26 16:47:02 +02:00
commit 0b0a456e28
6 changed files with 61 additions and 0 deletions

View file

@ -208,6 +208,17 @@ object Source {
override def toString: String = "() => Iterator"
})
/**
* Create [[Source]] that will continually produce given elements in specified order.
*
* Start a new 'cycled' `Source` from the given elements. The producer stream of elements
* will continue infinitely by repeating the sequence of elements provided by function parameter.
*/
def cycle[T](f: () Iterator[T]): Source[T, NotUsed] = {
val iterator = Iterator.continually { val i = f(); if (i.isEmpty) throw new IllegalArgumentException("empty iterator") else i }.flatten
fromIterator(() iterator).withAttributes(DefaultAttributes.cycledSource)
}
/**
* A graph with the shape of a source logically is a source, this method makes
* it so also in type.