All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aol.cyclops.streams.operators.MultiReduceOperator Maven / Gradle / Ivy

There is a newer version: 7.3.1
Show newest version
package com.aol.cyclops.streams.operators;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import lombok.Value;

import com.aol.cyclops.sequence.Monoid;
import com.aol.cyclops.streams.StreamUtils;
@Value
public class MultiReduceOperator {

	Stream stream;
	public List reduce(Iterable> reducers){
		Monoid m = new Monoid(){
			public List zero(){
				return StreamUtils.stream(reducers).map(r->r.zero()).collect(Collectors.toList());
			}
			public BiFunction combiner(){
				return (c1,c2) -> { 
					List l= new ArrayList<>();
					int i =0;
					for(Monoid next : reducers){
						l.add(next.combiner().apply(c1.get(i),c2.get(0)));
						i++;
					}
					
					
					return l;
				};
			}
			
		
			public Stream mapToType(Stream stream){
				return (Stream) stream.map(value->Arrays.asList(value));
			}
		};
		return (List)m.mapReduce(stream);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy