package com.insightfullogic.java8.answers.chapter3;

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

public class FilterUsingReduce {
	
	public static <I> List<I> filter(Stream<I> stream,Predicate<I> predicate){
	
		List<I> initial = new ArrayList<>();
		return stream.reduce(initial,
							(acc,x)->{
								if(predicate.test(x)){
									List<I> newAcc=new ArrayList<I>(acc);
									newAcc.add(x);
									return newAcc;
								}else{
									return acc;
								}
							}
						},FilterUsingReduce::combineLists);
	}


	private static <I> List<I> combinLists(List<I> left,List<I> right){
		List<I> newLeft=new ArrayList<>(left);
		newLeft.addAll(right);
		return newLeft;
	}
}

@@ -208,17 +208,222 @@
 duce %7B%0D%0A
-%09
+%0D%0A%09private static %3CI%3E List%3CI%3E combineLists(List%3CI%3E left, List%3CI%3E right) %7B%0D%0A%09%09List%3CI%3E newLeft = new ArrayList%3C%3E(left);%0D%0A%09%09newLeft.addAll(right);%0D%0A%09%09return newLeft;%0D%0A%09%7D%0D%0A%0D%0A%09//%CB%BC%C2%B7MapUsingReduce,%CE%BFMapUsingReduce%D7%A2
 %0D%0A%09publi
@@ -467,16 +467,17 @@
  stream,
+ 
 Predicat
@@ -491,20 +491,20 @@
 edicate)
+ 
 %7B%0D%0A
-%09
 %0D%0A%09%09List
@@ -572,39 +572,40 @@
 ial,
-%0D%0A%09%09%09%09%09%09%09(
+ (List%3CI%3E 
 acc,
+ I 
 x)
+ 
 -%3E
+ 
 %7B%0D%0A
-%09%09%09%09%09
 %09%09%09if
+ 
 (pre
@@ -619,24 +619,20 @@
 test(x))
+ 
 %7B%0D%0A
-%09%09%09%09%09
 %09%09%09%09List
@@ -641,17 +641,19 @@
 %3E newAcc
-=
+ = 
 new Arra
@@ -658,17 +658,16 @@
 rayList%3C
-I
 %3E(acc);%0D
@@ -671,21 +671,16 @@
 );%0D%0A%09%09%09%09
-%09%09%09
-%09%09
 newAcc.a
@@ -687,21 +687,16 @@
 dd(x);%0D%0A
-%09%09%09%09%09
 %09%09%09%09retu
@@ -714,26 +714,18 @@
 %0A%09%09%09
-%09%09%09%09%09
 %7D
+ 
 else
+ 
 %7B%0D%0A
-%09%09%09%09%09
 %09%09%09%09
@@ -741,37 +741,19 @@
 c;%0D%0A
-%09%09%09%09%09
 %09%09%09%7D%0D%0A
-%09%09%09%09%09%09%09%7D%0D%0A%09%09%09%09
 %09%09%7D,
+ 
 Filt
@@ -791,172 +791,7 @@
 %09%7D%0D%0A
-%0D%0A%0D%0A%09private static %3CI%3E List%3CI%3E combinLists(List%3CI%3E left,List%3CI%3E right)%7B%0D%0A%09%09List%3CI%3E newLeft=new ArrayList%3C%3E(left);%0D%0A%09%09newLeft.addAll(right);%0D%0A%09%09return newLeft;%0D%0A%09%7D%0D%0A
 %7D%0D%0A

complete FilterUsingReduce answer for chapter3