Skip to content

Commit 6acd800

Browse files
authored
Merge pull request functionaljava#283 from jbgi/uncurried-foldLeft
Use uncurried version of foldLeft as default implementation
2 parents d598df1 + 9983ab2 commit 6acd800

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -748,13 +748,7 @@ public final <B> Trampoline<B> foldRightC(final F2<A, B, B> f, final B b) {
748748
* @return The final result after the left-fold reduction.
749749
*/
750750
public final <B> B foldLeft(final F<B, F<A, B>> f, final B b) {
751-
B x = b;
752-
753-
for (List<A> xs = this; !xs.isEmpty(); xs = xs.tail()) {
754-
x = f.f(x).f(xs.head());
755-
}
756-
757-
return x;
751+
return foldLeft(uncurryF2(f), b);
758752
}
759753

760754
/**
@@ -765,7 +759,13 @@ public final <B> B foldLeft(final F<B, F<A, B>> f, final B b) {
765759
* @return The final result after the left-fold reduction.
766760
*/
767761
public final <B> B foldLeft(final F2<B, A, B> f, final B b) {
768-
return foldLeft(curry(f), b);
762+
B x = b;
763+
764+
for (List<A> xs = this; !xs.isEmpty(); xs = xs.tail()) {
765+
x = f.f(x, xs.head());
766+
}
767+
768+
return x;
769769
}
770770

771771
/**
@@ -776,7 +776,9 @@ public final <B> B foldLeft(final F2<B, A, B> f, final B b) {
776776
* @return The final result after the left-fold reduction.
777777
*/
778778
public final A foldLeft1(final F2<A, A, A> f) {
779-
return foldLeft1(curry(f));
779+
if (isEmpty())
780+
throw error("Undefined: foldLeft1 on empty list");
781+
return tail().foldLeft(f, head());
780782
}
781783

782784
/**
@@ -787,9 +789,7 @@ public final A foldLeft1(final F2<A, A, A> f) {
787789
* @return The final result after the left-fold reduction.
788790
*/
789791
public final A foldLeft1(final F<A, F<A, A>> f) {
790-
if (isEmpty())
791-
throw error("Undefined: foldLeft1 on empty list");
792-
return tail().foldLeft(f, head());
792+
return foldLeft1(uncurryF2(f));
793793
}
794794

795795
/**
@@ -798,7 +798,7 @@ public final A foldLeft1(final F<A, F<A, A>> f) {
798798
* @return A new list that is the reverse of this one.
799799
*/
800800
public final List<A> reverse() {
801-
return foldLeft(as -> a -> cons(a, as), List.nil());
801+
return foldLeft((as, a) -> cons(a, as), nil());
802802
}
803803

804804
/**
@@ -901,7 +901,7 @@ public final List<List<A>> partition(final int n) {
901901
* @param f Predicate function.
902902
*/
903903
public final P2<List<A>, List<A>> partition(F<A, Boolean> f) {
904-
P2<List<A>, List<A>> p2 = foldLeft(acc -> a ->
904+
P2<List<A>, List<A>> p2 = foldLeft((acc, a) ->
905905
f.f(a) ? p(acc._1().cons(a), acc._2()) : p(acc._1(), acc._2().cons(a)),
906906
p(nil(), nil())
907907
);
@@ -1390,7 +1390,7 @@ public final <B, C, D> TreeMap<B, D> groupBy(
13901390
final D groupingIdentity,
13911391
final F2<C, D, D> groupingAcc,
13921392
final Ord<B> keyOrd) {
1393-
return this.foldLeft(map -> element -> {
1393+
return this.foldLeft((map, element) -> {
13941394
final B key = keyFunction.f(element);
13951395
final C value = valueFunction.f(element);
13961396
return map.set(key, map.get(key)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public PriorityQueue<K, A> enqueue(K k, A a) {
117117
* Adds nodes using the list of products with priority k and value a. This operation takes O(list.length()).
118118
*/
119119
public PriorityQueue<K, A> enqueue(List<P2<K, A>> list) {
120-
return list.foldLeft(pq -> p -> pq.enqueue(p._1(), p._2()), this);
120+
return list.foldLeft((pq, p) -> pq.enqueue(p._1(), p._2()), this);
121121
}
122122

123123
/**

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@
2222
import java.util.*;
2323

2424
import static fj.Bottom.error;
25-
import static fj.Function.compose;
26-
import static fj.Function.constant;
27-
import static fj.Function.curry;
28-
import static fj.Function.flip;
29-
import static fj.Function.identity;
25+
import static fj.Function.*;
3026
import static fj.P.p;
3127
import static fj.P.p2;
3228
import static fj.Unit.unit;
@@ -167,12 +163,7 @@ public final <B> B foldRight1(final F2<A, B, B> f, final B b) {
167163
* @return The final result after the left-fold reduction.
168164
*/
169165
public final <B> B foldLeft(final F<B, F<A, B>> f, final B b) {
170-
B x = b;
171-
172-
for (Stream<A> xs = this; !xs.isEmpty(); xs = xs.tail()._1())
173-
x = f.f(x).f(xs.head());
174-
175-
return x;
166+
return foldLeft(uncurryF2(f), b);
176167
}
177168

178169
/**
@@ -183,7 +174,12 @@ public final <B> B foldLeft(final F<B, F<A, B>> f, final B b) {
183174
* @return The final result after the left-fold reduction.
184175
*/
185176
public final <B> B foldLeft(final F2<B, A, B> f, final B b) {
186-
return foldLeft(curry(f), b);
177+
B x = b;
178+
179+
for (Stream<A> xs = this; !xs.isEmpty(); xs = xs.tail()._1())
180+
x = f.f(x, xs.head());
181+
182+
return x;
187183
}
188184

189185
/**
@@ -194,7 +190,9 @@ public final <B> B foldLeft(final F2<B, A, B> f, final B b) {
194190
* @return The final result after the left-fold reduction.
195191
*/
196192
public final A foldLeft1(final F2<A, A, A> f) {
197-
return foldLeft1(curry(f));
193+
if (isEmpty())
194+
throw error("Undefined: foldLeft1 on empty list");
195+
return tail()._1().foldLeft(f, head());
198196
}
199197

200198
/**
@@ -205,9 +203,7 @@ public final A foldLeft1(final F2<A, A, A> f) {
205203
* @return The final result after the left-fold reduction.
206204
*/
207205
public final A foldLeft1(final F<A, F<A, A>> f) {
208-
if (isEmpty())
209-
throw error("Undefined: foldLeft1 on empty list");
210-
return tail()._1().foldLeft(f, head());
206+
return foldLeft1(uncurryF2(f));
211207
}
212208

213209
/**
@@ -1188,7 +1184,7 @@ public final P2<Stream<A>, Stream<A>> split(final F<A, Boolean> p) {
11881184
* @return A new stream that is the reverse of this one.
11891185
*/
11901186
public final Stream<A> reverse() {
1191-
return foldLeft(as -> a -> cons(a, () -> as), Stream.nil());
1187+
return foldLeft((as, a) -> cons(a, () -> as), Stream.nil());
11921188
}
11931189

11941190
/**

0 commit comments

Comments
 (0)