File tree 3 files changed +52
-2
lines changed
main/java/com/jnape/palatable/lambda
test/java/com/jnape/palatable/lambda/iteration
3 files changed +52
-2
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import com .jnape .palatable .lambda .functions .Fn1 ;
4
4
import com .jnape .palatable .lambda .functions .Fn2 ;
5
- import com .jnape .palatable .lambda .iteration .FilteringIterator ;
5
+ import com .jnape .palatable .lambda .iteration .FilteringIterable ;
6
6
7
7
import java .util .function .Function ;
8
8
@@ -23,7 +23,7 @@ private Filter() {
23
23
24
24
@ Override
25
25
public Iterable <A > apply (Function <? super A , Boolean > predicate , Iterable <A > as ) {
26
- return () -> new FilteringIterator <>(predicate , as . iterator () );
26
+ return new FilteringIterable <>(predicate , as );
27
27
}
28
28
29
29
@ SuppressWarnings ("unchecked" )
Original file line number Diff line number Diff line change
1
+ package com .jnape .palatable .lambda .iteration ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .Iterator ;
5
+ import java .util .List ;
6
+ import java .util .function .Function ;
7
+
8
+ import static com .jnape .palatable .lambda .functions .builtin .fn2 .All .all ;
9
+ import static java .util .Collections .singletonList ;
10
+
11
+ public final class FilteringIterable <A > implements Iterable <A > {
12
+ private final List <Function <? super A , Boolean >> predicates ;
13
+ private final Iterable <A > as ;
14
+
15
+ public FilteringIterable (Function <? super A , Boolean > predicate , Iterable <A > as ) {
16
+ List <Function <? super A , Boolean >> predicates = new ArrayList <>(singletonList (predicate ));
17
+ while (as instanceof FilteringIterable ) {
18
+ FilteringIterable <A > nested = (FilteringIterable <A >) as ;
19
+ predicates .addAll (nested .predicates );
20
+ as = nested .as ;
21
+ }
22
+ this .predicates = predicates ;
23
+ this .as = as ;
24
+ }
25
+
26
+ @ Override
27
+ public Iterator <A > iterator () {
28
+ Function <? super A , Boolean > metaPredicate = a -> all (p -> p .apply (a ), predicates );
29
+ return new FilteringIterator <>(metaPredicate , as .iterator ());
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ package com .jnape .palatable .lambda .iteration ;
2
+
3
+ import com .jnape .palatable .lambda .functions .Fn1 ;
4
+ import com .jnape .palatable .traitor .annotations .TestTraits ;
5
+ import com .jnape .palatable .traitor .runners .Traits ;
6
+ import org .junit .runner .RunWith ;
7
+ import testsupport .traits .Deforesting ;
8
+
9
+ import static com .jnape .palatable .lambda .functions .builtin .fn1 .Constantly .constantly ;
10
+ import static com .jnape .palatable .lambda .functions .builtin .fn2 .Filter .filter ;
11
+
12
+ @ RunWith (Traits .class )
13
+ public class FilteringIterableTest {
14
+
15
+ @ TestTraits ({Deforesting .class })
16
+ public Fn1 <Iterable <Integer >, Iterable <Integer >> testSubject () {
17
+ return filter (constantly (true ));
18
+ }
19
+ }
You can’t perform that action at this time.
0 commit comments