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

@ -0,0 +1,27 @@
/*
* Copyright (C) 2019 Lightbend Inc. <https://www.lightbend.com>
*/
package docs.stream.operators.sourceorflow
//#imports
import akka.actor.ActorSystem
import akka.stream.scaladsl.Source
//#imports
object Fold extends App {
//#histogram
case class Histogram(low: Long = 0, high: Long = 0) {
def add(i: Int): Histogram = if (i < 100) copy(low = low + 1) else copy(high = high + 1)
}
//#histogram
implicit val sys = ActorSystem()
//#fold
Source(1 to 150).fold(Histogram())((acc, n) => acc.add(n)).runForeach(println)
// Prints: Histogram(99,51)
//#fold
}