package com.insightfullogic.java8.answers.chapter3;

public class FilterUsingReduce {

}

package com.insightfullogic.java8.answers.chapter3;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

public class FilterUsingReduce {

	public static <I, O> List<O> map(Stream<I> stream, Function<I, O> mapper) {
		return stream.reduce(new ArrayList<O>(), (acc, x) -> {
			
			List<O> newAcc = new ArrayList<>(acc);
			newAcc.add(mapper.apply(x));
			return newAcc;
			
		}, (List<O> left, List<O> right) -> {
			
			List<O> newLeft = new ArrayList<>(left);
			newLeft.addAll(right);
			return newLeft;
			
		});
	}
}

add answer FilterUsingReduce for chapter3