Skip to content

Commit a2b4d38

Browse files
committed
added KleisliTest
1 parent 3251c6e commit a2b4d38

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

src/main/java/org/highj/control/arrow/kleisli/KleisliProfunctor.java

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

33
import org.derive4j.hkt.__;
4-
import org.derive4j.hkt.__2;
5-
import org.highj.Hkt;
64
import org.highj.control.arrow.Kleisli;
75
import org.highj.typeclass1.monad.Monad;
86
import org.highj.typeclass2.profunctor.Profunctor;
@@ -15,11 +13,6 @@ public interface KleisliProfunctor<M> extends Profunctor<__<Kleisli.µ, M>> {
1513

1614
Monad<M> getM();
1715

18-
@Override
19-
default <A, B, C, D> Kleisli<M, A, D> dimap(Function<A, B> f, Function<C, D> g, __<__<__<Kleisli.µ, M>, B>, C> p) {
20-
return rmap(g, lmap(f, p));
21-
}
22-
2316
@Override
2417
default <A, B, C> Kleisli<M, A, C> lmap(Function<A, B> f, __<__<__<Kleisli.µ, M>, B>, C> p) {
2518
return asKleisli(p).lmap(f);

src/main/java/org/highj/typeclass2/profunctor/Profunctor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
/**
1010
* A profunctor can be thought of as a bifunctor where the first argument is contravariant and the second argument is covariant.
1111
*
12+
* Minimal definition: dimap OR (lmap AND rmap)
13+
*
1214
* @param <P> the profunctor type class
1315
*/
1416
public interface Profunctor<P> {
1517

16-
<A, B, C, D> __2<P, A, D> dimap(Function<A, B> f, Function<C, D> g, __<__<P, B>, C> p);
18+
default <A, B, C, D> __2<P, A, D> dimap(Function<A, B> f, Function<C, D> g, __<__<P, B>, C> p) {
19+
return rmap(g, lmap(f, p));
20+
}
1721

1822
default <A, B, C> __2<P, A, C> lmap(Function<A, B> f, __<__<P, B>, C> p) {
1923
return dimap(f, Function.identity(), p);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.highj.control.arrow;
2+
3+
import org.derive4j.hkt.__;
4+
import org.derive4j.hkt.__2;
5+
import org.highj.Hkt;
6+
import org.highj.data.Maybe;
7+
import org.highj.data.eq.Eq;
8+
import org.highj.typeclass2.arrow.ArrowLaw;
9+
import org.junit.Test;
10+
11+
import java.util.function.Function;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.highj.data.Maybe.*;
15+
16+
public class KleisliTest {
17+
18+
@Test
19+
public void apply() {
20+
Function<String, __<Maybe.µ, Integer>> fn = s -> JustWhenTrue(s.contains("x"), () -> s.indexOf('x'));
21+
assertThat(new Kleisli<>(fn).apply("Max")).isEqualTo(Just(2));
22+
assertThat(new Kleisli<>(fn).apply("Moritz")).isEqualTo(Nothing());
23+
}
24+
25+
@Test
26+
public void lmap() {
27+
Function<String, __<Maybe.µ, Integer>> fn = s -> JustWhenTrue(s.contains("x"), () -> s.indexOf('x'));
28+
Kleisli<Maybe.µ, String, Integer> kleisli = new Kleisli<>(fn).lmap(s -> new StringBuilder(s).reverse().toString());
29+
assertThat(kleisli.apply("Max")).isEqualTo(Just(0));
30+
assertThat(kleisli.apply("Moritz")).isEqualTo(Nothing());
31+
}
32+
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+
47+
@Test
48+
public void profunctor() {
49+
}
50+
}

0 commit comments

Comments
 (0)