akka / akka-stream-contrib

Add-ons to Akka Stream
Other
172 stars 77 forks source link

replace scala.Either in PartitionWith Java API #135

Open laszlovandenhoek opened 6 years ago

laszlovandenhoek commented 6 years ago

The Java API of PartitionWith takes a function that yields a scala.Either. Constructing that from Java is cumbersome. Either.cond comes close but by-name laziness is not retained.

I would like to replace the Either with something else, but what should it be?

patriknw commented 6 years ago

May I ask what benefits PartitionWith has over using the akka.stream.javadsl.akka.stream.Partition and separate map operations in the downstream legs?

laszlovandenhoek commented 6 years ago

I see one important benefit: clarity about the purpose of the split, by allowing a concise, atomic formulation of the difference between the two outputs. You cannot specify both map operations in a one-liner.

Another (possibly less convincing) argument: if the Either reduces the size of the input element, it saves you from passing a "full" element to another stage, only to reduce it there with a map, as you would with Partition.

A third, completely arbitrary and subjective reason would be that my highest-voted StackOverflow answer concerns PartitionWith :wink:

patriknw commented 6 years ago

For the Java API I'd suggest creating a custom class, PartitionWith.Output<A, B> with clear factory methods such as PartitionWith.selectLeft(A element), PartitionWith.selectRight(B element).