Skip to content

Commit 36d7633

Browse files
committed
replaced ArrowLaw by ArrowContract
Took 34 minutes
1 parent 0abbfda commit 36d7633

File tree

6 files changed

+112
-88
lines changed

6 files changed

+112
-88
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.highj.control.arrow;
2+
3+
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
4+
import org.derive4j.hkt.__;
5+
import org.derive4j.hkt.__2;
6+
import org.highj.Hkt;
7+
import org.highj.data.Maybe;
8+
import org.highj.typeclass2.arrow.Arrow;
9+
import org.highj.typeclass2.arrow.ArrowContract;
10+
import org.junit.runner.RunWith;
11+
12+
@RunWith(JUnitQuickcheck.class)
13+
public class KleisliArrowContract implements ArrowContract<__<Kleisli.µ, Maybe.µ>> {
14+
15+
@Override
16+
public Arrow<__<Kleisli.µ, Maybe.µ>> subject() {
17+
return Kleisli.arrow(Maybe.monad);
18+
}
19+
20+
@Override
21+
public <B, C> boolean areEqual(__2<__<Kleisli.µ, Maybe.µ>, B, C> one, __2<__<Kleisli.µ, Maybe.µ>, B, C> two, B b) {
22+
Kleisli<Maybe.µ, B, C> kleisliOne = Hkt.asKleisli(one);
23+
Kleisli<Maybe.µ, B, C> kleisliTwo = Hkt.asKleisli(two);
24+
Maybe<C> maybeOne = Hkt.asMaybe(kleisliOne.apply(b));
25+
Maybe<C> maybeTwo = Hkt.asMaybe(kleisliTwo.apply(b));
26+
return maybeOne.equals(maybeTwo);
27+
}
28+
}

src/test/java/org/highj/control/arrow/KleisliTest.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package org.highj.control.arrow;
22

33
import org.derive4j.hkt.__;
4-
import org.derive4j.hkt.__2;
5-
import org.highj.Hkt;
64
import org.highj.data.Maybe;
7-
import org.highj.data.eq.Eq;
8-
import org.highj.typeclass2.arrow.ArrowLaw;
95
import org.junit.jupiter.api.Test;
106

117
import java.util.function.Function;
@@ -30,20 +26,6 @@ public void lmap() {
3026
assertThat(kleisli.apply("Moritz")).isEqualTo(Nothing());
3127
}
3228

33-
@Test
34-
public void arrowLaw() {
35-
new ArrowLaw<__<Kleisli.µ, Maybe.µ>>(Kleisli.arrow(Maybe.monad)) {
36-
@Override
37-
public <B, C> boolean areEqual(__2<__<Kleisli.µ, Maybe.µ>, B, C> one, __2<__<Kleisli.µ, Maybe.µ>, B, C> two, B b, Eq<C> eq) {
38-
Kleisli<Maybe.µ, B, C> kleisliOne = Hkt.asKleisli(one);
39-
Kleisli<Maybe.µ, B, C> kleisliTwo = Hkt.asKleisli(two);
40-
Maybe<C> maybeOne = Hkt.asMaybe(kleisliOne.apply(b));
41-
Maybe<C> maybeTwo = Hkt.asMaybe(kleisliTwo.apply(b));
42-
return Maybe.eq(eq).eq(maybeOne, maybeTwo);
43-
}
44-
}.test();
45-
}
46-
4729
@Test
4830
public void profunctor() {
4931
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.highj.data.tuple;
2+
3+
import com.pholser.junit.quickcheck.generator.ComponentizedGenerator;
4+
import com.pholser.junit.quickcheck.generator.GenerationStatus;
5+
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
6+
7+
@SuppressWarnings("rawtypes")
8+
public class T2Gen extends ComponentizedGenerator<T2> {
9+
10+
public T2Gen() {
11+
super(T2.class);
12+
}
13+
14+
@Override
15+
public T2<?, ?> generate(SourceOfRandomness random, GenerationStatus status) {
16+
return T2.of(
17+
componentGenerators().get(0).generate(random, status),
18+
componentGenerators().get(1).generate(random, status)
19+
);
20+
}
21+
22+
@Override
23+
public int numberOfNeededComponents() {
24+
return 2;
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.highj.function;
2+
3+
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
4+
import org.derive4j.hkt.__2;
5+
import org.highj.Hkt;
6+
import org.highj.typeclass2.arrow.Arrow;
7+
import org.highj.typeclass2.arrow.ArrowContract;
8+
import org.junit.runner.RunWith;
9+
10+
@RunWith(JUnitQuickcheck.class)
11+
public class F1ArrowContract implements ArrowContract<F1.µ> {
12+
13+
@Override
14+
public Arrow<F1.µ> subject() {
15+
return F1.arrow;
16+
}
17+
18+
@Override
19+
public <B, C> boolean areEqual(__2<F1.µ, B, C> one, __2<F1.µ, B, C> two, B b) {
20+
F1<B, C> f1One = Hkt.asF1(one);
21+
F1<B, C> f1Two = Hkt.asF1(two);
22+
return f1One.apply(b).equals(f1Two.apply(b));
23+
}
24+
}

src/test/java/org/highj/function/F1Test.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package org.highj.function;
22

3-
import org.derive4j.hkt.__2;
4-
import org.highj.Hkt;
53
import org.highj.data.Maybe;
6-
import org.highj.data.eq.Eq;
74
import org.highj.data.tuple.T2;
85
import org.highj.data.tuple.T3;
96
import org.highj.data.tuple.T4;
107
import org.highj.function.f1.F1Monad;
118
import org.highj.typeclass0.group.Monoid;
12-
import org.highj.typeclass2.arrow.ArrowLaw;
139
import org.junit.jupiter.api.Test;
1410

1511
import java.sql.Timestamp;
@@ -201,18 +197,6 @@ public void arrow() {
201197
assertThat(dot.apply("foobar")).isFalse();
202198
}
203199

204-
@Test
205-
public void arrowLaw() {
206-
new ArrowLaw<F1.µ>(F1.arrow) {
207-
@Override
208-
public <B, C> boolean areEqual(__2<F1.µ, B, C> one, __2<F1.µ, B, C> two, B b, Eq<C> eq) {
209-
F1<B, C> f1One = Hkt.asF1(one);
210-
F1<B, C> f1Two = Hkt.asF1(two);
211-
return eq.eq(f1One.apply(b), f1Two.apply(b));
212-
}
213-
}.test();
214-
}
215-
216200
@Test
217201
public void profunctor() {
218202
F1<String, Integer> f1 = String::length;

src/test/java/org/highj/typeclass2/arrow/ArrowLaw.java renamed to src/test/java/org/highj/typeclass2/arrow/ArrowContract.java

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,81 @@
11
package org.highj.typeclass2.arrow;
22

3+
import com.pholser.junit.quickcheck.Property;
34
import org.derive4j.hkt.__2;
45
import org.highj.Hkt;
5-
import org.highj.data.eq.Eq;
66
import org.highj.data.tuple.T2;
77
import org.highj.function.F1;
8-
import org.highj.util.Gen;
9-
import org.highj.util.Law;
108

119
import java.util.function.Function;
1210

1311
import static org.assertj.core.api.Assertions.assertThat;
1412

15-
public abstract class ArrowLaw<A> implements Law {
13+
public interface ArrowContract<A> {
1614

17-
private final Arrow<A> arrow;
15+
Arrow<A> subject();
1816

19-
public ArrowLaw(Arrow<A> arrow) {
20-
this.arrow = arrow;
21-
}
22-
23-
public abstract <B, C> boolean areEqual(__2<A, B, C> one, __2<A, B, C> two, B b, Eq<C> eq);
24-
25-
@Override
26-
public void test() {
27-
arrowIdentity();
28-
composition();
29-
arrFirst();
30-
firstComposition();
31-
t2_1();
32-
split();
33-
assoc();
34-
}
17+
<B, C> boolean areEqual(__2<A, B, C> one, __2<A, B, C> two, B b);
3518

3619
// arr id = id
37-
public void arrowIdentity() {
20+
@Property
21+
default void arrowIdentity(String s) {
22+
Arrow<A> arrow = subject();
3823
__2<A, String, String> arrowOfId = arrow.arr(Function.identity());
3924
__2<A, String, String> arrowIdentity = arrow.identity();
40-
for (String s : Gen.stringGen.get(20)) {
41-
assertThat(areEqual(arrowOfId, arrowIdentity, s, Eq.fromEquals())).isTrue();
42-
}
25+
assertThat(areEqual(arrowOfId, arrowIdentity, s)).isTrue();
4326
}
4427

28+
4529
// arr (f >>> g) = arr f >>> arr g
46-
public void composition() {
30+
@Property
31+
default void composition(String s) {
32+
Arrow<A> arrow = subject();
4733
Function<String, Integer> f1 = String::length;
4834
Function<Integer, Integer> f2 = x -> x * x;
4935
__2<A, String, Integer> arrOfComposed = arrow.arr(f1.andThen(f2));
5036
__2<A, String, Integer> composedArr = arrow.then(arrow.arr(f1), arrow.arr(f2));
51-
for (String s : Gen.stringGen.get(20)) {
52-
assertThat(areEqual(arrOfComposed, composedArr, s, Eq.fromEquals())).isTrue();
53-
}
37+
assertThat(areEqual(arrOfComposed, composedArr, s)).isTrue();
5438
}
5539

5640
// first (arr f) = arr (first f)
57-
public void arrFirst() {
41+
@Property
42+
default void arrFirst(T2<String, Boolean> t2) {
43+
Arrow<A> arrow = subject();
5844
F1<String, Integer> fn = String::length;
5945
__2<A, T2<String, Boolean>, T2<Integer, Boolean>> firstArr = arrow.first(arrow.arr(fn));
6046
__2<A, T2<String, Boolean>, T2<Integer, Boolean>> arrFirst = arrow.arr(F1.arrow.first(fn));
61-
for (T2<String, Boolean> t2 : Gen.zip(Gen.stringGen, Gen.boolGen).get(20)) {
62-
assertThat(areEqual(firstArr, arrFirst, t2,
63-
T2.eq(Eq.fromEquals(), Eq.fromEquals()))).isTrue();
64-
}
47+
assertThat(areEqual(firstArr, arrFirst, t2)).isTrue();
6548
}
6649

6750
// first (f >>> g) = first f >>> first g
68-
public void firstComposition() {
51+
@Property
52+
default void firstComposition(T2<String, Boolean> t2) {
53+
Arrow<A> arrow = subject();
6954
__2<A, String, Integer> f = arrow.arr(String::length);
7055
__2<A, Integer, Integer> g = arrow.arr(x -> x * x);
7156
__2<A, T2<String, Boolean>, T2<Integer, Boolean>> firstOfComposed =
7257
arrow.first(arrow.then(f, g));
7358
__2<A, T2<String, Boolean>, T2<Integer, Boolean>> composedFirst =
7459
arrow.then(arrow.first(f), arrow.first(g));
75-
for (T2<String, Boolean> t2 : Gen.zip(Gen.stringGen, Gen.boolGen).get(20)) {
76-
assertThat(areEqual(firstOfComposed, composedFirst, t2,
77-
T2.eq(Eq.fromEquals(), Eq.fromEquals()))).isTrue();
78-
}
60+
assertThat(areEqual(firstOfComposed, composedFirst, t2)).isTrue();
7961
}
8062

8163
// first f >>> arr fst = arr fst >>> f
82-
public void t2_1() {
64+
@Property
65+
default void t2_1(T2<String, Boolean> t2) {
66+
Arrow<A> arrow = subject();
8367
__2<A, String, Integer> f = arrow.arr(String::length);
8468

8569
__2<A, T2<String, Boolean>, Integer> firstThenT2 = arrow.then(arrow.first(f), arrow.arr(T2::_1));
8670
__2<A, T2<String, Boolean>, Integer> T2ThenF = arrow.then(arrow.arr(T2::_1), f);
8771

88-
for (T2<String, Boolean> t2 : Gen.zip(Gen.stringGen, Gen.boolGen).get(20)) {
89-
assertThat(areEqual(firstThenT2, T2ThenF, t2, Eq.fromEquals())).isTrue();
90-
}
72+
assertThat(areEqual(firstThenT2, T2ThenF, t2)).isTrue();
9173
}
9274

9375
// first f >>> arr (id *** g) = arr (id *** g) >>> first f
94-
public void split() {
76+
@Property
77+
default void split(T2<String, Long> t2) {
78+
Arrow<A> arrow = subject();
9579
__2<A, String, Integer> f = arrow.arr(String::length);
9680
__2<F1.µ, Long, Long> g = (F1<Long, Long>) x -> x * x;
9781

@@ -101,14 +85,13 @@ public void split() {
10185
__2<A, T2<String, Long>, T2<String, Long>> g2 = arrow.arr(Hkt.asF1(F1.arrow.split(F1.arrow.identity(), g)));
10286
__2<A, T2<String, Long>, T2<Integer, Long>> splittedFirst = arrow.then(g2, arrow.first(f));
10387

104-
for (T2<String, Long> t2 : Gen.zip(Gen.stringGen, Gen.longGen).get(20)) {
105-
assertThat(areEqual(firstSplitted, splittedFirst, t2,
106-
T2.eq(Eq.fromEquals(), Eq.fromEquals()))).isTrue();
107-
}
88+
assertThat(areEqual(firstSplitted, splittedFirst, t2)).isTrue();
10889
}
10990

11091
// first (first f) >>> arr assoc = arr assoc >>> first f -- where assoc ((a,b),c) = (a,(b,c))
111-
public void assoc() {
92+
@Property
93+
default void assoc(T2<T2<String, Boolean>, Long> nestedT2) {
94+
Arrow<A> arrow = subject();
11295
__2<A, String, Integer> f = arrow.arr(String::length);
11396

11497
__2<A, T2<T2<String, Boolean>, Long>, T2<T2<Integer, Boolean>, Long>> firstFirst =
@@ -125,10 +108,7 @@ public void assoc() {
125108
__2<A, T2<T2<String, Boolean>, Long>, T2<Integer, T2<Boolean, Long>>> assocFirst =
126109
arrow.then(arrAssoc2, first);
127110

128-
for (T2<T2<String, Boolean>, Long> t2 : Gen.zip(Gen.zip(Gen.stringGen, Gen.boolGen), Gen.longGen).get(20)) {
129-
assertThat(areEqual(firstAssoc, assocFirst, t2,
130-
T2.eq(Eq.fromEquals(), T2.eq(Eq.fromEquals(), Eq.fromEquals())))).isTrue();
131-
}
111+
assertThat(areEqual(firstAssoc, assocFirst, nestedT2)).isTrue();
132112
}
133113

134-
}
114+
}

0 commit comments

Comments
 (0)