Add examples for zipWithIndex #25468 (#25661)

* Add examples for zipWithIndex #25468

* better examples
This commit is contained in:
Saleh Khazaei 2018-10-02 18:26:26 +03:30 committed by Patrik Nordwall
parent 0101740825
commit 68e382c7f9
3 changed files with 38 additions and 1 deletions

View file

@ -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 }

View file

@ -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())

View file

@ -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
}
}
}