1515import fj .P2 ;
1616import fj .Show ;
1717import 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 .*;
2220import static fj .P .p ;
2321import static fj .P .p2 ;
2422import static fj .Unit .unit ;
@@ -169,9 +167,10 @@ public final Stream<A> toStream() {
169167 */
170168 @ SuppressWarnings ({"unchecked" })
171169 public final Array <A > toArray () {
172- final Object [] a = new Object [length ()];
170+ final int length = length ();
171+ final Object [] a = new Object [length ];
173172 List <A > x = this ;
174- for (int i = 0 ; i < length () ; i ++) {
173+ for (int i = 0 ; i < length ; i ++) {
175174 a [i ] = x .head ();
176175 x = x .tail ();
177176 }
@@ -625,18 +624,18 @@ public final <B> List<B> apply(final List<F<A, B>> lf) {
625624 * @return A new list that has appended the given list.
626625 */
627626 public final List <A > append (final List <A > as ) {
628- return fromList (this ).append (as ). toList ( );
627+ return Buffer . fromList (this ).prependToList (as );
629628 }
630629
631630 /**
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.
633632 *
634633 * @param f The function to apply on each element of the list.
635634 * @param b The beginning value to start the application from.
636635 * @return The final result after the right-fold reduction.
637636 */
638637 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 );
640639 }
641640
642641 /**
@@ -1581,7 +1580,9 @@ public static <A, B> P2<List<A>, List<B>> unzip(final List<P2<A, B>> xs) {
15811580 * @return A list of the given value replicated the given number of times.
15821581 */
15831582 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 ;
15851586 }
15861587
15871588 /**
@@ -1786,7 +1787,7 @@ public Iterator<A> iterator() {
17861787 * Appends (snoc) the given element to this buffer to produce a new buffer.
17871788 *
17881789 * @param a The element to append to this buffer.
1789- * @return A new buffer with the given element appended .
1790+ * @return This buffer.
17901791 */
17911792 public Buffer <A > snoc (final A a ) {
17921793 if (exported )
@@ -1805,10 +1806,10 @@ public Buffer<A> snoc(final A a) {
18051806 }
18061807
18071808 /**
1808- * Appends the given buffer to this buffer.
1809+ * Appends the given list to this buffer.
18091810 *
1810- * @param as The buffer to append to this one .
1811- * @return A new buffer that has appended the given buffer.
1811+ * @param as The list to append to this buffer .
1812+ * @return This buffer.
18121813 */
18131814 public Buffer <A > append (final List <A > as ) {
18141815 for (List <A > xs = as ; xs .isNotEmpty (); xs = xs .tail ())
@@ -1817,6 +1818,28 @@ public Buffer<A> append(final List<A> as) {
18171818 return this ;
18181819 }
18191820
1821+ /**
1822+ * Prepends the elements of this buffer to the given list.
1823+ *
1824+ * @param as the list to which elements are prepended.
1825+ */
1826+ public List <A > prependToList (final List <A > as ) {
1827+ if (isEmpty ()) {
1828+ return as ;
1829+ } else {
1830+ if (exported )
1831+ copy ();
1832+
1833+ tail .tail (as );
1834+ return toList ();
1835+ }
1836+ }
1837+
1838+ /**
1839+ * Returns <code>true</code> if this buffer is empty, <code>false</code> otherwise.
1840+ */
1841+ public boolean isEmpty () { return start .isEmpty (); }
1842+
18201843 /**
18211844 * Returns an immutable list projection of this buffer. Modifications to the underlying buffer
18221845 * will <em>not</em> be reflected in returned lists.
0 commit comments