diff --git a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/zipWithIndex.md b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/zipWithIndex.md index 1ea070b545..cf14460da7 100644 --- a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/zipWithIndex.md +++ b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/zipWithIndex.md @@ -27,3 +27,10 @@ Zips elements of current flow with its indices. @@@ +## Example + +Scala +: @@snip [FlowZipWithIndexSpec.scala](/akka-stream-tests/src/test/scala/akka/stream/scaladsl/FlowZipWithIndexSpec.scala) { #zip-with-index } + +Java +: @@snip [SourceOrFlow.java](/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java) { #zip-with-index } diff --git a/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java b/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java index 3a3b56a59a..e7e4d10eec 100644 --- a/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java +++ b/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java @@ -4,8 +4,15 @@ package jdocs.stream.operators; +import akka.stream.Materializer; import akka.stream.javadsl.Flow; +//#zip-with-index +import akka.stream.javadsl.Sink; +import akka.stream.javadsl.Source; +import java.util.Arrays; + +//#zip-with-index //#log import akka.stream.Attributes; import akka.stream.javadsl.Source; @@ -29,6 +36,16 @@ class SourceOrFlow { ; } + void zipWithIndexExample() { + Materializer materializer = null; + //#zip-with-index + Source.from(Arrays.asList("apple", "orange", "banana")) + .zipWithIndex() + .runWith(Sink.foreach(System.out::print), materializer); + // this will print ('apple', 0), ('orange', 1), ('banana', 2) + //#zip-with-index + } + void conflateExample() { //#conflate Source.cycle(() -> Arrays.asList(1, 10, 100).iterator()) diff --git a/akka-stream-tests/src/test/scala/akka/stream/scaladsl/FlowZipWithIndexSpec.scala b/akka-stream-tests/src/test/scala/akka/stream/scaladsl/FlowZipWithIndexSpec.scala index 08be7b5296..675840b503 100644 --- a/akka-stream-tests/src/test/scala/akka/stream/scaladsl/FlowZipWithIndexSpec.scala +++ b/akka-stream-tests/src/test/scala/akka/stream/scaladsl/FlowZipWithIndexSpec.scala @@ -4,6 +4,11 @@ package akka.stream.scaladsl +//#zip-with-index +import akka.stream.scaladsl.Source +import akka.stream.scaladsl.Sink + +//#zip-with-index import akka.stream.testkit.Utils._ import akka.stream.testkit.scaladsl.StreamTestKit._ import akka.stream.{ ActorMaterializer, ActorMaterializerSettings } @@ -21,7 +26,6 @@ class FlowZipWithIndexSpec extends StreamSpec { "work in the happy case" in assertAllStagesStopped { val probe = TestSubscriber.manualProbe[(Int, Long)]() Source(7 to 10).zipWithIndex.runWith(Sink.fromSubscriber(probe)) - val subscription = probe.expectSubscription() subscription.request(2) @@ -36,5 +40,14 @@ class FlowZipWithIndexSpec extends StreamSpec { probe.expectComplete() } + "work in fruit example" in { + //#zip-with-index + Source(List("apple", "orange", "banana")) + .zipWithIndex + .runWith(Sink.foreach(println)) + // this will print ('apple', 0), ('orange', 1), ('banana', 2) + //#zip-with-index + } + } }