/** * * * @param reducer Use supplied Monoid to reduce values * @return reduced values */ public final T reduce(Monoid<T> reducer){ return reducer.reduce(monad); } /*
/** * * * @param reducer Use supplied Monoid to reduce values starting via foldLeft * @return Reduced result */ public final static <T> T foldLeft(Stream<T> stream,Monoid<T> reducer){ return reducer.reduce(stream); } /**
/** * Attempt to map this Monad to the same type as the supplied Monoid, using supplied function * Then use Monoid to reduce values * * @param mapper Function to map Monad type * @param reducer Monoid to reduce values * @return Reduce result */ public final <R> R mapReduce(Function<? super T,? extends R> mapper, Monoid<R> reducer){ return reducer.reduce(monad.map(mapper)); }
/** * Attempt to map this Monad to the same type as the supplied Monoid, using supplied function * Then use Monoid to reduce values * * @param mapper Function to map Monad type * @param reducer Monoid to reduce values * @return Reduce result */ public final static <T,R> R mapReduce(Stream<T> stream,Function<? super T,? extends R> mapper, Monoid<R> reducer){ return reducer.reduce(stream.map(mapper)); }
/** * * * @param reducer Use supplied Monoid to reduce values starting via foldRight * @return Reduced result */ public final T foldRight(Monoid<T> reducer){ return reducer.reduce(StreamUtils.reverse(monad)); } /**
/** * * * @param reducer Use supplied Monoid to reduce values starting via foldRight * @return Reduced result */ public final static <T> T foldRight(Stream<T> stream,Monoid<T> reducer){ return reducer.reduce(StreamUtils.reverse(stream)); } /**
/** * Map a given Stream to required type (via mapToType method), then * reduce using this monoid * * Example of multiple reduction using multiple Monoids and PowerTuples * <pre>{@code * Monoid<Integer> sum = Monoid.of(0,(a,b)->a+b); * Monoid<Integer> mult = Monoid.of(1,(a,b)->a*b); * <PTuple2<Integer,Integer>> result = PowerTuples.tuple(sum,mult).<PTuple2<Integer,Integer>>asReducer() * .mapReduce(Stream.of(1,2,3,4)); * * assertThat(result,equalTo(tuple(10,24))); * }</pre> * * @param toReduce Stream to reduce * @return reduced value */ default T mapReduce(Stream toReduce){ return reduce(mapToType(toReduce)); } default T reduce(Stream<T> toReduce){
/** * Convert to a Stream with the result of a reduction operation repeated * specified times * * <pre> * {@code * List<Integer> list = StreamUtils.cycle(Stream.of(1,2,2),Reducers.toCountInt(),3) * . * .collect(Collectors.toList()); * //is asList(3,3,3); * } * </pre> * * @param m * Monoid to be used in reduction * @param times * Number of times value should be repeated * @return Stream with reduced values repeated */ public final static <T> Stream<T> cycle(Stream<T> stream,Monoid<T> m, int times) { return StreamUtils.cycle(times,AsStreamable.asStreamable(m.reduce(stream))); }
/** * Convert to a Stream with the result of a reduction operation repeated * specified times * * <pre> * {@code * List<Integer> list = AsGenericMonad,asMonad(Stream.of(1,2,2)) * .cycle(Reducers.toCountInt(),3) * .collect(Collectors.toList()); * //is asList(3,3,3); * } * </pre> * * @param m * Monoid to be used in reduction * @param times * Number of times value should be repeated * @return Stream with reduced values repeated */ public final SequenceM<T> cycle(Monoid<T> m, int times) { return monad(StreamUtils.cycle(times,AsStreamable.asStreamable(m.reduce(monad)))); }