@Test public void flatMap() throws InterruptedException { Try<Future<String>, Throwable> t = acquireNamed("hello") .map(a->{System.out.println("UsingX "+a.getOrElse("")); return a;}) .flatMap(i -> acquireNamed(i.getOrElse("")+ " world") .map(b->{System.out.println("UsingY "+b.getOrElse("")); return b;})) .run(); System.out.println(t); assertTrue(t.isSuccess()); assertThat(t.orElse(null).orElse("-1"),equalTo("HELLO WORLD")); }
public <R> Managed<R> flatMap(Function<? super T, Managed<R>> f){ Managed<T> m = this; return new Managed<R>(){ @Override public <R1> IO<R1> apply(Function<? super R, ? extends IO<R1>> fn) { IO<R1> x = m.apply(r1 -> { IO<R1> r = f.apply(r1).apply(r2 -> fn.apply(r2)); return r; }); return x; } }; }
Managed implements Higher<managed,T>,To<Managed<T>>, Publisher<T> { public <R> Managed<R> flatMap(Function<? super T, Managed<R>> f){ Managed<T> m = this; return new Managed<R>(){ @Override public <R1> IO<R1> apply(Function<? super R, ? extends IO<R1>> fn) { IO<R1> x = m.apply(r1 -> { IO<R1> r = f.apply(r1).apply(r2 -> fn.apply(r2)); return r; }); return x; } }; } }
Managed implements Higher<managed,T>,To<Managed<T>>, Publisher<T> { public <R> Managed<R> flatMap(Function<? super T, Managed<R>> f){ Managed<T> m = this; return new Managed<R>(){ @Override public <R1> IO<R1> apply(Function<? super R, ? extends IO<R1>> fn) { IO<R1> x = m.apply(r1 -> { IO<R1> r = f.apply(r1).apply(r2 -> fn.apply(r2)); return r; }); return x; } }; }  }
Managed implements Higher<managed,T>,To<Managed<T>>, Publisher<T> { public <R> Managed<R> flatMap(Function<? super T, Managed<R>> f){ Managed<T> m = this; return new Managed<R>(){ @Override public <R1> IO<R1> apply(Function<? super R, ? extends IO<R1>> fn) { IO<R1> x = m.apply(r1 -> { IO<R1> r = f.apply(r1).apply(r2 -> fn.apply(r2)); return r; }); return x; } }; }  static Managed<T> managed(T acq, Consumer<T> cleanup); static Managed<T> managed(T acq); static Managed<T> of(Publisher<T> acq, Consumer<T> cleanup); static Managed<T> of(Supplier<? extends T> acq, Consumer<T> cleanup); static Managed<T> of(Supplier<? extends T> acq); static Managed<T> checked(CheckedSupplier<? extends T> acq); static Managed<T> of(Publisher<T> acq); static Managed<T> of(IO<T> acquire, Consumer<T> cleanup); static Managed<T> of(IO<T> acquire); static Managed<Seq<T>> sequence(Iterable<? extends Managed<T>> all); Tupled<T,R2> with(Function<? super T, ? extends R2> fn); static Managed<Seq<R>> traverse(Iterable<T> stream,Function<? super T,Managed<? extends R>> fn); static Managed<Seq<R>> traverse(Function<? super T,? extends R> fn, Iterable<Managed<T>> stream); static Managed<T> narrow(Managed<? extends T> broad); final Managed<R> zip(Managed<T2> b, BiFunction<? super T,? super T2,? extends R> fn); abstract IO<R> apply(Function<? super T,? extends IO<R>> fn); final void forEach(Consumer<? super T> onNext,Consumer<Throwable> errorHandler); final Try<T,Throwable> run(); final T runAndThrowUnexpected(); final R foldRun(Function<? super Try<T,Throwable>,? extends R> transform); final Future<T> future(); final Try<T,Throwable> runAsync(Executor ex); ReactiveSeq<T> stream(); Managed<R> checkedMap(CheckedFunction<? super T, ? extends R> mapper); Managed<R> map(Function<? super T, ? extends R> mapper); Managed<R> checkedFlatMap(CheckedFunction<? super T, Managed<R>> f); Managed<R> flatMap(Function<? super T, Managed<R>> f); static Semigroup<Managed<T>> semigroup(Semigroup<T> s); static Monoid<Managed<T>> monoid(Monoid<T> s); @Override void subscribe(Subscriber<? super T> s); IO<T> io(); }
Managed implements Higher<managed,T>,To<Managed<T>>, Publisher<T> { public <R> Managed<R> flatMap(Function<? super T, Managed<R>> f){ Managed<T> m = this; return new Managed<R>(){ @Override public <R1> IO<R1> apply(Function<? super R, ? extends IO<R1>> fn) { IO<R1> x = m.apply(r1 -> { IO<R1> r = f.apply(r1).apply(r2 -> fn.apply(r2)); return r; }); return x; } }; }  static Managed<T> managed(T acq, Consumer<T> cleanup); static Managed<T> managed(T acq); static Managed<T> of(Publisher<T> acq, Consumer<T> cleanup); static Managed<T> of(Supplier<? extends T> acq, Consumer<T> cleanup); static Managed<T> of(Supplier<? extends T> acq); static Managed<T> checked(CheckedSupplier<? extends T> acq); static Managed<T> of(Publisher<T> acq); static Managed<T> of(IO<T> acquire, Consumer<T> cleanup); static Managed<T> of(IO<T> acquire); static Managed<Seq<T>> sequence(Iterable<? extends Managed<T>> all); Tupled<T,R2> with(Function<? super T, ? extends R2> fn); static Managed<Seq<R>> traverse(Iterable<T> stream,Function<? super T,Managed<? extends R>> fn); static Managed<Seq<R>> traverse(Function<? super T,? extends R> fn, Iterable<Managed<T>> stream); static Managed<T> narrow(Managed<? extends T> broad); final Managed<R> zip(Managed<T2> b, BiFunction<? super T,? super T2,? extends R> fn); abstract IO<R> apply(Function<? super T,? extends IO<R>> fn); final void forEach(Consumer<? super T> onNext,Consumer<Throwable> errorHandler); final Try<T,Throwable> run(); final T runAndThrowUnexpected(); final R foldRun(Function<? super Try<T,Throwable>,? extends R> transform); final Future<T> future(); final Try<T,Throwable> runAsync(Executor ex); ReactiveSeq<T> stream(); Managed<R> checkedMap(CheckedFunction<? super T, ? extends R> mapper); Managed<R> map(Function<? super T, ? extends R> mapper); Managed<R> checkedFlatMap(CheckedFunction<? super T, Managed<R>> f); Managed<R> flatMap(Function<? super T, Managed<R>> f); static Semigroup<Managed<T>> semigroup(Semigroup<T> s); static Monoid<Managed<T>> monoid(Monoid<T> s); @Override void subscribe(Subscriber<? super T> s); IO<T> io();  }