Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Keep in akka stream mean?

I am learning akka stream and encounter Keep.left and Keep.right in the code:

implicit val system = ActorSystem("KafkaProducer")
implicit val materializer = ActorMaterializer()

val source = Source(List("a", "b", "c"))
val sink = Sink.fold[String, String]("")(_ + _)

val runnable: RunnableGraph[Future[String]] = source.toMat(sink)(Keep.right)
val result: Future[String] = runnable.run()

What does here Keep.right mean?

like image 978
softshipper Avatar asked Nov 04 '17 22:11

softshipper


2 Answers

Every stream processing stage can produce a materialized value which can be captured using viaMat or toMat (as opposed to via() or to(), respectively). In your code snippet, the using of source.toMat(sink) indicates that you're interested in capturing the materialized value of the source and sink and Keep.right keeps the right side (i.e. sink) of the materialized value. Keep.left would keep the materialized value on the left side (i.e. source), and Keep.both would allow you to keep both.

More details is available in relevant sections in the Akka Streams documentation.

like image 78
Leo C Avatar answered Sep 21 '22 23:09

Leo C


Keep.left keeps only the left (first) of the input values. Keep.right only keeps the right (second) of two input values.

like image 31
erip Avatar answered Sep 17 '22 23:09

erip