2020import static fj .P .p ;
2121import static fj .P .p2 ;
2222import static fj .Unit .unit ;
23- import static fj .data .Array .array ;
2423import static fj .data .Array .mkArray ;
2524import static fj .data .List .Buffer .*;
2625import static fj .data .Option .none ;
3029import static fj .Ord .intOrd ;
3130
3231import fj .Ordering ;
32+ import fj .control .Trampoline ;
3333
3434import java .util .AbstractCollection ;
3535import java .util .Collection ;
@@ -458,7 +458,7 @@ public final <B, C> List<C> bind(final List<B> lb, final F2<A, B, C> f) {
458458 /**
459459 * Promotes the given function of arity-2 to a function on lists.
460460 *
461- * @param f The functio to promote to a function on lists.
461+ * @param f The function to promote to a function on lists.
462462 * @return The given function, promoted to operate on lists.
463463 */
464464 public static <A , B , C > F <List <A >, F <List <B >, List <C >>> liftM2 (final F <A , F <B , C >> f ) {
@@ -626,6 +626,20 @@ public final <B> B foldRight(final F2<A, B, B> f, final B b) {
626626 return foldRight (curry (f ), b );
627627 }
628628
629+ /**
630+ * Performs a right-fold reduction across this list in O(1) stack space.
631+ * @param f The function to apply on each element of the list.
632+ * @param b The beginning value to start the application from.
633+ * @return A Trampoline containing the final result after the right-fold reduction.
634+ */
635+ public final <B > Trampoline <B > foldRightC (final F2 <A , B , B > f , final B b ) {
636+ return Trampoline .suspend (new P1 <Trampoline <B >>() {
637+ public Trampoline <B > _1 () {
638+ return isEmpty () ? Trampoline .pure (b ) : tail ().foldRightC (f , b ).map (f .f (head ()));
639+ }
640+ });
641+ }
642+
629643 /**
630644 * Performs a left-fold reduction across this list. This function runs in constant space.
631645 *
@@ -1080,7 +1094,7 @@ public final <B, C> F<B, List<C>> mapM(final F<A, F<B, C>> f) {
10801094 /**
10811095 * Maps the given function across this list by binding through the Option monad.
10821096 *
1083- * @param f Thefunction to apply through the this list.
1097+ * @param f The function to apply through the this list.
10841098 * @return A possible list of values after binding through the Option monad.
10851099 */
10861100 public final <B > Option <List <B >> mapMOption (final F <A , Option <B >> f ) {
@@ -1099,6 +1113,28 @@ public List<B> f(final List<B> bbs) {
10991113 }, Option .<List <B >>some (List .<B >nil ()));
11001114 }
11011115
1116+ /**
1117+ * Maps the given function across this list by binding through the Trampoline monad.
1118+ *
1119+ * @param f The function to apply through the this list.
1120+ * @return A list of values in the Trampoline monad.
1121+ */
1122+ public final <B > Trampoline <List <B >> mapMTrampoline (final F <A , Trampoline <B >> f ) {
1123+ return foldRight (new F2 <A , Trampoline <List <B >>, Trampoline <List <B >>>() {
1124+ public Trampoline <List <B >> f (final A a , final Trampoline <List <B >> bs ) {
1125+ return f .f (a ).bind (new F <B , Trampoline <List <B >>>() {
1126+ public Trampoline <List <B >> f (final B b ) {
1127+ return bs .map (new F <List <B >, List <B >>() {
1128+ public List <B > f (final List <B > bbs ) {
1129+ return bbs .cons (b );
1130+ }
1131+ });
1132+ }
1133+ });
1134+ }
1135+ }, Trampoline .<List <B >>pure (List .<B >nil ()));
1136+ }
1137+
11021138 /**
11031139 * Returns the index of the first element in this list which is equal (by the given equality) to the
11041140 * query element, or None if there is no such element.
0 commit comments