Skip to content

Commit dea93c2

Browse files
authored
Merge pull request functionaljava#284 from jbgi/optimizable-typeclasses
Optimizable typeclasses
2 parents f583adb + 3d57ed7 commit dea93c2

File tree

13 files changed

+1510
-302
lines changed

13 files changed

+1510
-302
lines changed

core/src/main/java/fj/Equal.java

Lines changed: 209 additions & 50 deletions
Large diffs are not rendered by default.

core/src/main/java/fj/Monoid.java

Lines changed: 720 additions & 67 deletions
Large diffs are not rendered by default.

core/src/main/java/fj/Ord.java

Lines changed: 183 additions & 43 deletions
Large diffs are not rendered by default.

core/src/main/java/fj/Semigroup.java

Lines changed: 316 additions & 80 deletions
Large diffs are not rendered by default.

core/src/main/java/fj/data/List.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ public final List<A> append(final List<A> as) {
715715
* @return The final result after the right-fold reduction.
716716
*/
717717
public final <B> B foldRight(final F<A, F<B, B>> f, final B b) {
718-
return reverse().foldLeft(flip(f), b);
718+
return foldRight(uncurryF2(f), b);
719719
}
720720

721721
/**
@@ -726,7 +726,7 @@ public final <B> B foldRight(final F<A, F<B, B>> f, final B b) {
726726
* @return The final result after the right-fold reduction.
727727
*/
728728
public final <B> B foldRight(final F2<A, B, B> f, final B b) {
729-
return foldRight(curry(f), b);
729+
return reverse().foldLeft(flip(f), b);
730730
}
731731

732732
/**
@@ -1462,7 +1462,7 @@ public static <A> F<List<A>, Integer> length_() {
14621462
* @return The maximum element in this list according to the given ordering.
14631463
*/
14641464
public final A maximum(final Ord<A> o) {
1465-
return foldLeft1(o.max);
1465+
return foldLeft1(o::max);
14661466
}
14671467

14681468
/**
@@ -1472,7 +1472,7 @@ public final A maximum(final Ord<A> o) {
14721472
* @return The minimum element in this list according to the given ordering.
14731473
*/
14741474
public final A minimum(final Ord<A> o) {
1475-
return foldLeft1(o.min);
1475+
return foldLeft1(o::min);
14761476
}
14771477

14781478
public final java.util.List<A> toJavaList() {

core/src/main/java/fj/data/NonEmptyList.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import static fj.Function.flip;
1010
import static fj.Function.identity;
11+
import static fj.Function.uncurryF2;
1112
import static fj.data.Option.some;
1213
import static fj.data.Option.somes;
1314

@@ -73,6 +74,16 @@ public NonEmptyList<A> snoc(final A a) {
7374
*/
7475
public int length() { return 1 + tail.length(); }
7576

77+
/**
78+
* Appends the given list to this list.
79+
*
80+
* @param as The list to append.
81+
* @return A new list with the given list appended.
82+
*/
83+
public NonEmptyList<A> append(final List<A> as) {
84+
return nel(head, tail.append(as));
85+
}
86+
7687
/**
7788
* Appends the given list to this list.
7889
*
@@ -95,14 +106,28 @@ public final A foldRight1(final F<A, F<A, A>> f) {
95106
return reverse().foldLeft1(flip(f));
96107
}
97108

109+
/**
110+
* Performs a right-fold reduction across this list. This function uses O(length) stack space.
111+
*/
112+
public final A foldRight1(final F2<A, A, A> f) {
113+
return reverse().foldLeft1(flip(f));
114+
}
115+
98116
/**
99117
* Performs a left-fold reduction across this list. This function runs in constant space.
100118
*/
101119
public final A foldLeft1(final F<A, F<A, A>> f) {
120+
return foldLeft1(uncurryF2(f));
121+
}
122+
123+
/**
124+
* Performs a left-fold reduction across this list. This function runs in constant space.
125+
*/
126+
public final A foldLeft1(final F2<A, A, A> f) {
102127
A x = head;
103128

104129
for (List<A> xs = tail; !xs.isEmpty(); xs = xs.tail()) {
105-
x = f.f(x).f(xs.head());
130+
x = f.f(x, xs.head());
106131
}
107132

108133
return x;

core/src/main/java/fj/data/Stream.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public final <B> B uncons(final B nil, final F<A, F<P1<Stream<A>>, B>> cons) {
120120
* @return The final result after the right-fold reduction.
121121
*/
122122
public final <B> B foldRight(final F<A, F<P1<B>, B>> f, final B b) {
123-
return isEmpty() ? b : f.f(head()).f(P.lazy(() -> tail()._1().foldRight(f, b)));
123+
return foldRight(uncurryF2(f), b);
124124
}
125125

126126
/**
@@ -131,7 +131,7 @@ public final <B> B foldRight(final F<A, F<P1<B>, B>> f, final B b) {
131131
* @return The final result after the right-fold reduction.
132132
*/
133133
public final <B> B foldRight(final F2<A, P1<B>, B> f, final B b) {
134-
return foldRight(curry(f), b);
134+
return isEmpty() ? b : f.f(head(), P.lazy(() -> tail()._1().foldRight(f, b)));
135135
}
136136

137137
/**
@@ -142,7 +142,7 @@ public final <B> B foldRight(final F2<A, P1<B>, B> f, final B b) {
142142
* @return The final result after the right-fold reduction.
143143
*/
144144
public final <B> B foldRight1(final F<A, F<B, B>> f, final B b) {
145-
return foldRight(compose(Function.<P1<B>, B, B>andThen().f(P1.__1()), f), b);
145+
return foldRight1(uncurryF2(f), b);
146146
}
147147

148148
/**
@@ -153,7 +153,7 @@ public final <B> B foldRight1(final F<A, F<B, B>> f, final B b) {
153153
* @return The final result after the right-fold reduction.
154154
*/
155155
public final <B> B foldRight1(final F2<A, B, B> f, final B b) {
156-
return foldRight1(curry(f), b);
156+
return isEmpty() ? b : f.f(head(), tail()._1().foldRight1(f, b));
157157
}
158158

159159
/**

core/src/main/java/fj/data/optic/Fold.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public final List<A> getAll(final S s) {
3939

4040
/** find the first target of a {@link Fold} matching the predicate */
4141
public final F<S, Option<A>> find(final F<A, Boolean> p) {
42-
return foldMap(Monoid.optionMonoid(), a -> p.f(a) ? Option.some(a) : Option.none());
42+
return foldMap(Monoid.firstOptionMonoid(), a -> p.f(a) ? Option.some(a) : Option.none());
4343
}
4444

4545
/** get the first target of a {@link Fold} */

core/src/main/java/fj/data/optic/PTraversal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public final List<A> getAll(final S s) {
105105

106106
/** find the first target of a {@link PTraversal} matching the predicate */
107107
public final F<S, Option<A>> find(final F<A, Boolean> p) {
108-
return foldMap(Monoid.optionMonoid(), a -> p.f(a) ? Option.some(a) : Option.none());
108+
return foldMap(Monoid.firstOptionMonoid(), a -> p.f(a) ? Option.some(a) : Option.none());
109109
}
110110

111111
/** get the first target of a {@link PTraversal} */

core/src/main/java/fj/function/Doubles.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
import fj.data.Option;
77

88
import static fj.Function.curry;
9-
import static fj.Semigroup.doubleAdditionSemigroup;
10-
import static fj.Semigroup.doubleMultiplicationSemigroup;
119
import static fj.data.Option.none;
1210
import static fj.data.Option.some;
13-
import static java.lang.Math.abs;
1411

1512
/**
1613
* Curried functions over Doubles.
@@ -25,17 +22,17 @@ private Doubles() {
2522
/**
2623
* Curried Double addition.
2724
*/
28-
public static final F<Double, F<Double, Double>> add = doubleAdditionSemigroup.sum();
25+
public static final F<Double, F<Double, Double>> add = x -> y -> x + y;
2926

3027
/**
3128
* Curried Double multiplication.
3229
*/
33-
public static final F<Double, F<Double, Double>> multiply = doubleMultiplicationSemigroup.sum();
30+
public static final F<Double, F<Double, Double>> multiply = x -> y -> x * y;
3431

3532
/**
3633
* Curried Double subtraction.
3734
*/
38-
public static final F<Double, F<Double, Double>> subtract = curry((x, y) -> x - y);
35+
public static final F<Double, F<Double, Double>> subtract = x -> y -> x - y;
3936

4037
/**
4138
* Negation.
@@ -50,12 +47,12 @@ private Doubles() {
5047
/**
5148
* Remainder.
5249
*/
53-
public static final F<Double, F<Double, Double>> remainder = curry((a, b) -> a % b);
50+
public static final F<Double, F<Double, Double>> remainder = x -> y -> x % y;
5451

5552
/**
5653
* Power.
5754
*/
58-
public static final F<Double, F<Double, Double>> power = curry(StrictMath::pow);
55+
public static final F<Double, F<Double, Double>> power = x -> y -> StrictMath.pow(x, y);
5956

6057
/**
6158
* Evenness.
@@ -69,7 +66,7 @@ private Doubles() {
6966
* @return The sum of the doubless in the list.
7067
*/
7168
public static double sum(final List<Double> doubles) {
72-
return Monoid.doubleAdditionMonoid.sumLeft(doubles);
69+
return doubles.foldLeft((x, y) -> x + y, 0.0);
7370
}
7471

7572
/**
@@ -79,7 +76,7 @@ public static double sum(final List<Double> doubles) {
7976
* @return The product of the doubles in the list.
8077
*/
8178
public static double product(final List<Double> doubles) {
82-
return Monoid.doubleMultiplicationMonoid.sumLeft(doubles);
79+
return doubles.foldLeft((x, y) -> x * y, 1.0);
8380
}
8481

8582
/**

0 commit comments

Comments
 (0)