Source.unfold examples (#28081)
This commit is contained in:
parent
fc48184a1e
commit
653d05e7d6
5 changed files with 123 additions and 10 deletions
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package docs.stream.operators.source
|
||||
|
||||
import akka.NotUsed
|
||||
import akka.stream.scaladsl.Source
|
||||
|
||||
object Unfold {
|
||||
|
||||
// #countdown
|
||||
def countDown(from: Int): Source[Int, NotUsed] =
|
||||
Source.unfold(from) { current =>
|
||||
if (current == 0) None
|
||||
else Some((current - 1, current))
|
||||
}
|
||||
// #countdown
|
||||
|
||||
// #fibonacci
|
||||
def fibonacci: Source[BigInt, NotUsed] =
|
||||
Source.unfold((BigInt(0), BigInt(1))) {
|
||||
case (a, b) =>
|
||||
Some(((b, a + b), a))
|
||||
}
|
||||
// #fibonacci
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue