Beispiel #1
0
 public static <A> List<A> flatten(List<List<A>> list) {
   return foldRight(list, List.<A>list(), x -> y -> concat(x, y));
 }
Beispiel #2
0
 public static <A, B> Result<List<B>> traverse(List<A> list, Function<A, Result<B>> f) {
   return list.foldRight(
       Result.success(List.list()), x -> y -> Result.map2(f.apply(x), y, a -> b -> b.cons(a)));
 }
Beispiel #3
0
 private <B> TailCall<B> foldLeft_(B acc, List<A> list, Function<B, Function<A, B>> f) {
   return list.isEmpty()
       ? ret(acc)
       : sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
 }
Beispiel #4
0
 public static <A, B> B foldRight(List<A> list, B n, Function<A, Function<B, B>> f) {
   return list.foldRight(n, f);
 }
Beispiel #5
0
 private TailCall<List<A>> reverse_(List<A> acc, List<A> list) {
   return list.isEmpty()
       ? ret(acc)
       : sus(() -> reverse_(new Cons<>(list.head(), acc), list.tail()));
 }
Beispiel #6
0
 private TailCall<List<A>> dropWhile_(List<A> list, Function<A, Boolean> f) {
   return !list.isEmpty() && f.apply(list.head())
       ? sus(() -> dropWhile_(list.tail(), f))
       : ret(list);
 }
Beispiel #7
0
 private TailCall<List<A>> drop_(List<A> list, int n) {
   return n <= 0 || list.isEmpty() ? ret(list) : sus(() -> drop_(list.tail(), n - 1));
 }
Beispiel #8
0
 private TailCall<StringBuilder> toString(StringBuilder acc, List<A> list) {
   return list.isEmpty()
       ? ret(acc)
       : sus(() -> toString(acc.append(list.head()).append(", "), list.tail()));
 }
Beispiel #9
0
 private Cons(A head, List<A> tail) {
   this.head = head;
   this.tail = tail;
   this.length = tail.length() + 1;
 }