From 0e0680bd821fbefbc183e5bbf584eed280c67eba Mon Sep 17 00:00:00 2001 From: Muskan Gupta Date: Mon, 12 Oct 2020 14:38:57 +0530 Subject: [PATCH] Adding example for collection operator (#29716) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adding example for collection operator * Modifying example * Add parenthesises to side effect only method Co-authored-by: Johan Andrén --- .../stream/operators/Sink/collection.md | 7 ++++++ .../stream/operators/sink/Collection.scala | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 akka-docs/src/test/scala/docs/stream/operators/sink/Collection.scala diff --git a/akka-docs/src/main/paradox/stream/operators/Sink/collection.md b/akka-docs/src/main/paradox/stream/operators/Sink/collection.md index 21cc0b0cd7..324b319e2e 100644 --- a/akka-docs/src/main/paradox/stream/operators/Sink/collection.md +++ b/akka-docs/src/main/paradox/stream/operators/Sink/collection.md @@ -13,6 +13,13 @@ @scala[Collect values emitted from the stream into an arbitrary collection `That`. The resulting collection is available through a `Future[That]` or when the stream completes. Note that the collection boundaries are those defined in the `CanBuildFrom` associated with the chosen collection. See [The Architecture of Scala 2.13's Collections](https://docs.scala-lang.org/overviews/core/architecture-of-scala-213-collections.html) for more info. The [`seq`](seq.html) operator is a shorthand for `Sink.collection[T, Vector[T]]`.]@java[Operator only available in the Scala API. The closest operator in the Java API is [`Sink.seq`](seq.html).] +## Example + +This example reads the numbers from a source and stores them in the List collection. + +Scala +: @@snip [Collection.scala](/akka-docs/src/test/scala/docs/stream/operators/sink/Collection.scala) { #collection } + ## Reactive Streams semantics @@@ diff --git a/akka-docs/src/test/scala/docs/stream/operators/sink/Collection.scala b/akka-docs/src/test/scala/docs/stream/operators/sink/Collection.scala new file mode 100644 index 0000000000..bbc76b8abc --- /dev/null +++ b/akka-docs/src/test/scala/docs/stream/operators/sink/Collection.scala @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2020 Lightbend Inc. + */ + +package docs.stream.operators.sink + +import akka.actor.ActorSystem +import akka.stream.scaladsl.{ Sink, Source } + +import scala.concurrent.{ ExecutionContextExecutor, Future } + +object Collection { + implicit val system: ActorSystem = ??? + implicit val ec: ExecutionContextExecutor = system.dispatcher + def collectionExample(): Unit = { + //#collection + val source = Source(1 to 5) + val result: Future[List[Int]] = source.runWith(Sink.collection[Int, List[Int]]) + result.foreach(println) + //List(1, 2, 3, 4, 5) + //#collection + } +}