Fixed 19039 for both scaladsl and javadsl
19039: Added cycle method to Source class for both scaladsl and javadsl. Test added.
This commit is contained in:
parent
b3444fa1b0
commit
78e0a534f4
6 changed files with 61 additions and 0 deletions
|
|
@ -203,6 +203,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.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue