Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions core/src/main/java/fj/data/Validation.java
Original file line number Diff line number Diff line change
Expand Up @@ -801,19 +801,19 @@ public <B, C, D> Validation<List<E>, D> accumulate(Validation<E, B> v2, Validati
}
}



public static <A, E> Validation<List<E>, List<A>> sequence(List<Validation<E, A>> list) {
F2<Validation<E, A>, Validation<List<E>, List<A>>, Validation<List<E>, List<A>>> f2 = (v, acc) -> {
if (acc.isFail() && v.isFail()) {
return Validation.validation(acc.toEither().left().map(l -> l.cons(v.fail())));
} else if (acc.isSuccess() && v.isSuccess()) {
return acc.map(l -> l.cons(v.success()));
} else {
return acc;
}
};
return list.foldRight(f2, Validation.success(List.nil()));
/**
* If the list contains a failure, returns a Validation of the fails in the
* list, otherwise returns a successful Validation with the list of
* successful values.
*/
public static <A, E> Validation<List<E>, List<A>> sequenceReduce(List<Validation<E, A>> list) {
if (list.exists(v -> v.isFail())) {
F2<List<E>, Validation<E, A>, List<E>> f = (acc, v) -> acc.cons(v.fail());
return Validation.fail(list.filter(v -> v.isFail()).foldLeft(f, List.nil()).reverse());
} else {
F2<List<A>, Validation<E, A>, List<A>> f = (acc, v) -> acc.cons(v.success());
return Validation.success(list.filter(v -> v.isSuccess()).foldLeft(f, List.nil()).reverse());
}
}

public <C> List<Validation<E, C>> traverseList(F<T, List<C>> f){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import fj.data.Validation;
import fj.test.Arbitrary;
import fj.test.Property;
import fj.test.reflect.CheckParams;
import fj.test.runner.PropertyTestRunner;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import static fj.test.Arbitrary.*;
import static fj.test.Property.implies;
import static fj.test.Property.prop;

/**
* Created by MarkPerry on 3/07/2015.
Expand All @@ -26,7 +25,23 @@ public Property partition() {
boolean b1 = p._1().length() + p._2().length() == list.length();
boolean b2 = p._1().map(s -> Validation.<String, Integer>fail(s)).equals(list.filter(v -> v.isFail()));
boolean b3 = p._2().map(s -> Validation.<String, Integer>success(s)).equals(list.filter(v -> v.isSuccess()));
return Property.prop(b1 && b2 && b3);
return prop(b1 && b2 && b3);
});
}

public Property sequenceReduce() {
Arbitrary<List<Validation<String, Integer>>> al = arbList(arbValidation(arbUSASCIIString, arbInteger));
return Property.property(al, list -> {
Validation<List<String>, List<Integer>> v = Validation.sequenceReduce(list);
Property p1 = implies(
list.exists(v1 -> v1.isFail()),
() -> prop(v.fail().equals(list.filter(v2 -> v2.isFail()).map(v2 -> v2.fail())))
);
Property p2 = implies(
list.forall(v1 -> v1.isSuccess()),
() -> prop(v.success().equals(list.filter(v2 -> v2.isSuccess()).map(v2 -> v2.success())))
);
return p1.and(p2);
});
}

Expand Down