forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooleans.java
More file actions
148 lines (127 loc) · 4.11 KB
/
Booleans.java
File metadata and controls
148 lines (127 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package fj.function;
import fj.F;
import fj.F2;
import fj.F3;
import static fj.Function.*;
import fj.Monoid;
import fj.data.List;
import fj.data.Stream;
import static fj.Semigroup.disjunctionSemigroup;
import static fj.Semigroup.conjunctionSemigroup;
import static fj.Semigroup.exclusiveDisjunctionSemiGroup;
/**
* Curried logical functions.
*
* @version %build.number%
*/
public final class Booleans {
private Booleans() {
throw new UnsupportedOperationException();
}
/**
* Curried form of logical "inclusive or" (disjunction).
*/
public static final F<Boolean, F<Boolean, Boolean>> or = disjunctionSemigroup.sum();
/**
* Curried form of logical "and" (conjunction).
*/
public static final F<Boolean, F<Boolean, Boolean>> and = conjunctionSemigroup.sum();
/**
* Curried form of logical xor (nonequivalence).
*/
public static final F<Boolean, F<Boolean, Boolean>> xor = exclusiveDisjunctionSemiGroup.sum();
/**
* Logical negation.
*/
public static final F<Boolean, Boolean> not = new F<Boolean, Boolean>() {
public Boolean f(final Boolean p) {
return !p;
}
};
/**
* Curried form of logical "only if" (material implication).
*/
public static final F<Boolean, F<Boolean, Boolean>> implies = curry(new F2<Boolean, Boolean, Boolean>() {
public Boolean f(final Boolean p, final Boolean q) {
return !p || q;
}
});
/**
* Curried form of logical "if" (reverse material implication).
*/
public static final F<Boolean, F<Boolean, Boolean>> if_ = flip(implies);
/**
* Curried form of logical "if and only if" (biconditional, equivalence).
*/
public static final F<Boolean, F<Boolean, Boolean>> iff = compose2(not, xor);
/**
* Curried form of logical "not implies" (nonimplication).
*/
public static final F<Boolean, F<Boolean, Boolean>> nimp = compose2(not, implies);
/**
* Curried form of logical "not if" (reverse nonimplication).
*/
public static final F<Boolean, F<Boolean, Boolean>> nif = compose2(not, if_);
/**
* Curried form of logical "not or".
*/
public static final F<Boolean, F<Boolean, Boolean>> nor = compose2(not, or);
/**
* Returns true if all the elements of the given list are true.
*
* @param l A list to check for all the elements being true.
* @return true if all the elements of the given list are true. False otherwise.
*/
public static boolean and(final List<Boolean> l) {
return Monoid.conjunctionMonoid.sumLeft(l);
}
/**
* Returns true if all the elements of the given stream are true.
*
* @param l A stream to check for all the elements being true.
* @return true if all the elements of the given stream are true. False otherwise.
*/
public static boolean and(final Stream<Boolean> l) {
return Monoid.conjunctionMonoid.sumLeft(l);
}
/**
* Returns true if any element of the given list is true.
*
* @param l A list to check for any element being true.
* @return true if any element of the given list is true. False otherwise.
*/
public static boolean or(final List<Boolean> l) {
return Monoid.disjunctionMonoid.sumLeft(l);
}
/**
* Returns true if any element of the given stream is true.
*
* @param l A stream to check for any element being true.
* @return true if any element of the given stream is true. False otherwise.
*/
public static boolean or(final Stream<Boolean> l) {
return Monoid.disjunctionMonoid.sumLeft(l);
}
/**
* Negates the given predicate.
*
* @param p A predicate to negate.
* @return The negation of the given predicate.
*/
public static <A> F<A, Boolean> not(final F<A, Boolean> p) {
return compose(not, p);
}
/**
* Curried form of conditional. If the first argument is true, returns the second argument,
* otherwise the third argument.
*
* @return A function that returns its second argument if the first argument is true, otherwise the third argument.
*/
public static <A> F<Boolean, F<A, F<A, A>>> cond() {
return curry(new F3<Boolean, A, A, A>() {
public A f(final Boolean p, final A a1, final A a2) {
return p ? a1 : a2;
}
});
}
}