Search in sources :

Example 1 with Reducer

use of cyclops.function.Reducer in project cyclops by aol.

the class MultiReduceOperator method reduce.

@SuppressWarnings({ "rawtypes", "unchecked" })
public List<R> reduce(final Iterable<? extends Monoid<R>> reducers) {
    final Reducer<List<R>, R> m = new Reducer<List<R>, R>() {

        @Override
        public Function<? super R, List<R>> conversion() {
            return a -> Arrays.asList(a);
        }

        @Override
        public List<R> zero() {
            return Streams.stream(reducers).map(r -> r.zero()).collect(Collectors.toList());
        }

        @Override
        public Stream mapToType(final Stream stream) {
            return stream.map(value -> Arrays.asList(value));
        }

        @Override
        public List<R> apply(final List<R> c1, final List<R> c2) {
            final List l = new ArrayList<>();
            int i = 0;
            for (final Monoid next : reducers) {
                l.add(next.apply(c1.get(i), c2.get(0)));
                i++;
            }
            return l;
        }
    };
    return m.foldMap(stream);
}
Also used : Reducer(cyclops.function.Reducer) Arrays(java.util.Arrays) List(java.util.List) Stream(java.util.stream.Stream) Streams(cyclops.companion.Streams) Monoid(cyclops.function.Monoid) AllArgsConstructor(lombok.AllArgsConstructor) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Stream(java.util.stream.Stream) Monoid(cyclops.function.Monoid) Reducer(cyclops.function.Reducer)

Aggregations

Streams (cyclops.companion.Streams)1 Monoid (cyclops.function.Monoid)1 Reducer (cyclops.function.Reducer)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 AllArgsConstructor (lombok.AllArgsConstructor)1