optionalVia

For a stream containing optional elements, transforms each element by applying the given viaFlow and passing the value downstream as an optional value.

Simple operators

Signature

Source.optionalViaSource.optionalVia Flow.optionalViaFlow.optionalVia

Description

For a stream containing optional elements, transforms each element by applying the given viaFlow and passing the value downstream as an optional value.

Scala
sourceSource.optionalVia(
  Source(List(Some("1"), None, None, Some("4"))),
  Flow.fromFunction { (string: String) => string.toInt }
)(Keep.none).runForeach(println)
// Some(1)
// None
// None
// Some(4)
Java
sourceFlow<String, Integer, NotUsed> flow = Flow.fromFunction(Integer::parseInt);

Source<Optional<String>, NotUsed> source =
    Source.from(
        Arrays.asList(Optional.of("1"), Optional.empty(), Optional.empty(), Optional.of("4")));

Source.optionalVia(source, flow, Keep.none()).runForeach(System.out::println, system);
// Optional[1]
// Optional.empty
// Optional.empty
// Optional[4]

Reactive Streams semantics

emits while the provided viaFlow is runs with defined elements

backpressures when the viaFlow runs for the defined elements and downstream backpressures

completes when the upstream completes