parent
a9d545bf2b
commit
8027f2123b
3 changed files with 82 additions and 0 deletions
|
|
@ -15,6 +15,20 @@ Transform each element into zero or more elements that are individually passed d
|
|||
## Description
|
||||
|
||||
Transform each element into zero or more elements that are individually passed downstream.
|
||||
This can be used to flatten collections into individual stream elements.
|
||||
Returning an empty iterable results in zero elements being passed downstream
|
||||
rather than the stream being cancelled.
|
||||
|
||||
## Example
|
||||
|
||||
The following takes a stream of integers and emits each element twice downstream.
|
||||
|
||||
Scala
|
||||
: @@snip [MapConcat.scala](/akka-docs/src/test/scala/docs/stream/operators/sourceorflow/MapConcat.scala) { #map-concat }
|
||||
|
||||
Java
|
||||
: @@snip [MapConcat.java](/akka-docs/src/test/java/jdocs/stream/operators/sourceorflow/MapConcat.java) { #map-concat }
|
||||
|
||||
|
||||
## Reactive Streams semantics
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.stream.operators.sourceorflow;
|
||||
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.stream.javadsl.Source;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class MapConcat {
|
||||
private static ActorSystem system = null;
|
||||
|
||||
// #map-concat
|
||||
Iterable<Integer> duplicate(int i) {
|
||||
return Arrays.asList(i, i);
|
||||
}
|
||||
|
||||
// #map-concat
|
||||
|
||||
void example() {
|
||||
// #map-concat
|
||||
Source.from(Arrays.asList(1, 2, 3))
|
||||
.mapConcat(i -> duplicate(i))
|
||||
.runForeach(System.out::println, system);
|
||||
// prints:
|
||||
// 1
|
||||
// 1
|
||||
// 2
|
||||
// 2
|
||||
// 3
|
||||
// #map-concat
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Copyright (C) 2019 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package docs.stream.operators.sourceorflow
|
||||
|
||||
import akka.stream.scaladsl.Source
|
||||
|
||||
import scala.concurrent.ExecutionContext
|
||||
|
||||
object MapConcat {
|
||||
|
||||
def mapConcat(): Unit = {
|
||||
import akka.actor.ActorSystem
|
||||
|
||||
implicit val system: ActorSystem = ActorSystem()
|
||||
implicit val ec: ExecutionContext = system.dispatcher
|
||||
|
||||
//#map-concat
|
||||
def duplicate(i: Int): List[Int] = List(i, i)
|
||||
|
||||
Source(1 to 3).mapConcat(i => duplicate(i)).runForeach(println)
|
||||
// prints:
|
||||
// 1
|
||||
// 1
|
||||
// 2
|
||||
// 2
|
||||
// 3
|
||||
//#map-concat
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue