Skip to content

Commit bd665e1

Browse files
committed
NonEmtyList. Add uncurried version of foldRight/foldLeft + append(List).
1 parent 2798b2c commit bd665e1

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

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;

0 commit comments

Comments
 (0)