Skip to content

Commit fdc4c5f

Browse files
committed
Merge pull request functionaljava#146 from orionll/master
Added Equal.notEq(), List.isPrefixOf(), List.isSuffixOf()
2 parents 4f48317 + fa9e803 commit fdc4c5f

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

core/src/main/java/fj/Equal.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ public boolean eq(final A a1, final A a2) {
3838
return f.f(a1).f(a2);
3939
}
4040

41+
/**
42+
* Returns <code>true</code> if the two given arguments are not equal, <code>false</code> otherwise.
43+
*
44+
* @param a1 An object to test for inequality against another.
45+
* @param a2 An object to test for inequality against another.
46+
* @return <code>true</code> if the two given arguments are not equal, <code>false</code> otherwise.
47+
*/
48+
public boolean notEq(final A a1, final A a2) {
49+
return !eq(a1, a2);
50+
}
51+
4152
/**
4253
* First-class equality check.
4354
*

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,32 @@ public boolean allEqual(final Equal<A> eq) {
13011301
return isEmpty() || tail().isEmpty() || eq.eq(head(), tail().head()) && tail().allEqual(eq);
13021302
}
13031303

1304+
public final boolean isPrefixOf(final Equal<A> eq, final List<A> xs) {
1305+
final Iterator<A> i = iterator();
1306+
final Iterator<A> j = xs.iterator();
1307+
1308+
while (i.hasNext() && j.hasNext()) {
1309+
if (!eq.eq(i.next(), j.next())) {
1310+
return false;
1311+
}
1312+
}
1313+
1314+
return !i.hasNext();
1315+
}
1316+
1317+
public final boolean isSuffixOf(final Equal<A> eq, final List<A> xs) {
1318+
final Iterator<A> i = iterator();
1319+
final Iterator<A> j = xs.drop(xs.length() - length()).iterator();
1320+
1321+
while (i.hasNext() && j.hasNext()) {
1322+
if (!eq.eq(i.next(), j.next())) {
1323+
return false;
1324+
}
1325+
}
1326+
1327+
return !i.hasNext();
1328+
}
1329+
13041330
/**
13051331
* First-class length.
13061332
*
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package fj.data.properties;
2+
3+
import fj.Equal;
4+
import fj.P;
5+
import fj.P2;
6+
import fj.data.List;
7+
import fj.runner.PropertyTestRunner;
8+
import fj.test.Gen;
9+
import fj.test.Property;
10+
import org.junit.runner.RunWith;
11+
12+
import static fj.test.Arbitrary.*;
13+
import static fj.test.Property.implies;
14+
import static fj.test.Property.prop;
15+
import static fj.test.Property.property;
16+
import static fj.Equal.intEqual;
17+
18+
/**
19+
* Created by Zheka Kozlov on 02.06.2015.
20+
*/
21+
@RunWith(PropertyTestRunner.class)
22+
public class ListProperties {
23+
24+
public Property isPrefixOf() {
25+
final Gen<P2<List<Integer>, Integer>> gen = arbList(arbInteger).gen.bind(list ->
26+
Gen.choose(0, list.length()).map(i -> P.p(list, i)));
27+
28+
return property(arbitrary(gen), pair -> prop(pair._1().take(pair._2()).isPrefixOf(intEqual, pair._1())));
29+
}
30+
31+
public Property isSuffixOf() {
32+
final Gen<P2<List<Integer>, Integer>> gen = arbList(arbInteger).gen.bind(list ->
33+
Gen.choose(0, list.length()).map(i -> P.p(list, i)));
34+
35+
return property(arbitrary(gen), pair -> prop(pair._1().drop(pair._2()).isSuffixOf(intEqual, pair._1())));
36+
}
37+
38+
public Property isPrefixOfShorter() {
39+
return property(arbList(arbInteger), arbList(arbInteger), (list1, list2) ->
40+
implies(list1.length() > list2.length(), () -> prop(!list1.isPrefixOf(intEqual, list2))));
41+
}
42+
43+
public Property isSuffixOfShorter() {
44+
return property(arbList(arbInteger), arbList(arbInteger), (list1, list2) ->
45+
implies(list1.length() > list2.length(), () -> prop(!list1.isSuffixOf(intEqual, list2))));
46+
}
47+
48+
public Property isPrefixOfDifferentHeads() {
49+
return property(arbList(arbInteger), arbList(arbInteger), arbInteger, arbInteger, (list1, list2, h1, h2) ->
50+
implies(intEqual.notEq(h1, h2), () -> prop(!list1.cons(h1).isPrefixOf(intEqual, list2.cons(h2)))));
51+
}
52+
53+
public Property isSuffixOfDifferentHeads() {
54+
return property(arbList(arbInteger), arbList(arbInteger), arbInteger, arbInteger, (list1, list2, h1, h2) ->
55+
implies(intEqual.notEq(h1, h2), () -> prop(!list1.snoc(h1).isSuffixOf(intEqual, list2.snoc(h2)))));
56+
}
57+
}

0 commit comments

Comments
 (0)