forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegers.java
More file actions
121 lines (100 loc) · 2.93 KB
/
Integers.java
File metadata and controls
121 lines (100 loc) · 2.93 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
package fj.function;
import fj.F;
import static fj.Function.curry;
import fj.Monoid;
import fj.data.List;
import fj.data.Option;
import static fj.data.Option.some;
import static fj.data.Option.none;
import static fj.Semigroup.intAdditionSemigroup;
import static fj.Semigroup.intMultiplicationSemigroup;
import static java.lang.Math.abs;
/**
* Curried functions over Integers.
*
* @version %build.number%
*/
public final class Integers {
private Integers() {
throw new UnsupportedOperationException();
}
/**
* Curried Integer addition.
*/
public static final F<Integer, F<Integer, Integer>> add = intAdditionSemigroup.sum();
/**
* Curried Integer multiplication.
*/
public static final F<Integer, F<Integer, Integer>> multiply = intMultiplicationSemigroup.sum();
/**
* Curried Integer subtraction.
*/
public static final F<Integer, F<Integer, Integer>> subtract = curry((x, y) -> x - y);
/**
* Negation.
*/
public static final F<Integer, Integer> negate = x -> x * -1;
/**
* Absolute value.
*/
public static final F<Integer, Integer> abs = Math::abs;
/**
* Remainder.
*/
public static final F<Integer, F<Integer, Integer>> remainder = curry((a, b) -> a % b);
/**
* Power.
*/
public static final F<Integer, F<Integer, Integer>> power = curry((a, b) -> (int) StrictMath.pow(a, b));
/**
* Evenness.
*/
public static final F<Integer, Boolean> even = i -> i % 2 == 0;
/**
* Sums a list of integers.
*
* @param ints A list of integers to sum.
* @return The sum of the integers in the list.
*/
public static int sum(final List<Integer> ints) {
return Monoid.intAdditionMonoid.sumLeft(ints);
}
/**
* Returns the product of a list of integers.
*
* @param ints A list of integers to multiply together.
* @return The product of the integers in the list.
*/
public static int product(final List<Integer> ints) {
return Monoid.intMultiplicationMonoid.sumLeft(ints);
}
/**
* A function that converts strings to integers.
*
* @return A function that converts strings to integers.
*/
public static F<String, Option<Integer>> fromString() {
return s -> {
try { return some(Integer.valueOf(s)); }
catch (final NumberFormatException ignored) {
return none();
}
};
}
/**
* A function that returns true if the given integer is greater than zero.
*/
public static final F<Integer, Boolean> gtZero = i -> i > 0;
/**
* A function that returns true if the given integer is greater than or equal to zero.
*/
public static final F<Integer, Boolean> gteZero = i -> i >= 0;
/**
* A function that returns true if the given integer is less than zero.
*/
public static final F<Integer, Boolean> ltZero = i -> i < 0;
/**
* A function that returns true if the given integer is less than or equal to zero.
*/
public static final F<Integer, Boolean> lteZero = i -> i <= 0;
}