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);
}
Aggregations