0
|
1 package fj.function;
|
|
2
|
|
3 import fj.F;
|
|
4 import fj.F2;
|
|
5 import static fj.Function.curry;
|
|
6
|
|
7 import fj.Monoid;
|
|
8 import fj.data.List;
|
|
9 import fj.data.Option;
|
|
10 import static fj.data.Option.some;
|
|
11 import static fj.data.Option.none;
|
|
12 import static fj.Semigroup.intAdditionSemigroup;
|
|
13 import static fj.Semigroup.intMultiplicationSemigroup;
|
|
14
|
|
15 import static java.lang.Math.abs;
|
|
16
|
|
17 /**
|
|
18 * Curried functions over Integers.
|
|
19 *
|
|
20 * @version %build.number%
|
|
21 */
|
|
22 public final class Integers {
|
|
23 private Integers() {
|
|
24 throw new UnsupportedOperationException();
|
|
25 }
|
|
26
|
|
27 /**
|
|
28 * Curried Integer addition.
|
|
29 */
|
|
30 public static final F<Integer, F<Integer, Integer>> add = intAdditionSemigroup.sum();
|
|
31
|
|
32 /**
|
|
33 * Curried Integer multiplication.
|
|
34 */
|
|
35 public static final F<Integer, F<Integer, Integer>> multiply = intMultiplicationSemigroup.sum();
|
|
36
|
|
37 /**
|
|
38 * Curried Integer subtraction.
|
|
39 */
|
|
40 public static final F<Integer, F<Integer, Integer>> subtract = curry(new F2<Integer, Integer, Integer>() {
|
|
41 public Integer f(final Integer x, final Integer y) {
|
|
42 return x - y;
|
|
43 }
|
|
44 });
|
|
45
|
|
46 /**
|
|
47 * Negation.
|
|
48 */
|
|
49 public static final F<Integer, Integer> negate = new F<Integer, Integer>() {
|
|
50 public Integer f(final Integer x) {
|
|
51 return x * -1;
|
|
52 }
|
|
53 };
|
|
54
|
|
55 /**
|
|
56 * Absolute value.
|
|
57 */
|
|
58 public static final F<Integer, Integer> abs = new F<Integer, Integer>() {
|
|
59 public Integer f(final Integer x) {
|
|
60 return abs(x);
|
|
61 }
|
|
62 };
|
|
63
|
|
64 /**
|
|
65 * Remainder.
|
|
66 */
|
|
67 public static final F<Integer, F<Integer, Integer>> remainder = curry(new F2<Integer, Integer, Integer>() {
|
|
68 public Integer f(final Integer a, final Integer b) {
|
|
69 return a % b;
|
|
70 }
|
|
71 });
|
|
72
|
|
73 /**
|
|
74 * Power.
|
|
75 */
|
|
76 public static final F<Integer, F<Integer, Integer>> power = curry(new F2<Integer, Integer, Integer>() {
|
|
77 public Integer f(final Integer a, final Integer b) {
|
|
78 return (int) StrictMath.pow(a, b);
|
|
79 }
|
|
80 });
|
|
81
|
|
82 /**
|
|
83 * Evenness.
|
|
84 */
|
|
85 public static final F<Integer, Boolean> even = new F<Integer, Boolean>() {
|
|
86 public Boolean f(final Integer i) {
|
|
87 return i % 2 == 0;
|
|
88 }
|
|
89 };
|
|
90
|
|
91 /**
|
|
92 * Sums a list of integers.
|
|
93 *
|
|
94 * @param ints A list of integers to sum.
|
|
95 * @return The sum of the integers in the list.
|
|
96 */
|
|
97 public static int sum(final List<Integer> ints) {
|
|
98 return Monoid.intAdditionMonoid.sumLeft(ints);
|
|
99 }
|
|
100
|
|
101 /**
|
|
102 * Returns the product of a list of integers.
|
|
103 *
|
|
104 * @param ints A list of integers to multiply together.
|
|
105 * @return The product of the integers in the list.
|
|
106 */
|
|
107 public static int product(final List<Integer> ints) {
|
|
108 return Monoid.intMultiplicationMonoid.sumLeft(ints);
|
|
109 }
|
|
110
|
|
111 /**
|
|
112 * A function that converts strings to integers.
|
|
113 *
|
|
114 * @return A function that converts strings to integers.
|
|
115 */
|
|
116 public static F<String, Option<Integer>> fromString() {
|
|
117 return new F<String, Option<Integer>>() {
|
|
118 public Option<Integer> f(final String s) {
|
|
119 try { return some(Integer.valueOf(s)); }
|
|
120 catch (final NumberFormatException ignored) {
|
|
121 return none();
|
|
122 }
|
|
123 }
|
|
124 };
|
|
125 }
|
|
126
|
|
127 /**
|
|
128 * A function that returns true if the given integer is greater than zero.
|
|
129 */
|
|
130 public static final F<Integer, Boolean> gtZero = new F<Integer, Boolean>() {
|
|
131 public Boolean f(final Integer i) {
|
|
132 return i > 0;
|
|
133 }
|
|
134 };
|
|
135
|
|
136 /**
|
|
137 * A function that returns true if the given integer is greater than or equal to zero.
|
|
138 */
|
|
139 public static final F<Integer, Boolean> gteZero = new F<Integer, Boolean>() {
|
|
140 public Boolean f(final Integer i) {
|
|
141 return i >= 0;
|
|
142 }
|
|
143 };
|
|
144
|
|
145 /**
|
|
146 * A function that returns true if the given integer is less than zero.
|
|
147 */
|
|
148 public static final F<Integer, Boolean> ltZero = new F<Integer, Boolean>() {
|
|
149 public Boolean f(final Integer i) {
|
|
150 return i < 0;
|
|
151 }
|
|
152 };
|
|
153
|
|
154 /**
|
|
155 * A function that returns true if the given integer is less than or equal to zero.
|
|
156 */
|
|
157 public static final F<Integer, Boolean> lteZero = new F<Integer, Boolean>() {
|
|
158 public Boolean f(final Integer i) {
|
|
159 return i <= 0;
|
|
160 }
|
|
161 };
|
|
162 }
|