groupedAdjacentByWeighted
Partitions this stream into chunks by a delimiter function and a weight limit.
Signature
Source.groupedAdjacentByWeightedSource.groupedAdjacentByWeighted Flow.groupedAdjacentByWeightedFlow.groupedAdjacentByWeighted
Description
Partitions this stream into chunks by a delimiter function and a weight limit.
Adheres to the ActorAttributes.SupervisionStrategy attribute (applied to both the key and cost functions). On Supervision.Resume the offending element is skipped; on Supervision.Restart the current group is dropped.
See also:
- groupedAdjacentBy for a simpler variant.
Examples
The example below demonstrates how groupedAdjacentByWeighted partitions the elements into Seq List.
- Scala
-
source
Source(List("Hello", "HiHi", "Hi", "Hi", "Greetings", "Hey")) .groupedAdjacentByWeighted(_.head, 4)(_.length) .runForeach(println) // prints: // Vector(Hello) // Vector(HiHi) // Vector(Hi, Hi) // Vector(Greetings) // Vector(Hey) - Java
-
source
Source.from(List.of("Hello", "HiHi", "Hi", "Hi", "Greetings", "Hey")) .groupedAdjacentByWeighted(str -> str.charAt(0), 4, str -> (long) str.length()) .runForeach(System.out::println, system); // prints: // [Hello] // [HiHi] // [Hi, Hi] // [Greetings] // [Hey]
Reactive Streams semantics
emits when the delimiter function returns a different value than the previous element’s result, or exceeds the maxWeight.
backpressures when a chunk has been assembled and downstream backpressures
completes when upstream completes