File tree Expand file tree Collapse file tree
main/java/com/jnape/palatable/lambda/functions/builtin/fn1
test/java/com/jnape/palatable/lambda/functions/builtin/fn1 Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments