Fix examples (#31002)

This commit is contained in:
Muskan Gupta 2021-12-22 17:41:02 +05:30 committed by GitHub
parent 509b3be617
commit 0d76eb4eeb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -86,7 +86,7 @@ class SourceOrFlow {
// #zip-with-index
Source.from(Arrays.asList("apple", "orange", "banana"))
.zipWithIndex()
.runWith(Sink.foreach(System.out::print), system);
.runForeach(System.out::println, system);
// this will print ('apple', 0), ('orange', 1), ('banana', 2)
// #zip-with-index
}
@ -95,7 +95,7 @@ class SourceOrFlow {
// #zip
Source<String, NotUsed> sourceFruits = Source.from(Arrays.asList("apple", "orange", "banana"));
Source<String, NotUsed> sourceFirstLetters = Source.from(Arrays.asList("A", "O", "B"));
sourceFruits.zip(sourceFirstLetters).runWith(Sink.foreach(System.out::print), system);
sourceFruits.zip(sourceFirstLetters).runForeach(System.out::println, system);
// this will print ('apple', 'A'), ('orange', 'O'), ('banana', 'B')
// #zip
@ -109,7 +109,7 @@ class SourceOrFlow {
.zipWith(
sourceFruits,
(Function2<String, String, String>) (countStr, fruitName) -> countStr + " " + fruitName)
.runWith(Sink.foreach(System.out::print), system);
.runForeach(System.out::println, system);
// this will print 'one apple', 'two orange', 'three banana'
// #zip-with
@ -119,7 +119,7 @@ class SourceOrFlow {
// #prepend
Source<String, NotUsed> ladies = Source.from(Arrays.asList("Emma", "Emily"));
Source<String, NotUsed> gentlemen = Source.from(Arrays.asList("Liam", "William"));
gentlemen.prepend(ladies).runWith(Sink.foreach(System.out::print), system);
gentlemen.prepend(ladies).runForeach(System.out::println, system);
// this will print "Emma", "Emily", "Liam", "William"
// #prepend
@ -129,7 +129,7 @@ class SourceOrFlow {
// #prepend
Source<String, NotUsed> ladies = Source.from(Arrays.asList("Emma", "Emily"));
Source<String, NotUsed> gentlemen = Source.from(Arrays.asList("Liam", "William"));
gentlemen.prependLazy(ladies).runWith(Sink.foreach(System.out::print), system);
gentlemen.prependLazy(ladies).runForeach(System.out::println, system);
// this will print "Emma", "Emily", "Liam", "William"
// #prepend
@ -139,7 +139,7 @@ class SourceOrFlow {
// #concat
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.concat(sourceB).runWith(Sink.foreach(System.out::print), system);
sourceA.concat(sourceB).runForeach(System.out::println, system);
// prints 1, 2, 3, 4, 10, 20, 30, 40
// #concat
@ -149,7 +149,7 @@ class SourceOrFlow {
// #concat
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.concatLazy(sourceB).runWith(Sink.foreach(System.out::print), system);
sourceA.concatLazy(sourceB).runForeach(System.out::println, system);
// prints 1, 2, 3, 4, 10, 20, 30, 40
// #concat
@ -159,7 +159,7 @@ class SourceOrFlow {
// #interleave
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.interleave(sourceB, 2).runWith(Sink.foreach(System.out::print), system);
sourceA.interleave(sourceB, 2).runForeach(System.out::println, system);
// prints 1, 2, 10, 20, 3, 4, 30, 40
// #interleave
@ -169,7 +169,7 @@ class SourceOrFlow {
// #merge
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.merge(sourceB).runWith(Sink.foreach(System.out::print), system);
sourceA.merge(sourceB).runForeach(System.out::println, system);
// merging is not deterministic, can for example print 1, 2, 3, 4, 10, 20, 30, 40
// #merge
@ -180,11 +180,11 @@ class SourceOrFlow {
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.mergePreferred(sourceB, false, false).runWith(Sink.foreach(System.out::print), system);
sourceA.mergePreferred(sourceB, false, false).runForeach(System.out::println, system);
// prints 1, 10, ... since both sources have their first element ready and the left source is
// preferred
sourceA.mergePreferred(sourceB, true, false).runWith(Sink.foreach(System.out::print), system);
sourceA.mergePreferred(sourceB, true, false).runForeach(System.out::println, system);
// prints 10, 1, ... since both sources have their first element ready and the right source is
// preferred
// #mergePreferred
@ -195,9 +195,7 @@ class SourceOrFlow {
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA
.mergePrioritized(sourceB, 99, 1, false)
.runWith(Sink.foreach(System.out::print), system);
sourceA.mergePrioritized(sourceB, 99, 1, false).runForeach(System.out::println, system);
// prints e.g. 1, 10, 2, 3, 4, 20, 30, 40 since both sources have their first element ready and
// the left source has higher priority if both sources have elements ready, sourceA has a
// 99% chance of being picked next while sourceB has a 1% chance
@ -210,13 +208,13 @@ class SourceOrFlow {
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(2, 4, 6, 8));
sourceA
.mergeSorted(sourceB, Comparator.<Integer>naturalOrder())
.runWith(Sink.foreach(System.out::print), system);
.runForeach(System.out::println, system);
// prints 1, 2, 3, 4, 5, 6, 7, 8
Source<Integer, NotUsed> sourceC = Source.from(Arrays.asList(20, 1, 1, 1));
sourceA
.mergeSorted(sourceC, Comparator.<Integer>naturalOrder())
.runWith(Sink.foreach(System.out::print), system);
.runForeach(System.out::println, system);
// prints 1, 3, 5, 7, 20, 1, 1, 1
// #merge-sorted
}
@ -227,10 +225,10 @@ class SourceOrFlow {
Source<String, NotUsed> source2 = Source.from(Arrays.asList("Second source"));
Source<String, NotUsed> emptySource = Source.empty();
source1.orElse(source2).runWith(Sink.foreach(System.out::print), system);
source1.orElse(source2).runForeach(System.out::println, system);
// this will print "First source"
emptySource.orElse(source2).runWith(Sink.foreach(System.out::print), system);
emptySource.orElse(source2).runForeach(System.out::println, system);
// this will print "Second source"
// #or-else
@ -471,7 +469,7 @@ class SourceOrFlow {
Source<String, NotUsed> longWords = words.filter(w -> w.length() > 6);
longWords.runWith(Sink.foreach(System.out::print), system);
longWords.runForeach(System.out::println, system);
// consectetur
// adipiscing
// eiusmod
@ -490,7 +488,7 @@ class SourceOrFlow {
Source<String, NotUsed> longWords = words.filterNot(w -> w.length() <= 6);
longWords.runWith(Sink.foreach(System.out::print), system);
longWords.runForeach(System.out::println, system);
// consectetur
// adipiscing
// eiusmod
@ -503,7 +501,7 @@ class SourceOrFlow {
Source<Integer, NotUsed> fiveIntegers = Source.from(Arrays.asList(1, 2, 3, 4, 5));
Source<Integer, NotUsed> droppedThreeInts = fiveIntegers.drop(3);
droppedThreeInts.runWith(Sink.foreach(System.out::print), system);
droppedThreeInts.runForeach(System.out::println, system);
// 4
// 5
// #drop
@ -514,7 +512,7 @@ class SourceOrFlow {
Source<Integer, NotUsed> droppedWhileNegative =
Source.from(Arrays.asList(-3, -2, -1, 0, 1, 2, 3, -1)).dropWhile(integer -> integer < 0);
droppedWhileNegative.runWith(Sink.foreach(System.out::print), system);
droppedWhileNegative.runForeach(System.out::println, system);
// 1
// 2
// 3