forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubles.java
More file actions
161 lines (143 loc) · 3.97 KB
/
Doubles.java
File metadata and controls
161 lines (143 loc) · 3.97 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
149
150
151
152
153
154
155
156
157
158
159
160
161
package fj.function;
import fj.F;
import fj.F2;
import fj.Monoid;
import fj.data.List;
import fj.data.Option;
import static fj.Function.curry;
import static fj.Semigroup.doubleAdditionSemigroup;
import static fj.Semigroup.doubleMultiplicationSemigroup;
import static fj.data.Option.none;
import static fj.data.Option.some;
import static java.lang.Math.abs;
/**
* Curried functions over Doubles.
*
* @version %build.number%
*/
public final class Doubles {
private Doubles() {
throw new UnsupportedOperationException();
}
/**
* Curried Double addition.
*/
public static final F<Double, F<Double, Double>> add = doubleAdditionSemigroup.sum();
/**
* Curried Double multiplication.
*/
public static final F<Double, F<Double, Double>> multiply = doubleMultiplicationSemigroup.sum();
/**
* Curried Double subtraction.
*/
public static final F<Double, F<Double, Double>> subtract = curry(new F2<Double, Double, Double>() {
public Double f(final Double x, final Double y) {
return x - y;
}
});
/**
* Negation.
*/
public static final F<Double, Double> negate = new F<Double, Double>() {
public Double f(final Double x) {
return x * -1;
}
};
/**
* Absolute value.
*/
public static final F<Double, Double> abs = new F<Double, Double>() {
public Double f(final Double x) {
return abs(x);
}
};
/**
* Remainder.
*/
public static final F<Double, F<Double, Double>> remainder = curry(new F2<Double, Double, Double>() {
public Double f(final Double a, final Double b) {
return a % b;
}
});
/**
* Power.
*/
public static final F<Double, F<Double, Double>> power = curry(new F2<Double, Double, Double>() {
public Double f(final Double a, final Double b) {
return StrictMath.pow(a, b);
}
});
/**
* Evenness.
*/
public static final F<Double, Boolean> even = new F<Double, Boolean>() {
public Boolean f(final Double i) {
return i % 2 == 0;
}
};
/**
* Sums a list of doubles.
*
* @param doubles A list of doubles to sum.
* @return The sum of the doubless in the list.
*/
public static double sum(final List<Double> doubles) {
return Monoid.doubleAdditionMonoid.sumLeft(doubles);
}
/**
* Returns the product of a list of doubles.
*
* @param doubles A list of doubles to multiply together.
* @return The product of the doubles in the list.
*/
public static double product(final List<Double> doubles) {
return Monoid.doubleMultiplicationMonoid.sumLeft(doubles);
}
/**
* A function that converts strings to doubles.
*
* @return A function that converts strings to doubles.
*/
public static F<String, Option<Double>> fromString() {
return new F<String, Option<Double>>() {
public Option<Double> f(final String s) {
try { return some(Double.valueOf(s)); }
catch (final NumberFormatException ignored) {
return none();
}
}
};
}
/**
* A function that returns true if the given double is greater than zero.
*/
public static final F<Double, Boolean> gtZero = new F<Double, Boolean>() {
public Boolean f(final Double i) {
return Double.compare(i, 0) > 0;
}
};
/**
* A function that returns true if the given double is greater than or equal to zero.
*/
public static final F<Double, Boolean> gteZero = new F<Double, Boolean>() {
public Boolean f(final Double i) {
return Double.compare(i, 0) >= 0;
}
};
/**
* A function that returns true if the given double is less than zero.
*/
public static final F<Double, Boolean> ltZero = new F<Double, Boolean>() {
public Boolean f(final Double i) {
return Double.compare(i, 0) < 0;
}
};
/**
* A function that returns true if the given double is less than or equal to zero.
*/
public static final F<Double, Boolean> lteZero = new F<Double, Boolean>() {
public Boolean f(final Double i) {
return Double.compare(i, 0) <= 0;
}
};
}