doc: Flow.fold (#27850)

This commit is contained in:
Arnout Engelen 2019-11-18 18:02:44 +02:00 committed by Johan Andrén
parent 472dac469f
commit 548b3589f2
3 changed files with 82 additions and 1 deletions

View file

@ -4,6 +4,7 @@
package jdocs.stream.operators;
import akka.actor.ActorSystem;
import akka.japi.pf.PFBuilder;
import akka.stream.Materializer;
import akka.stream.javadsl.Flow;
@ -45,6 +46,7 @@ import java.util.Comparator;
class SourceOrFlow {
private static Materializer materializer = null;
private static ActorSystem system = null;
void logExample() {
Flow.of(String.class)
@ -271,4 +273,40 @@ class SourceOrFlow {
// 7 (= 7)
// #grouped
}
static
// #fold
class Histogram {
final long low;
final long high;
private Histogram(long low, long high) {
this.low = low;
this.high = high;
}
// Immutable start value
public static Histogram INSTANCE = new Histogram(0L, 0L);
public Histogram add(int number) {
if (number < 100) {
return new Histogram(low + 1L, high);
} else {
return new Histogram(low, high + 1L);
}
}
}
// #fold
void foldExample() {
// #fold
// Folding over the numbers from 1 to 150:
Source.range(1, 150)
.fold(Histogram.INSTANCE, (acc, n) -> acc.add(n))
.runForeach(h -> System.out.println("Histogram(" + h.low + ", " + h.high + ")"), system);
// Prints: Histogram(99, 51)
// #fold
}
}