Skip to content

Commit a51c874

Browse files
committed
Merge pull request #165 from mperry/partition-validation
Implemented Validation.partition and arbitrary validations
2 parents 431126a + 21d3701 commit a51c874

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

core/src/main/java/fj/data/Validation.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,16 @@ public Validation<NumberFormatException, Short> f(final String s) {
12871287
}
12881288
};
12891289

1290+
/**
1291+
* Partitions the list into the list of fails and the list of successes
1292+
*/
1293+
public static <A, B> P2<List<A>, List<B>> partition(List<Validation<A, B>> list) {
1294+
return P.p(
1295+
list.filter(v -> v.isFail()).map(v -> v.fail()),
1296+
list.filter(v -> v.isSuccess()).map(v -> v.success())
1297+
);
1298+
}
1299+
12901300
@Override
12911301
public String toString() {
12921302
return Show.validationShow(Show.<E>anyShow(), Show.<T>anyShow()).showS(this);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package fj.data.properties;
2+
3+
import fj.P2;
4+
import fj.data.List;
5+
import fj.data.Validation;
6+
import fj.test.Arbitrary;
7+
import fj.test.Property;
8+
import fj.test.reflect.CheckParams;
9+
import fj.test.runner.PropertyTestRunner;
10+
import org.junit.Assert;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
import static fj.test.Arbitrary.*;
15+
16+
/**
17+
* Created by MarkPerry on 3/07/2015.
18+
*/
19+
@RunWith(PropertyTestRunner.class)
20+
public class ValidationProperties {
21+
22+
public Property partition() {
23+
Arbitrary<List<Validation<String, Integer>>> al = arbList(arbValidation(arbUSASCIIString, arbInteger));
24+
return Property.property(al, list -> {
25+
P2<List<String>, List<Integer>> p = Validation.partition(list);
26+
boolean b1 = p._1().length() + p._2().length() == list.length();
27+
boolean b2 = p._1().map(s -> Validation.<String, Integer>fail(s)).equals(list.filter(v -> v.isFail()));
28+
boolean b3 = p._2().map(s -> Validation.<String, Integer>success(s)).equals(list.filter(v -> v.isSuccess()));
29+
return Property.prop(b1 && b2 && b3);
30+
});
31+
}
32+
33+
}

quickcheck/src/main/java/fj/test/Arbitrary.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,13 @@ public static <A> Arbitrary<NonEmptyList<A>> arbNonEmptyList(final Arbitrary<A>
737737
return arbitrary(Gen.listOf1(aa.gen).map(list -> NonEmptyList.fromList(list).some()));
738738
}
739739

740+
/**
741+
* Returns an arbitrary Validation for the given arbitrary parameters.
742+
*/
743+
public static <A, B> Arbitrary<Validation<A, B>> arbValidation(final Arbitrary<A> aa, final Arbitrary<B> ab) {
744+
return arbitrary(arbBoolean.gen.bind(bool -> bool ? ab.gen.map(b -> Validation.<A, B>success(b)) : aa.gen.map(a -> Validation.<A, B>fail(a))));
745+
}
746+
740747
/**
741748
* Returns an arbitrary implementation for streams.
742749
*

0 commit comments

Comments
 (0)