/** * Answers the question how should this type behave when returned in a flatMap function * by another type? For example - Optional uses comp.of(opt.get()) when a value is present * and comp.empty() when no value is present. * * @param comp * @param apply * @return */ default Object resolveForCrossTypeFlatMap(Comprehender comp,T apply){ return comp.of(apply); }
/** * Answers the question how should this type behave when returned in a flatMap function * by another type? For example - Optional uses comp.of(opt.get()) when a value is present * and comp.empty() when no value is present. * * @param comp * @param apply * @return */ default Object resolveForCrossTypeFlatMap(Comprehender comp,T apply){ return comp.of(apply); }
/** * Answers the question how should this type behave when returned in a flatMap function * by another type? For example - Optional uses comp.of(opt.get()) when a value is present * and comp.empty() when no value is present. * * @param comp * @param apply * @return */ default Object resolveForCrossTypeFlatMap(Comprehender comp,T apply){ return comp.of(apply); }
@Override public Object resolveForCrossTypeFlatMap(Comprehender comp, Eval apply) { return comp.of(apply); }
@Override public Object resolveForCrossTypeFlatMap(Comprehender comp, Reader apply) { return comp.of(apply); }
public static <T> T first(Comprehender<T> comp,Collection c){ Iterator<T> it = c.iterator(); if(it.hasNext()) return comp.of(it.next()); return comp.empty(); } }
public static <T> T first(Comprehender<T> comp,Collection c){ Iterator<T> it = c.iterator(); if(it.hasNext()) return comp.of(it.next()); return comp.empty(); } }
/** * Answers the question how should this type behave when returned in a flatMap function * by another type? For example - Optional uses comp.of(opt.get()) when a value is present * and comp.empty() when no value is present. * * @param comp * @param apply * @return */ @Override public Object resolveForCrossTypeFlatMap(Comprehender comp,Monad apply){ return comp.of(apply.unwrap()); }
public Object resolveForCrossTypeFlatMap(Comprehender comp,SimpleReactStream apply){ return comp.of(apply.block()); } static <T> T unwrapOtherMonadTypes(Comprehender<T> comp,Object apply){
/** * Wrapper around filter * * @param t Monadic type being wrapped * @param p JDK Predicate to wrap * @return Result of call to <pre>{@code t.filter ( i -> p.test(i)); }</pre> */ default Object filter(T t, Predicate p){ return this.flatMap(t, d -> p.test(d) ? of(d) : empty()); }
/** * Wrapper around filter * * @param t Monadic type being wrapped * @param p JDK Predicate to wrap * @return Result of call to <pre>{@code t.filter ( i -> p.test(i)); }</pre> */ default Object filter(T t, Predicate p){ return this.flatMap(t, d -> p.test(d) ? of(d) : empty()); }
/** * Wrapper around filter * * @param t Monadic type being wrapped * @param p JDK Predicate to wrap * @return Result of call to <pre>{@code t.filter ( i -> p.test(i)); }</pre> */ default Object filter(T t, Predicate p){ return this.flatMap(t, d -> p.test(d) ? of(d) : empty()); }
/** * Generate a new instance of the underlying monad with given value * * @param value to construct new instance with * @return new instance of underlying Monad */ default <MONAD,T> MONAD unit(T value) { return (MONAD)new ComprehenderSelector().selectComprehender(unwrap()).of(value); }
/** * Generate a new instance of the underlying monad with given value * * @param value to construct new instance with * @return new instance of underlying Monad */ default <MONAD,T> MONAD unit(T value) { return (MONAD)new ComprehenderSelector().selectComprehender(unwrap()).of(value); }
/** * Generate a new instance of the underlying monad with given value * * @param value to construct new instance with * @return new instance of underlying Monad */ default <MONAD,T> MONAD unit(T value) { return (MONAD)new ComprehenderSelector().selectComprehender(unwrap()).of(value); }
@SuppressWarnings("rawtypes") @Override public Object resolveForCrossTypeFlatMap(Comprehender comp,Try apply){ return apply.matches(c->c.isType( (Success s) -> comp.of(apply.get()) ).anyValues(), c->c.isType( (Failure f) -> comp.empty()).anyValues()); }
@Override public Object resolveForCrossTypeFlatMap(Comprehender comp,FeatureToggle apply){ return apply.matches( c -> c.isType((Enabled e)-> comp.of(e.get())).anyValues(), c-> c.isType( (Disabled d) -> comp.empty()).anyValues() ); }
@SuppressWarnings("rawtypes") @Override public Object resolveForCrossTypeFlatMap(Comprehender comp,Try apply){ return apply.matches(c->c.isType( (Success s) -> comp.of(apply.get()) ).anyValues(), c->c.isType( (Failure f) -> comp.empty()).anyValues()); }
@Override public Object resolveForCrossTypeFlatMap(Comprehender comp,FeatureToggle apply){ return apply.matches( c -> c.isType((Enabled e)-> comp.of(e.get())).anyValues(), c-> c.isType( (Disabled d) -> comp.empty()).anyValues() ); }
/** * Perform a reduction where NT is a (native) Monad type * e.g. * <pre>{@code * Monoid<Optional<Integer>> optionalAdd = Monoid.of(Optional.of(0), (a,b)-> Optional.of(a.get()+b.get())); assertThat(monad(Stream.of(2,8,3,1)).reduceM(optionalAdd).unwrap(),equalTo(Optional.of(14))); }</pre> * * * @param reducer * @return */ default <NT,R> Monad<NT,R> reduceM(Monoid<NT> reducer){ // List(2, 8, 3, 1).foldLeftM(0) {binSmalls} -> Optional(14) // convert to list Optionals return asMonad(monad(stream()).map(value ->new ComprehenderSelector() .selectComprehender(reducer.zero().getClass()).of(value)) .sequence().reduce((Monoid)reducer)); }