Skip to content
Advertisement

fold not working as expected using akka streams

Below is code I’ve written to try an output the sum of each akka message received which has been edited from this guide :

https://doc.akka.io/docs/akka/current/stream/stream-flows-and-basics.html

JavaScript

The fold operation seems to cause nothing to outputted on the console.

However if I use

JavaScript

instead of

JavaScript

Then the following is outputted :

JavaScript

I’m attempting to group a list and perform a fold operation on each of the groups but the fold is not even being executed. Have I missed a step ?

Advertisement

Answer

Flow.fold does not emit a value until the upstream completes.

Also note that your groupedFlow is an identity flow: it could be removed without changing anything:

  • grouped takes each successive pair of elements and bundles them into a List
  • The map stage converts that List to an ArrayList
  • mapConcat unwraps the ArrayList and emits the elements

The clearest expression of what you’re looking for (a stream of the sum of pairs of successive groups of 2) in Java is probably along the lines of

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement