|
15 | 15 | import fj.P2; |
16 | 16 | import fj.Show; |
17 | 17 | import fj.Unit; |
18 | | -import static fj.Function.curry; |
19 | | -import static fj.Function.constant; |
20 | | -import static fj.Function.identity; |
21 | | -import static fj.Function.compose; |
| 18 | + |
| 19 | +import static fj.Function.*; |
22 | 20 | import static fj.P.p; |
23 | 21 | import static fj.P.p2; |
24 | 22 | import static fj.Unit.unit; |
@@ -169,9 +167,10 @@ public final Stream<A> toStream() { |
169 | 167 | */ |
170 | 168 | @SuppressWarnings({"unchecked"}) |
171 | 169 | public final Array<A> toArray() { |
172 | | - final Object[] a = new Object[length()]; |
| 170 | + final int length = length(); |
| 171 | + final Object[] a = new Object[length]; |
173 | 172 | List<A> x = this; |
174 | | - for (int i = 0; i < length(); i++) { |
| 173 | + for (int i = 0; i < length; i++) { |
175 | 174 | a[i] = x.head(); |
176 | 175 | x = x.tail(); |
177 | 176 | } |
@@ -629,14 +628,14 @@ public final List<A> append(final List<A> as) { |
629 | 628 | } |
630 | 629 |
|
631 | 630 | /** |
632 | | - * Performs a right-fold reduction across this list. This function uses O(length) stack space. |
| 631 | + * Performs a right-fold reduction across this list. |
633 | 632 | * |
634 | 633 | * @param f The function to apply on each element of the list. |
635 | 634 | * @param b The beginning value to start the application from. |
636 | 635 | * @return The final result after the right-fold reduction. |
637 | 636 | */ |
638 | 637 | public final <B> B foldRight(final F<A, F<B, B>> f, final B b) { |
639 | | - return isEmpty() ? b : f.f(head()).f(tail().foldRight(f, b)); |
| 638 | + return reverse().foldLeft(flip(f), b); |
640 | 639 | } |
641 | 640 |
|
642 | 641 | /** |
@@ -1581,7 +1580,9 @@ public static <A, B> P2<List<A>, List<B>> unzip(final List<P2<A, B>> xs) { |
1581 | 1580 | * @return A list of the given value replicated the given number of times. |
1582 | 1581 | */ |
1583 | 1582 | public static <A> List<A> replicate(final int n, final A a) { |
1584 | | - return n <= 0 ? List.<A>nil() : replicate(n - 1, a).cons(a); |
| 1583 | + List<A> list = List.nil(); |
| 1584 | + for (int i = 0; i < n; i++) { list = list.cons(a); } |
| 1585 | + return list; |
1585 | 1586 | } |
1586 | 1587 |
|
1587 | 1588 | /** |
|
0 commit comments