X Tutup
Skip to content

Commit b8b3105

Browse files
committed
#all: returns true if all members of an Iterable succeed for a predicate evaluation; false otherwise
1 parent 17671fc commit b8b3105

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.jnape.palatable.lambda.functions;
2+
3+
import com.jnape.palatable.lambda.DyadicFunction;
4+
import com.jnape.palatable.lambda.MonadicFunction;
5+
6+
public final class All<A> extends DyadicFunction<MonadicFunction<? super A, Boolean>, Iterable<A>, Boolean> {
7+
8+
@Override
9+
public final Boolean apply(MonadicFunction<? super A, Boolean> predicate, Iterable<A> as) {
10+
for (A a : as)
11+
if (!predicate.apply(a))
12+
return false;
13+
14+
return true;
15+
}
16+
17+
public static <A> All<A> all() {
18+
return new All<A>();
19+
}
20+
21+
public static <A> MonadicFunction<Iterable<A>, Boolean> all(MonadicFunction<? super A, Boolean> predicate) {
22+
return All.<A>all().partial(predicate);
23+
}
24+
25+
public static <A> boolean all(MonadicFunction<? super A, Boolean> predicate, Iterable<A> as) {
26+
return all(predicate).apply(as);
27+
}
28+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.jnape.palatable.lambda.functions;
2+
3+
import com.jnape.palatable.lambda.MonadicFunction;
4+
import com.jnape.palatable.lambda.Predicate;
5+
import com.jnape.palatable.traitor.annotations.TestTraits;
6+
import com.jnape.palatable.traitor.runners.Traits;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import testsupport.traits.EmptyIterableSupport;
10+
11+
import static com.jnape.palatable.lambda.builtin.monadic.Always.always;
12+
import static com.jnape.palatable.lambda.functions.All.all;
13+
import static com.jnape.palatable.lambda.functions.Repeat.repeat;
14+
import static java.util.Arrays.asList;
15+
import static org.hamcrest.core.Is.is;
16+
import static org.junit.Assert.assertThat;
17+
18+
@RunWith(Traits.class)
19+
public class AllTest {
20+
21+
private static final MonadicFunction<? super Number, Boolean> EVEN = new Predicate<Number>() {
22+
@Override
23+
public Boolean apply(Number number) {
24+
return number.doubleValue() % 2 == 0;
25+
}
26+
};
27+
28+
@TestTraits({EmptyIterableSupport.class})
29+
public MonadicFunction<Iterable<Object>, Boolean> createTestSubject() {
30+
return all(always(true));
31+
}
32+
33+
@Test
34+
public void trueIfAllElementsMatchPredicate() {
35+
Iterable<Integer> numbers = asList(2, 4, 6, 8);
36+
assertThat(all(EVEN, numbers), is(true));
37+
}
38+
39+
@Test
40+
public void falseIfAnyElementsFailPredicate() {
41+
Iterable<Integer> numbers = asList(1, 2, 4, 6, 8);
42+
assertThat(all(EVEN, numbers), is(false));
43+
}
44+
45+
@Test
46+
public void terminatesIterationImmediatelyUponPredicateFailure() {
47+
assertThat(all(EVEN, repeat(1)), is(false));
48+
}
49+
}

src/test/java/testsupport/traits/EmptyIterableSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
import java.util.ArrayList;
77

8-
public class EmptyIterableSupport implements Trait<MonadicFunction<Iterable, Iterable>> {
8+
public class EmptyIterableSupport implements Trait<MonadicFunction<Iterable, ?>> {
99

1010
@Override
11-
public void test(MonadicFunction<Iterable, Iterable> testSubject) {
11+
public void test(MonadicFunction<Iterable, ?> testSubject) {
1212
try {
1313
testSubject.apply(new ArrayList());
1414
} catch (Exception e) {

0 commit comments

Comments
 (0)
X Tutup