Commit fad92a29 authored by pysaumont's avatar pysaumont
Browse files

Fixes after AC review for chapters 7 and 8

parent 9a5c9987
......@@ -25,14 +25,13 @@ public abstract class List<A> {
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 Result<A> headOption();
public Result<A> lastOption() {
return foldLeft(Result.empty(), x -> Result::success);
}
public Result<A> headOption_() {
return foldRight(Result.empty(), x -> y -> Result.success(x));
public Result<A> headOption() {
throw new IllegalStateException("to be implemented");
}
public List<A> cons(A a) {
......@@ -123,11 +122,6 @@ public abstract class List<A> {
public <B> List<B> flatMap(Function<A, List<B>> f) {
return list();
}
@Override
public Result<A> headOption() {
return Result.empty();
}
}
private static class Cons<A> extends List<A> {
......@@ -249,11 +243,6 @@ public abstract class List<A> {
public <B> List<B> flatMap(Function<A, List<B>> f) {
return foldRight(list(), h -> t -> concat(f.apply(h), t));
}
@Override
public Result<A> headOption() {
return Result.success(head);
}
}
@SuppressWarnings("unchecked")
......
......@@ -36,10 +36,7 @@ public abstract class List<A> {
}
public <A1, A2> Tuple<List<A1>, List<A2>> unzip(Function<A, Tuple<A1, A2>> f) {
return this.foldRight(new Tuple<>(list(), list()), a -> tl -> {
Tuple<A1, A2> t = f.apply(a);
return new Tuple<>(tl._1.cons(t._1), tl._2.cons(t._2));
});
throw new IllegalStateException("To be implemented");
}
@SuppressWarnings("rawtypes")
......@@ -304,6 +301,6 @@ public abstract class List<A> {
}
public static <A1, A2> Tuple<List<A1>, List<A2>> unzip(List<Tuple<A1, A2>> list) {
throw new IllegalStateException("To be implemented");
return list.foldRight(new Tuple<>(list(), list()), t -> tl -> new Tuple<>(tl._1.cons(t._1), tl._2.cons(t._2)));
}
}
......@@ -183,7 +183,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
throw new IllegalStateException("To be implemented");
}
......
......@@ -183,7 +183,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
......@@ -183,7 +183,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
......@@ -183,7 +183,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
......@@ -183,7 +183,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
......@@ -183,7 +183,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
......@@ -186,7 +186,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
......@@ -186,7 +186,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
......@@ -21,7 +21,7 @@ public class ListTest {
);
@Test
public void testGroupBy_() throws Exception {
Map<String, List<Payment>> map = list.groupBy_(x -> x.name);
Map<String, List<Payment>> map = list.groupByImperative(x -> x.name);
assertEquals(Integer.valueOf(361), map.get("Mickey").map(list -> list.foldLeft(0, a -> b -> a + b.amount)).getOrElse(0));
assertEquals(Integer.valueOf(601), map.get("Minnie").map(list -> list.foldLeft(0, a -> b -> a + b.amount)).getOrElse(0));
assertEquals(Integer.valueOf(175), map.get("Donald").map(list -> list.foldLeft(0, a -> b -> a + b.amount)).getOrElse(0));
......
......@@ -26,12 +26,19 @@ public abstract class List<A> {
public abstract List<A> filter(Function<A, Boolean> f);
public abstract <B> List<B> flatMap(Function<A, List<B>> f);
public abstract Result<A> headOption();
public abstract Result<A> lastOption_();
public Result<A> lastOption() {
return foldLeft(Result.empty(), x -> Result::success);
}
public Result<A> lastOption_() {
return isEmpty()
? Result.empty()
: tail().isEmpty()
? Result.success(head())
: tail().lastOption();
}
public List<A> cons(A a) {
return new Cons<>(a, this);
}
......@@ -125,11 +132,6 @@ public abstract class List<A> {
public Result<A> headOption() {
return Result.empty();
}
@Override
public Result<A> lastOption_() {
return Result.empty();
}
}
private static class Cons<A> extends List<A> {
......@@ -256,13 +258,6 @@ public abstract class List<A> {
public Result<A> headOption() {
return Result.success(head);
}
@Override
public Result<A> lastOption_() {
return tail.isEmpty()
? Result.success(head)
: tail.lastOption();
}
}
@SuppressWarnings("unchecked")
......
......@@ -25,14 +25,12 @@ public abstract class List<A> {
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 Result<A> headOption();
public abstract Result<A> lastOption_();
public Result<A> lastOption() {
return foldLeft(Result.empty(), x -> Result::success);
}
public Result<A> headOption_() {
public Result<A> headOption() {
return foldRight(Result.empty(), x -> y -> Result.success(x));
}
......@@ -124,16 +122,6 @@ public abstract class List<A> {
public <B> List<B> flatMap(Function<A, List<B>> f) {
return list();
}
@Override
public Result<A> headOption() {
return Result.empty();
}
@Override
public Result<A> lastOption_() {
return Result.empty();
}
}
private static class Cons<A> extends List<A> {
......@@ -255,18 +243,6 @@ public abstract class List<A> {
public <B> List<B> flatMap(Function<A, List<B>> f) {
return foldRight(list(), h -> t -> concat(f.apply(h), t));
}
@Override
public Result<A> headOption() {
return Result.success(head);
}
@Override
public Result<A> lastOption_() {
return tail.isEmpty()
? Result.success(head)
: tail.lastOption();
}
}
@SuppressWarnings("unchecked")
......
......@@ -274,4 +274,8 @@ public abstract class List<A> {
public static <A> Result<List<A>> sequence(List<Result<A>> list) {
return list.foldRight(Result.success(List.list()), x -> y -> Result.map2(x, y, a -> b -> b.cons(a)));
}
public static <A> Result<List<A>> sequence2(List<Result<A>> list) {
return list.filter(a -> a.isSuccess() || a.isFailure()).foldRight(Result.success(List.list()), x -> y -> Result.map2(x, y, a -> b -> b.cons(a)));
}
}
......@@ -183,7 +183,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
......@@ -183,7 +183,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
......@@ -183,7 +183,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
......@@ -183,7 +183,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
......@@ -183,7 +183,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
......@@ -186,7 +186,7 @@ public abstract class List<A> {
return new Tuple<>(rt._1._1.reverse(), rt._2);
}
public <B> Map<B, List<A>> groupBy_(Function<A, B> f) {
public <B> Map<B, List<A>> groupByImperative(Function<A, B> f) {
List<A> workList = this;
Map<B, List<A>> m = Map.empty();
while (!workList.isEmpty()) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment