Commit 9a5c9987 authored by pysaumont's avatar pysaumont
Browse files

Fixes after AC review

parent 26b75120
......@@ -5,9 +5,9 @@ public interface Function<T, U> {
U apply(T arg);
default <V> Function<V, U> compose(Function<V, T> f) {
return x -> apply(f.apply(x));
}
default <V> Function<V, U> compose(Function<V, T> f) {
return x -> apply(f.apply(x));
}
default <V> Function<T, V> andThen(Function<U, V> f) {
return x -> f.apply(apply(x));
......
......@@ -12,7 +12,7 @@ public class SumProductLength {
throw new RuntimeException("To be implemented");
}
public static <A> Integer lengthVaFoldLeft(List<A> list) {
public static <A> Integer lengthViaFoldLeft(List<A> list) {
throw new RuntimeException("To be implemented");
}
}
......@@ -18,7 +18,10 @@ public abstract class List<A> {
public abstract List<A> init();
public abstract int length();
public abstract <B> B foldLeft(B identity, Function<B, Function<A, B>> f);
public abstract <B> B foldRight(B identity, Function<A, Function<B, B>> f);
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
throw new IllegalStateException("To be implemented");
}
public List<A> cons(A a) {
return new Cons<>(a, this);
......@@ -83,11 +86,6 @@ public abstract class List<A> {
public <B> B foldLeft(B identity, Function<B, Function<A, B>> f) {
return identity;
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
throw new IllegalStateException("To be implemented");
}
}
private static class Cons<A> extends List<A> {
......@@ -182,11 +180,6 @@ public abstract class List<A> {
? ret(acc)
: sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
throw new IllegalStateException("To be implemented");
}
}
@SuppressWarnings("unchecked")
......
......@@ -18,7 +18,10 @@ public abstract class List<A> {
public abstract List<A> init();
public abstract int length();
public abstract <B> B foldLeft(B identity, Function<B, Function<A, B>> f);
public abstract <B> B foldRight(B identity, Function<A, Function<B, B>> f);
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
public List<A> cons(A a) {
return new Cons<>(a, this);
......@@ -83,11 +86,6 @@ public abstract class List<A> {
public <B> B foldLeft(B identity, Function<B, Function<A, B>> f) {
return identity;
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return identity;
}
}
private static class Cons<A> extends List<A> {
......@@ -182,11 +180,6 @@ public abstract class List<A> {
? ret(acc)
: sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
}
@SuppressWarnings("unchecked")
......
......@@ -18,7 +18,10 @@ public abstract class List<A> {
public abstract List<A> init();
public abstract int length();
public abstract <B> B foldLeft(B identity, Function<B, Function<A, B>> f);
public abstract <B> B foldRight(B identity, Function<A, Function<B, B>> f);
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
public List<A> cons(A a) {
return new Cons<>(a, this);
......@@ -83,11 +86,6 @@ public abstract class List<A> {
public <B> B foldLeft(B identity, Function<B, Function<A, B>> f) {
return identity;
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return identity;
}
}
private static class Cons<A> extends List<A> {
......@@ -182,11 +180,6 @@ public abstract class List<A> {
? ret(acc)
: sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
}
@SuppressWarnings("unchecked")
......
......@@ -18,9 +18,12 @@ public abstract class List<A> {
public abstract List<A> init();
public abstract int length();
public abstract <B> B foldLeft(B identity, Function<B, Function<A, B>> f);
public abstract <B> B foldRight(B identity, Function<A, Function<B, B>> f);
public abstract <B> List<B> map(Function<A, B> f);
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
public List<A> cons(A a) {
return new Cons<>(a, this);
}
......@@ -85,11 +88,6 @@ public abstract class List<A> {
return identity;
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return identity;
}
@Override
public <B> List<B> map(Function<A, B> f) {
throw new IllegalStateException("To be implemented");
......@@ -189,11 +187,6 @@ public abstract class List<A> {
: sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
@Override
public <B> List<B> map(Function<A, B> f) {
throw new IllegalStateException("To be implemented");
......
......@@ -18,10 +18,13 @@ public abstract class List<A> {
public abstract List<A> init();
public abstract int length();
public abstract <B> B foldLeft(B identity, Function<B, Function<A, B>> f);
public abstract <B> B foldRight(B identity, Function<A, Function<B, B>> f);
public abstract <B> List<B> map(Function<A, B> f);
public abstract List<A> filter(Function<A, Boolean> f);
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
public List<A> cons(A a) {
return new Cons<>(a, this);
}
......@@ -86,11 +89,6 @@ public abstract class List<A> {
return identity;
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return identity;
}
@Override
public <B> List<B> map(Function<A, B> f) {
return list();
......@@ -195,11 +193,6 @@ public abstract class List<A> {
: sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
@Override
public <B> List<B> map(Function<A, B> f) {
return foldRight(list(), h -> t -> new Cons<>(f.apply(h),t));
......
......@@ -18,11 +18,14 @@ public abstract class List<A> {
public abstract List<A> init();
public abstract int length();
public abstract <B> B foldLeft(B identity, Function<B, Function<A, B>> f);
public abstract <B> B foldRight(B identity, Function<A, Function<B, B>> f);
public abstract <B> List<B> map(Function<A, B> f);
public abstract List<A> filter(Function<A, Boolean> f);
public abstract <B> List<B> flatMap(Function<A, List<B>> f);
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
public List<A> cons(A a) {
return new Cons<>(a, this);
}
......@@ -87,11 +90,6 @@ public abstract class List<A> {
return identity;
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return identity;
}
@Override
public <B> List<B> map(Function<A, B> f) {
return list();
......@@ -201,11 +199,6 @@ public abstract class List<A> {
: sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
@Override
public <B> List<B> map(Function<A, B> f) {
return foldRight(list(), h -> t -> new Cons<>(f.apply(h),t));
......
......@@ -23,8 +23,8 @@ public class SumProductLengthTest {
@Test
public void testLengthVaFoldLeft() {
assertEquals(Integer.valueOf(0), lengthVaFoldLeft(list()));
assertEquals(Integer.valueOf(4), lengthVaFoldLeft(list(1.0, 2.0, 3.0, 4.0)));
assertEquals(Integer.valueOf(0), lengthViaFoldLeft(list()));
assertEquals(Integer.valueOf(4), lengthViaFoldLeft(list(1.0, 2.0, 3.0, 4.0)));
}
}
......@@ -12,7 +12,7 @@ public class SumProductLength {
return list.foldLeft(1.0, x -> y -> x * y);
}
public static <A> Integer lengthVaFoldLeft(List<A> list) {
public static <A> Integer lengthViaFoldLeft(List<A> list) {
return list.foldLeft(0, x -> ignore -> x + 1);
}
}
......@@ -18,7 +18,10 @@ public abstract class List<A> {
public abstract List<A> init();
public abstract int length();
public abstract <B> B foldLeft(B identity, Function<B, Function<A, B>> f);
public abstract <B> B foldRight(B identity, Function<A, Function<B, B>> f);
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
public List<A> cons(A a) {
return new Cons<>(a, this);
......@@ -83,11 +86,6 @@ public abstract class List<A> {
public <B> B foldLeft(B identity, Function<B, Function<A, B>> f) {
return identity;
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return identity;
}
}
private static class Cons<A> extends List<A> {
......@@ -182,11 +180,6 @@ public abstract class List<A> {
? ret(acc)
: sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
}
@SuppressWarnings("unchecked")
......
......@@ -18,7 +18,10 @@ public abstract class List<A> {
public abstract List<A> init();
public abstract int length();
public abstract <B> B foldLeft(B identity, Function<B, Function<A, B>> f);
public abstract <B> B foldRight(B identity, Function<A, Function<B, B>> f);
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
public List<A> cons(A a) {
return new Cons<>(a, this);
......@@ -83,11 +86,6 @@ public abstract class List<A> {
public <B> B foldLeft(B identity, Function<B, Function<A, B>> f) {
return identity;
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return identity;
}
}
private static class Cons<A> extends List<A> {
......@@ -182,11 +180,6 @@ public abstract class List<A> {
? ret(acc)
: sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
}
@SuppressWarnings("unchecked")
......
......@@ -18,7 +18,10 @@ public abstract class List<A> {
public abstract List<A> init();
public abstract int length();
public abstract <B> B foldLeft(B identity, Function<B, Function<A, B>> f);
public abstract <B> B foldRight(B identity, Function<A, Function<B, B>> f);
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
public List<A> cons(A a) {
return new Cons<>(a, this);
......@@ -83,11 +86,6 @@ public abstract class List<A> {
public <B> B foldLeft(B identity, Function<B, Function<A, B>> f) {
return identity;
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return identity;
}
}
private static class Cons<A> extends List<A> {
......@@ -182,11 +180,6 @@ public abstract class List<A> {
? ret(acc)
: sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
}
@SuppressWarnings("unchecked")
......
......@@ -18,9 +18,12 @@ public abstract class List<A> {
public abstract List<A> init();
public abstract int length();
public abstract <B> B foldLeft(B identity, Function<B, Function<A, B>> f);
public abstract <B> B foldRight(B identity, Function<A, Function<B, B>> f);
public abstract <B> List<B> map(Function<A, B> f);
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
public List<A> cons(A a) {
return new Cons<>(a, this);
}
......@@ -85,11 +88,6 @@ public abstract class List<A> {
return identity;
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return identity;
}
@Override
public <B> List<B> map(Function<A, B> f) {
return list();
......@@ -189,11 +187,6 @@ public abstract class List<A> {
: sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
@Override
public <B> List<B> map(Function<A, B> f) {
return foldRight(list(), h -> t -> new Cons<>(f.apply(h),t));
......
......@@ -18,10 +18,13 @@ public abstract class List<A> {
public abstract List<A> init();
public abstract int length();
public abstract <B> B foldLeft(B identity, Function<B, Function<A, B>> f);
public abstract <B> B foldRight(B identity, Function<A, Function<B, B>> f);
public abstract <B> List<B> map(Function<A, B> f);
public abstract List<A> filter(Function<A, Boolean> f);
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
public List<A> cons(A a) {
return new Cons<>(a, this);
}
......@@ -86,11 +89,6 @@ public abstract class List<A> {
return identity;
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return identity;
}
@Override
public <B> List<B> map(Function<A, B> f) {
return list();
......@@ -195,11 +193,6 @@ public abstract class List<A> {
: sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
@Override
public <B> List<B> map(Function<A, B> f) {
return foldRight(list(), h -> t -> new Cons<>(f.apply(h),t));
......
......@@ -3,6 +3,8 @@ package com.fpinjava.lists.exercise05_21;
import com.fpinjava.common.Function;
import com.fpinjava.common.TailCall;
import java.util.Arrays;
import static com.fpinjava.common.TailCall.*;
......@@ -18,12 +20,15 @@ public abstract class List<A> {
public abstract List<A> init();
public abstract int length();
public abstract <B> B foldLeft(B identity, Function<B, Function<A, B>> f);
public abstract <B> B foldRight(B identity, Function<A, Function<B, B>> f);
public abstract <B> List<B> map(Function<A, B> f);
public abstract List<A> filter(Function<A, Boolean> f);
public abstract <B> List<B> flatMap(Function<A, List<B>> f);
public abstract List<A> concat(List<A> list2);
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
public List<A> cons(A a) {
return new Cons<>(a, this);
}
......@@ -88,11 +93,6 @@ public abstract class List<A> {
return identity;
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return identity;
}
@Override
public <B> List<B> map(Function<A, B> f) {
return list();
......@@ -207,11 +207,6 @@ public abstract class List<A> {
: sus(() -> foldLeft_(f.apply(acc).apply(list.head()), list.tail(), f));
}
@Override
public <B> B foldRight(B identity, Function<A, Function<B, B>> f) {
return reverse().foldLeft(identity, x -> y -> f.apply(y).apply(x));
}
@Override
public <B> List<B> map(Function<A, B> f) {
return foldRight(list(), h -> t -> new Cons<>(f.apply(h),t));
......@@ -262,4 +257,34 @@ public abstract class List<A> {
public static <A> List<A> flatten(List<List<A>> list) {
return foldRight(list, List.<A>list(), x -> y -> concat(x, y));
}
@SafeVarargs
public static <A> List<A> list_(A... as) {
return list_(list(), as).eval();
}
public static <A> TailCall<List<A>> list_(List<A> acc, A[] as) {
return as.length == 0
? ret(acc)
: sus(() -> list_(new Cons<>(as[0], acc),
Arrays.copyOfRange(as, 1, as.length)));
}
@SafeVarargs
public static <A> List<A> list2(A... as) {
return list2(list(), as).eval();
}
public static <A> TailCall<List<A>> list2(List<A> acc, A[] as) {
return as.length == 0
? ret(acc)
: sus(() -> list2(new Cons<>(as[as.length -1], acc),
Arrays.copyOfRange(as, 0, as.length - 1)));
}
public static void main(String[] args) {
System.out.println(list2(1, 2, 3, 4));
}
}