diff --git a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/grouped.md b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/grouped.md index 5663f08af3..1003913a42 100644 --- a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/grouped.md +++ b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/grouped.md @@ -17,6 +17,17 @@ Accumulate incoming events until the specified number of elements have been accu Accumulate incoming events until the specified number of elements have been accumulated and then pass the collection of elements downstream. +## Examples + +The below example demonstrates how `grouped` groups the accumulated elements into @scala[`Seq`] @java[`List`] +and maps with other operation. + +Scala +: @@snip [Grouped.scala](/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Grouped.scala) { #grouped } + +Java +: @@snip [SourceOrFlow.java](/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java) { #grouped } + ## Reactive Streams semantics @@@div { .callout } @@ -29,13 +40,4 @@ elements downstream. @@@ -## Examples -Below example demonstrates how `grouped` groups the accumulated elements into @scala[`Seq`] @java[`List`] -and maps with other operation. - -Scala -: @@snip [Grouped.scala](/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Grouped.scala) { #grouped } - -Java -: @@snip [SourceOrFlow.java](/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java) { #grouped } diff --git a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/intersperse.md b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/intersperse.md index beaf1d6bf5..7bceb1ec19 100644 --- a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/intersperse.md +++ b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/intersperse.md @@ -16,6 +16,18 @@ Intersperse stream with provided element similar to `List.mkString`. Intersperse stream with provided element similar to `List.mkString`. It can inject start and end marker elements to stream. +## Example + +The following takes a stream of integers, converts them to strings and then adds a `[` at the start, `, ` between each +element and a `]` at the end. + +Scala +: @@snip [Intersperse.scala](/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Intersperse.scala) { #intersperse } + +Java +: @@snip [Intersperse.java](/akka-docs/src/test/java/jdocs/stream/operators/sourceorflow/Intersperse.java) { #intersperse } + + ## Reactive Streams semantics @@@div { .callout } diff --git a/akka-docs/src/test/java/jdocs/stream/operators/sourceorflow/Intersperse.java b/akka-docs/src/test/java/jdocs/stream/operators/sourceorflow/Intersperse.java new file mode 100644 index 0000000000..19167bd472 --- /dev/null +++ b/akka-docs/src/test/java/jdocs/stream/operators/sourceorflow/Intersperse.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2019 Lightbend Inc. + */ + +package jdocs.stream.operators.sourceorflow; + +import akka.actor.ActorSystem; +import akka.stream.javadsl.Source; + +import java.util.Arrays; + +public class Intersperse { + public static void main(String[] args) { + ActorSystem system = ActorSystem.create(); + // #intersperse + Source.from(Arrays.asList(1, 2, 3)) + .map(String::valueOf) + .intersperse("[", ", ", "]") + .runForeach(System.out::print, system); + // prints + // [1, 2, 3] + // #intersperse + system.terminate(); + } +} diff --git a/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Intersperse.scala b/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Intersperse.scala new file mode 100644 index 0000000000..33aa654a2d --- /dev/null +++ b/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Intersperse.scala @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2019 Lightbend Inc. + */ + +package docs.stream.operators.sourceorflow + +import akka.stream.scaladsl.Sink +import akka.stream.scaladsl.Source + +object Intersperse extends App { + import akka.actor.ActorSystem + + implicit val system: ActorSystem = ActorSystem() + + //#intersperse + Source(1 to 4).map(_.toString).intersperse("[", ", ", "]").runWith(Sink.foreach(print)) + // prints + // [1, 2, 3, 4] + //#intersperse + + system.terminate() +}