/** * A flatMap function that allows flatMapping to a different Monad type * will attempt to lift any non-Monadic values returned into a Monadic form * * @param t Monad to perform flatMap on * @param fn FlatMap function that returns different type * @return flatMap applied and return type converted back to host type, non-Monadic return values lifted into a Monadic form */ default Object liftAndFlatMap(T t, Function fn){ return executeflatMap(t,input ->liftObject(this,fn.apply(input))); } /**
/** * A flatMap function that allows flatMapping to a different Monad type * will attempt to lift any non-Monadic values returned into a Monadic form * * @param t Monad to perform flatMap on * @param fn FlatMap function that returns different type * @return flatMap applied and return type converted back to host type, non-Monadic return values lifted into a Monadic form */ default Object liftAndFlatMap(T t, Function fn){ return executeflatMap(t,input ->liftObject(this,fn.apply(input))); } /**
/** * A flatMap function that allows flatMapping to a different Monad type * will attempt to lift any non-Monadic values returned into a Monadic form * * @param t Monad to perform flatMap on * @param fn FlatMap function that returns different type * @return flatMap applied and return type converted back to host type, non-Monadic return values lifted into a Monadic form */ default Object liftAndFlatMap(T t, Function fn){ return executeflatMap(t,input ->liftObject(this,fn.apply(input))); } /**
/** * Type safe unwrap * <pre> * {@code * Optional<List<String>> stream = anyM("hello","world") .asSequence() .unwrapOptional(); assertThat(stream.get(),equalTo(Arrays.asList("hello","world"))); * } * * </pre> * @return */ public final Optional<List<T>> unwrapOptional(){ Optional unwrapper = Optional.of(1); return (Optional)new ComprehenderSelector() .selectComprehender(unwrapper) .executeflatMap(unwrapper, i-> unwrap()); } /**
/** * <pre> * {@code * CompletableFuture<List<String>> cf = anyM("hello","world") .asSequence() .unwrapCompletableFuture(); assertThat(cf.join(),equalTo(Arrays.asList("hello","world"))); * } * </pre> * @return */ public final CompletableFuture<List<T>> unwrapCompletableFuture(){ CompletableFuture unwrapper = CompletableFuture.completedFuture(1); return (CompletableFuture)new ComprehenderSelector() .selectComprehender(unwrapper) .executeflatMap(unwrapper, i-> unwrap()); }
/** * Type safe unwrap * * <pre> * {@code * Stream<String> stream = anyM("hello","world").asSequence().unwrapStream(); assertThat(stream.collect(Collectors.toList()),equalTo(Arrays.asList("hello","world"))); * } * </pre> * @return Stream with current wrapped values */ public final Stream<T> unwrapStream(){ Stream unwrapper = Stream.of(1); return (Stream)new ComprehenderSelector() .selectComprehender(unwrapper) .executeflatMap(unwrapper, i-> unwrap()); } /**
default <R> Monad<Stream<R>,R> flatMapToStream(Function<? super MONAD,Stream<? extends R>> fn){ Stream stream = Stream.of(1); Monad r = this.<Stream,T>withMonad((Stream)new ComprehenderSelector().selectComprehender( stream).executeflatMap(stream, i-> unwrap())); return r.flatMap(e->e); }
/** * @return This monad coverted to an Optional * * Streams will be converted into <pre>{@code Optional<List<T>> }</pre> * */ default <T> Optional<T> toOptional(){ Optional stream = Optional.of(1); return this.<Optional,T>withMonad((Optional)new ComprehenderSelector().selectComprehender( stream).executeflatMap(stream, i-> unwrap())).unwrap(); }
/** * @return This monad coverted to an Optional * * Streams will be converted into <pre>{@code Optional<List<T>> }</pre> * */ default <T> Optional<T> toOptional(){ Optional stream = Optional.of(1); return this.<Optional,T>withMonad((Optional)new ComprehenderSelector().selectComprehender( stream).executeflatMap(stream, i-> unwrap())).unwrap(); }
/** * @return This monad coverted to an Optional * * Streams will be converted into <pre>{@code Optional<List<T>> }</pre> * */ default <T> Optional<T> toOptional(){ Optional stream = Optional.of(1); return this.<Optional,T>withMonad((Optional)new ComprehenderSelector().selectComprehender( stream).executeflatMap(stream, i-> unwrap())).unwrap(); }
default <R> Monad<Stream<R>,R> flatMapToStream(Function<? super MONAD,Stream<? extends R>> fn){ Stream stream = Stream.of(1); Monad r = this.<Stream,T>withMonad((Stream)new ComprehenderSelector().selectComprehender( stream).executeflatMap(stream, i-> unwrap())); return r.flatMap(e->e); }
default <R> Monad<Stream<R>,R> flatMapToStream(Function<? super MONAD,Stream<? extends R>> fn){ Stream stream = Stream.of(1); Monad r = this.<Stream,T>withMonad((Stream)new ComprehenderSelector().selectComprehender( stream).executeflatMap(stream, i-> unwrap())); return r.flatMap(e->e); }
/** * Unwrap this Monad into a Stream. * If the underlying monad is a Stream it is returned * Otherwise we flatMap the underlying monad to a Stream type */ default Stream<T> stream(){ if(unwrap() instanceof Stream) return (Stream)unwrap(); if(unwrap() instanceof Iterable) return StreamSupport.stream(((Iterable)unwrap()).spliterator(), false); Stream stream = Stream.of(1); return (Stream)withMonad((Stream)new ComprehenderSelector().selectComprehender( stream).executeflatMap(stream, i-> unwrap())) .unwrap(); }
/** * Unwrap this Monad into a Stream. * If the underlying monad is a Stream it is returned * Otherwise we flatMap the underlying monad to a Stream type */ default Stream<T> stream(){ if(unwrap() instanceof Stream) return (Stream)unwrap(); if(unwrap() instanceof Iterable) return StreamSupport.stream(((Iterable)unwrap()).spliterator(), false); Stream stream = Stream.of(1); return (Stream)withMonad((Stream)new ComprehenderSelector().selectComprehender( stream).executeflatMap(stream, i-> unwrap()))//.flatMap(Function.identity()) .unwrap(); }
/** * Unwrap this Monad into a Stream. * If the underlying monad is a Stream it is returned * Otherwise we flatMap the underlying monad to a Stream type */ default Stream<T> stream(){ if(unwrap() instanceof Stream) return (Stream)unwrap(); if(unwrap() instanceof Iterable) return StreamSupport.stream(((Iterable)unwrap()).spliterator(), false); Stream stream = Stream.of(1); return (Stream)withMonad((Stream)new ComprehenderSelector().selectComprehender( stream).executeflatMap(stream, i-> unwrap())) .unwrap(); }
/** * Perform a looser typed flatMap / bind operation * The return type can be another type other than the host type * * @param fn flatMap function * @return flatMapped monad */ default <R> Monad<MONAD,T> bind(Function<? super T,? extends R> fn){ return withMonad((MONAD)new ComprehenderSelector().selectComprehender( unwrap()) .executeflatMap(unwrap(), fn)); } /**
/** * Transform the contents of a Monad into a Monad wrapping a Stream e.g. * Turn an <pre>{@code Optional<List<Integer>> into Stream<Integer> }</pre> * * <pre>{@code * List<List<Integer>> list = monad(Optional.of(Arrays.asList(1,2,3,4,5,6))) .<Stream<Integer>,Integer>streamedMonad() .grouped(3) .collect(Collectors.toList()); assertThat(list.get(0),hasItems(1,2,3)); assertThat(list.get(1),hasItems(4,5,6)); * * }</pre> * * * @return A Monad that wraps a Stream */ default <R,NT> Monad<R,NT> streamedMonad(){ Stream stream = Stream.of(1); Monad r = this.<Stream,T>withMonad((Stream)new ComprehenderSelector().selectComprehender( stream).executeflatMap(stream, i-> unwrap())); return r.flatMap(e->e); } /**
/** * Perform a looser typed flatMap / bind operation * The return type can be another type other than the host type * * @param fn flatMap function * @return flatMapped monad */ default <R> Monad<MONAD,T> bind(Function<? super T,? extends R> fn){ return withMonad((MONAD)new ComprehenderSelector().selectComprehender( unwrap()) .executeflatMap(unwrap(), fn)); } /**
/** * Transform the contents of a Monad into a Monad wrapping a Stream e.g. * Turn an <pre>{@code Optional<List<Integer>> into Stream<Integer> }</pre> * * <pre>{@code * List<List<Integer>> list = monad(Optional.of(Arrays.asList(1,2,3,4,5,6))) .<Stream<Integer>,Integer>streamedMonad() .grouped(3) .collect(Collectors.toList()); assertThat(list.get(0),hasItems(1,2,3)); assertThat(list.get(1),hasItems(4,5,6)); * * }</pre> * * * @return A Monad that wraps a Stream */ default <R,NT> Monad<R,NT> streamedMonad(){ Stream stream = Stream.of(1); Monad r = this.<Stream,T>withMonad((Stream)new ComprehenderSelector().selectComprehender( stream).executeflatMap(stream, i-> unwrap())); return r.flatMap(e->e); } /**
/** * Perform a looser typed flatMap / bind operation * The return type can be another type other than the host type * * @param fn flatMap function * @return flatMapped monad */ default <R> Monad<MONAD,T> bind(Function<? super T,? extends R> fn){ return withMonad((MONAD)new ComprehenderSelector().selectComprehender( unwrap()) .executeflatMap(unwrap(), fn)); } /**