add examples for scan operator #25468

This commit is contained in:
airt 2019-01-04 22:08:54 +08:00 committed by Johan Andrén
parent a8a443abec
commit e124d11995
3 changed files with 39 additions and 0 deletions

View file

@ -31,3 +31,10 @@ Note that the `zero` value must be immutable.
@@@
## Examples
Scala
: @@snip [SourceOrFlow.scala](/akka-docs/src/test/scala/docs/stream/operators/SourceOrFlow.scala) { #scan }
Java
: @@snip [SourceOrFlow.java](/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java) { #scan }

View file

@ -172,6 +172,19 @@ class SourceOrFlow {
//#conflate
}
void scanExample() {
//#scan
Source<Integer, NotUsed> source = Source.range(1, 5);
source.scan(0, (acc, x) -> acc + x).runForeach(System.out::println, materializer);
// 0 (= 0)
// 1 (= 0 + 1)
// 3 (= 0 + 1 + 2)
// 6 (= 0 + 1 + 2 + 3)
// 10 (= 0 + 1 + 2 + 3 + 4)
// 15 (= 0 + 1 + 2 + 3 + 4 + 5)
//#scan
}
static //#conflateWithSeed-type
class Summed {

View file

@ -52,4 +52,23 @@ object SourceOrFlow {
//#conflateWithSeed
}
def scanExample(): Unit = {
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
implicit val system: ActorSystem = ActorSystem()
implicit val materializer: ActorMaterializer = ActorMaterializer()
//#scan
val source = Source(1 to 5)
source.scan(0)((acc, x) acc + x).runForeach(println)
// 0 (= 0)
// 1 (= 0 + 1)
// 3 (= 0 + 1 + 2)
// 6 (= 0 + 1 + 2 + 3)
// 10 (= 0 + 1 + 2 + 3 + 4)
// 15 (= 0 + 1 + 2 + 3 + 4 + 5)
//#scan
}
}