Skip to content

Commit ade5e47

Browse files
committed
Adding #empty, a simple predicate to determine if an Iterable is empty
1 parent df9ccd2 commit ade5e47

5 files changed

Lines changed: 109 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
77
### Added
88
- `Applicative` arrives; all functors gain applicative properties
99
- `Either#invert` is pulled up into `CoProduct2` and additionally specialized for `Choice2`
10-
- `CoProductN#embed`
10+
- `CoProductN#embed`
11+
- `not`, used for negating predicate functions
12+
- `empty`, used to test if an Iterable is empty
1113

1214
### Changed
1315
- `Functor`, `Bifunctor`, and `Profunctor` (as well as all instances) get a unification parameter
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn1;
2+
3+
import com.jnape.palatable.lambda.functions.specialized.Predicate;
4+
5+
/**
6+
* A predicate that returns true if <code>as</code> is empty; false otherwise.
7+
*
8+
* @param <A> the iterable element type
9+
*/
10+
public final class Empty<A> implements Predicate<Iterable<A>> {
11+
12+
private static final Empty INSTANCE = new Empty();
13+
14+
private Empty() {
15+
}
16+
17+
@Override
18+
public Boolean apply(Iterable<A> as) {
19+
return !as.iterator().hasNext();
20+
}
21+
22+
@SuppressWarnings("unchecked")
23+
public static <A> Empty<A> empty() {
24+
return INSTANCE;
25+
}
26+
27+
public static <A> Boolean empty(Iterable<A> as) {
28+
return Empty.<A>empty().test(as);
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn1;
2+
3+
import com.jnape.palatable.lambda.functions.specialized.BiPredicate;
4+
import com.jnape.palatable.lambda.functions.specialized.Predicate;
5+
6+
import java.util.function.Function;
7+
8+
/**
9+
* Negate a predicate function.
10+
*
11+
* @param <A> the input argument type
12+
*/
13+
public final class Not<A> implements BiPredicate<Function<? super A, Boolean>, A> {
14+
private static final Not INSTANCE = new Not();
15+
16+
private Not() {
17+
}
18+
19+
@Override
20+
public Boolean apply(Function<? super A, Boolean> pred, A a) {
21+
return !pred.apply(a);
22+
}
23+
24+
@SuppressWarnings("unchecked")
25+
public static <A> Not<A> not() {
26+
return INSTANCE;
27+
}
28+
29+
public static <A> Predicate<A> not(Function<? super A, Boolean> pred) {
30+
return Not.<A>not().apply(pred);
31+
}
32+
33+
public static <A> Boolean not(Function<? super A, Boolean> pred, A a) {
34+
return not(pred).apply(a);
35+
}
36+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn1;
2+
3+
import org.junit.Test;
4+
5+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Empty.empty;
6+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Repeat.repeat;
7+
import static java.util.Collections.emptySet;
8+
import static java.util.Collections.singleton;
9+
import static org.junit.Assert.assertFalse;
10+
import static org.junit.Assert.assertTrue;
11+
12+
public class EmptyTest {
13+
14+
@Test
15+
public void emptiness() {
16+
Empty<Object> empty = empty();
17+
18+
assertTrue(empty.apply(emptySet()));
19+
assertFalse(empty.apply(singleton(1)));
20+
assertFalse(empty.apply(repeat(1)));
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn1;
2+
3+
import com.jnape.palatable.lambda.functions.specialized.Predicate;
4+
import org.junit.Test;
5+
6+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Not.not;
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
10+
public class NotTest {
11+
12+
@Test
13+
public void negatesPredicate() {
14+
Predicate<Boolean> isTrue = not(a -> !a);
15+
assertTrue(isTrue.apply(true));
16+
assertFalse(isTrue.apply(false));
17+
}
18+
}

0 commit comments

Comments
 (0)