Example for intersperse (#28343)

This commit is contained in:
Christopher Batey 2019-12-11 14:39:41 +00:00 committed by Johan Andrén
parent 1e67d9221e
commit 17533f077c
4 changed files with 70 additions and 9 deletions

View file

@ -0,0 +1,25 @@
/*
* 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 Intersperse {
public static void main(String[] args) {
ActorSystem system = ActorSystem.create();
// #intersperse
Source.from(Arrays.asList(1, 2, 3))
.map(String::valueOf)
.intersperse("[", ", ", "]")
.runForeach(System.out::print, system);
// prints
// [1, 2, 3]
// #intersperse
system.terminate();
}
}

View file

@ -0,0 +1,22 @@
/*
* Copyright (C) 2019 Lightbend Inc. <https://www.lightbend.com>
*/
package docs.stream.operators.sourceorflow
import akka.stream.scaladsl.Sink
import akka.stream.scaladsl.Source
object Intersperse extends App {
import akka.actor.ActorSystem
implicit val system: ActorSystem = ActorSystem()
//#intersperse
Source(1 to 4).map(_.toString).intersperse("[", ", ", "]").runWith(Sink.foreach(print))
// prints
// [1, 2, 3, 4]
//#intersperse
system.terminate()
}