From 1b68951f86b58c2e4a781b3618d9d5ab5bc84057 Mon Sep 17 00:00:00 2001 From: Christopher Batey Date: Mon, 25 Nov 2019 09:50:41 +0000 Subject: [PATCH] =doc: take and takeWhile (#28078) * Switch java examples to pass in actorsystem instead of materializer --- .../stream/operators/Source-or-Flow/take.md | 9 ++++ .../operators/Source-or-Flow/takeWhile.md | 8 +++ .../jdocs/stream/operators/SourceOrFlow.java | 52 +++++++++++++------ .../stream/operators/sourceorflow/Take.scala | 21 ++++++++ .../operators/sourceorflow/TakeWhile.scala | 20 +++++++ 5 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Take.scala create mode 100644 akka-docs/src/test/scala/docs/stream/operators/sourceorflow/TakeWhile.scala diff --git a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/take.md b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/take.md index 3984f66150..8f24acca3c 100644 --- a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/take.md +++ b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/take.md @@ -16,6 +16,15 @@ Pass `n` incoming elements downstream and then complete Pass `n` incoming elements downstream and then complete +## Example + +Scala +: @@snip [Take.scala](/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Take.scala) { #take } + +Java +: @@snip [SourceOrFlow.java](/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java) { #take } + + ## Reactive Streams semantics @@@div { .callout } diff --git a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/takeWhile.md b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/takeWhile.md index cf49b47422..2e72033b4c 100644 --- a/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/takeWhile.md +++ b/akka-docs/src/main/paradox/stream/operators/Source-or-Flow/takeWhile.md @@ -17,6 +17,14 @@ Pass elements downstream as long as a predicate function return true for the ele Pass elements downstream as long as a predicate function return true for the element include the element when the predicate first return false and then complete. +## Example + +Scala +: @@snip [TakeWhile.scala](/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/TakeWhile.scala) { #take-while } + +Java +: @@snip [SourceOrFlow.java](/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java) { #take-while } + ## Reactive Streams semantics @@@div { .callout } 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 833ada4070..6791a95a26 100644 --- a/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java +++ b/akka-docs/src/test/java/jdocs/stream/operators/SourceOrFlow.java @@ -6,7 +6,6 @@ package jdocs.stream.operators; import akka.actor.ActorSystem; import akka.japi.pf.PFBuilder; -import akka.stream.Materializer; import akka.stream.javadsl.Flow; import akka.NotUsed; @@ -45,7 +44,6 @@ import java.util.Arrays; import java.util.Comparator; class SourceOrFlow { - private static Materializer materializer = null; private static ActorSystem system = null; void logExample() { @@ -62,11 +60,10 @@ 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); + .runWith(Sink.foreach(System.out::print), system); // this will print ('apple', 0), ('orange', 1), ('banana', 2) // #zip-with-index } @@ -75,7 +72,7 @@ class SourceOrFlow { // #zip Source sourceFruits = Source.from(Arrays.asList("apple", "orange", "banana")); Source sourceFirstLetters = Source.from(Arrays.asList("A", "O", "B")); - sourceFruits.zip(sourceFirstLetters).runWith(Sink.foreach(System.out::print), materializer); + sourceFruits.zip(sourceFirstLetters).runWith(Sink.foreach(System.out::print), system); // this will print ('apple', 'A'), ('orange', 'O'), ('banana', 'B') // #zip @@ -89,7 +86,7 @@ class SourceOrFlow { .zipWith( sourceFruits, (Function2) (countStr, fruitName) -> countStr + " " + fruitName) - .runWith(Sink.foreach(System.out::print), materializer); + .runWith(Sink.foreach(System.out::print), system); // this will print 'one apple', 'two orange', 'three banana' // #zip-with @@ -99,7 +96,7 @@ class SourceOrFlow { // #prepend Source ladies = Source.from(Arrays.asList("Emma", "Emily")); Source gentlemen = Source.from(Arrays.asList("Liam", "William")); - gentlemen.prepend(ladies).runWith(Sink.foreach(System.out::print), materializer); + gentlemen.prepend(ladies).runWith(Sink.foreach(System.out::print), system); // this will print "Emma", "Emily", "Liam", "William" // #prepend @@ -109,7 +106,7 @@ class SourceOrFlow { // #concat Source sourceA = Source.from(Arrays.asList(1, 2, 3, 4)); Source sourceB = Source.from(Arrays.asList(10, 20, 30, 40)); - sourceA.concat(sourceB).runWith(Sink.foreach(System.out::print), materializer); + sourceA.concat(sourceB).runWith(Sink.foreach(System.out::print), system); // prints 1, 2, 3, 4, 10, 20, 30, 40 // #concat @@ -119,7 +116,7 @@ class SourceOrFlow { // #interleave Source sourceA = Source.from(Arrays.asList(1, 2, 3, 4)); Source sourceB = Source.from(Arrays.asList(10, 20, 30, 40)); - sourceA.interleave(sourceB, 2).runWith(Sink.foreach(System.out::print), materializer); + sourceA.interleave(sourceB, 2).runWith(Sink.foreach(System.out::print), system); // prints 1, 2, 10, 20, 3, 4, 30, 40 // #interleave @@ -129,7 +126,7 @@ class SourceOrFlow { // #merge Source sourceA = Source.from(Arrays.asList(1, 2, 3, 4)); Source sourceB = Source.from(Arrays.asList(10, 20, 30, 40)); - sourceA.merge(sourceB).runWith(Sink.foreach(System.out::print), materializer); + sourceA.merge(sourceB).runWith(Sink.foreach(System.out::print), system); // merging is not deterministic, can for example print 1, 2, 3, 4, 10, 20, 30, 40 // #merge @@ -141,13 +138,13 @@ class SourceOrFlow { Source sourceB = Source.from(Arrays.asList(2, 4, 6, 8)); sourceA .mergeSorted(sourceB, Comparator.naturalOrder()) - .runWith(Sink.foreach(System.out::print), materializer); + .runWith(Sink.foreach(System.out::print), system); // prints 1, 2, 3, 4, 5, 6, 7, 8 Source sourceC = Source.from(Arrays.asList(20, 1, 1, 1)); sourceA .mergeSorted(sourceC, Comparator.naturalOrder()) - .runWith(Sink.foreach(System.out::print), materializer); + .runWith(Sink.foreach(System.out::print), system); // prints 1, 3, 5, 7, 20, 1, 1, 1 // #merge-sorted } @@ -158,10 +155,10 @@ class SourceOrFlow { Source source2 = Source.from(Arrays.asList("Second source")); Source emptySource = Source.empty(); - source1.orElse(source2).runWith(Sink.foreach(System.out::print), materializer); + source1.orElse(source2).runWith(Sink.foreach(System.out::print), system); // this will print "First source" - emptySource.orElse(source2).runWith(Sink.foreach(System.out::print), materializer); + emptySource.orElse(source2).runWith(Sink.foreach(System.out::print), system); // this will print "Second source" // #or-else @@ -179,7 +176,7 @@ class SourceOrFlow { void scanExample() { // #scan Source source = Source.range(1, 5); - source.scan(0, (acc, x) -> acc + x).runForeach(System.out::println, materializer); + source.scan(0, (acc, x) -> acc + x).runForeach(System.out::println, system); // 0 (= 0) // 1 (= 0 + 1) // 3 (= 0 + 1 + 2) @@ -259,7 +256,7 @@ class SourceOrFlow { // #grouped Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7)) .grouped(3) - .runForeach(System.out::println, materializer); + .runForeach(System.out::println, system); // [1, 2, 3] // [4, 5, 6] // [7] @@ -267,7 +264,7 @@ class SourceOrFlow { Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7)) .grouped(3) .map(g -> g.stream().reduce(0, Integer::sum)) - .runForeach(System.out::println, materializer); + .runForeach(System.out::println, system); // 6 (= 1 + 2 + 3) // 15 (= 4 + 5 + 6) // 7 (= 7) @@ -309,4 +306,25 @@ class SourceOrFlow { // Prints: Histogram(99, 51) // #fold } + + void takeExample() { + // #take + Source.from(Arrays.asList(1, 2, 3, 4, 5)).take(3).runForeach(System.out::println, system); + // this will print: + // 1 + // 2 + // 3 + // #take + } + + void takeWhileExample() { + // #take-while + Source.from(Arrays.asList(1, 2, 3, 4, 5)) + .takeWhile(i -> i < 3) + .runForeach(System.out::println, system); + // this will print: + // 1 + // 2 + // #take-while + } } diff --git a/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Take.scala b/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Take.scala new file mode 100644 index 0000000000..945b01e40d --- /dev/null +++ b/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/Take.scala @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2019 Lightbend Inc. + */ + +package docs.stream.operators.sourceorflow + +object Take { + def takeExample(): Unit = { + import akka.actor.ActorSystem + import akka.stream.scaladsl.Source + + implicit val system: ActorSystem = ActorSystem() + + // #take + Source(1 to 5).take(3).runForeach(println) + // 1 + // 2 + // 3 + // #take + } +} diff --git a/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/TakeWhile.scala b/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/TakeWhile.scala new file mode 100644 index 0000000000..158e301071 --- /dev/null +++ b/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/TakeWhile.scala @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2019 Lightbend Inc. + */ + +package docs.stream.operators.sourceorflow + +object TakeWhile { + def takeWhileExample(): Unit = { + import akka.actor.ActorSystem + import akka.stream.scaladsl.Source + + implicit val system: ActorSystem = ActorSystem() + + // #take-while + Source(1 to 10).takeWhile(_ < 3).runForeach(println) + // 1 + // 2 + // #take-while + } +}