/** * A flattening transformation operation that takes the first value from the returned Publisher. * <pre> * {@code * Future.ofResult(1).map(i->i+2).mergeMap(i->Flux.just(()->i*3,20); * //Future[9] * * }</pre> * * @param mapper transformation function * @return MonadicValue */ default <R> MonadicValue<R> mergeMap(final Function<? super T, ? extends Publisher<? extends R>> mapper) { return this.flatMap(a -> { final Publisher<? extends R> publisher = mapper.apply(a); final ValueSubscriber<R> sub = ValueSubscriber.subscriber(); publisher.subscribe(sub); final Maybe<R> maybe = sub.toMaybe(); return maybe.fold(in->unit(in),__->emptyUnit()); }); }
/** * A flattening transformation operation that takes the first value from the returned Publisher. * <pre> * {@code * Future.ofResult(1).map(i->i+2).mergeMap(i->Flux.just(()->i*3,20); * //Future[9] * * }</pre> * * @param mapper transformation function * @return MonadicValue */ default <R> MonadicValue<R> mergeMap(final Function<? super T, ? extends Publisher<? extends R>> mapper) { return this.flatMap(a -> { final Publisher<? extends R> publisher = mapper.apply(a); final ValueSubscriber<R> sub = ValueSubscriber.subscriber(); publisher.subscribe(sub); final Maybe<R> maybe = sub.toMaybe(); return maybe.fold(in->unit(in),__->emptyUnit()); }); }