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