Skip to content

Commit 40853e4

Browse files
committed
introduced Function1 and some instances
1 parent 0eb4d14 commit 40853e4

File tree

9 files changed

+146
-4
lines changed

9 files changed

+146
-4
lines changed
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
11
package org.highj.data.eq;
22

33
import org.derive4j.hkt.__;
4+
import org.highj.data.eq.instances.Eq1Contravariant1;
5+
import org.highj.typeclass1.contravariant.Contravariant1;
6+
import org.highj.typeclass2.injective.Function1;
47

58
/**
69
* An equivalence relation for type constructors.
710
*
811
* @param <F> the type of the type constructor
912
*/
10-
public interface Eq1<F> extends __<Eq1.µ, F>{
13+
public interface Eq1<F> extends __<Eq1.µ, F> {
1114

12-
interface µ {}
15+
interface µ {
16+
}
1317

1418
/**
1519
* Derives an {@link Eq} instance for an instantiated type constructor.
16-
* @param eq an {@link Eq} instance of the element type
20+
*
21+
* @param eq an {@link Eq} instance of the element type
1722
* @param <T> the type of the element type
1823
* @return the instance
1924
*/
2025
<T> Eq<__<F, T>> eq1(Eq<? super T> eq);
26+
27+
default <E> Eq1<E> contramap(Function1<E, F> f1) {
28+
return new Eq1<E>() {
29+
@Override
30+
public <T> Eq<__<E, T>> eq1(Eq<? super T> eq) {
31+
Eq<__<F, T>> eq1 = Eq1.this.eq1(eq);
32+
return (one, two) -> eq1.eq(f1.apply1(one), f1.apply1(two));
33+
}
34+
};
35+
}
36+
37+
/**
38+
* The {@link Contravariant1} instance of {@link Eq1}
39+
*/
40+
Eq1Contravariant1 contravariant1 = new Eq1Contravariant1() {
41+
};
2142
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.highj.data.eq.instances;
2+
3+
import org.derive4j.hkt.__;
4+
import org.highj.Hkt;
5+
import org.highj.data.eq.Eq1;
6+
import org.highj.typeclass1.contravariant.Contravariant1;
7+
import org.highj.typeclass2.injective.Function1;
8+
9+
public interface Eq1Contravariant1 extends Contravariant1<Eq1.µ> {
10+
11+
@Override
12+
default <A, B> Eq1<A> contramap(Function1<A, B> fn, __<Eq1.µ, B> nestedB) {
13+
return Hkt.asEq1(nestedB).contramap(fn);
14+
}
15+
}

src/main/java/org/highj/data/ord/Ord1.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.derive4j.hkt.__;
44
import org.highj.data.eq.Eq;
55
import org.highj.data.eq.Eq1;
6+
import org.highj.data.ord.instances.Ord1Contravariant1;
7+
import org.highj.typeclass1.contravariant.Contravariant1;
8+
import org.highj.typeclass2.injective.Function1;
69

710
/**
811
* An sorting order for type constructors.
@@ -26,6 +29,22 @@ interface µ {}
2629
*/
2730
<A> Ord<__<F, A>> cmp(Ord<? super A> ord);
2831

32+
default <E> Ord1<E> contramap(Function1<E, F> f1) {
33+
return new Ord1<E>() {
34+
@Override
35+
public <T> Ord<__<E, T>> cmp(Ord<? super T> ord) {
36+
Ord<__<F, T>> ord1 = Ord1.this.cmp(ord);
37+
return (one, two) -> ord1.cmp(f1.apply1(one), f1.apply1(two));
38+
}
39+
};
40+
}
41+
42+
/**
43+
* The {@link Contravariant1} instance of {@link Ord1}
44+
*/
45+
Ord1Contravariant1 contravariant1 = new Ord1Contravariant1() {
46+
};
47+
2948
/**
3049
* Constructs an {@link Eq1} instance from this {@link Ord1}.
3150
*
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.highj.data.ord.instances;
2+
3+
import org.derive4j.hkt.__;
4+
import org.highj.Hkt;
5+
import org.highj.data.ord.Ord1;
6+
import org.highj.typeclass1.contravariant.Contravariant1;
7+
import org.highj.typeclass2.injective.Function1;
8+
9+
10+
public interface Ord1Contravariant1 extends Contravariant1<Ord1.µ> {
11+
12+
@Override
13+
default <A, B> Ord1<A> contramap(Function1<A, B> fn, __<Ord1.µ, B> nestedB) {
14+
return Hkt.asOrd1(nestedB).contramap(fn);
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.highj.typeclass1.contravariant;
2+
3+
4+
import org.derive4j.hkt.__;
5+
import org.highj.typeclass1.invariant.Invariant1;
6+
import org.highj.typeclass2.injective.Function1;
7+
8+
public interface Contravariant1<F> extends Invariant1<F> {
9+
10+
<A, B> __<F, A> contramap(Function1<A, B> fn, __<F, B> nestedB);
11+
12+
default <A, B> __<F, B> invmap(Function1<A, B> fn, Function1<B, A> nf, __<F, A> nestedA) {
13+
return contramap(nf, nestedA);
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.highj.typeclass1.functor;
2+
3+
import org.derive4j.hkt.__;
4+
import org.highj.typeclass1.invariant.Invariant1;
5+
import org.highj.typeclass2.injective.Function1;
6+
7+
public interface Functor1<F> extends Invariant1<F> {
8+
9+
<A, B> __<F, B> map(Function1<A, B> fn, __<F, A> nestedA);
10+
11+
default <A, B> __<F, B> invmap(Function1<A, B> fn, Function1<B,A> nf, __<F, A> nestedA) {
12+
return map(fn, nestedA);
13+
}
14+
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.highj.typeclass1.invariant;
2+
3+
import org.derive4j.hkt.__;
4+
import org.highj.typeclass2.injective.Function1;
5+
6+
public interface Invariant1<F> {
7+
8+
<A, B> __<F, B> invmap(Function1<A, B> fn, Function1<B, A> nf, __<F, A> nestedA);
9+
10+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.highj.typeclass2.injective;
2+
3+
import org.derive4j.hkt.__;
4+
5+
public interface Function1<F, G> {
6+
7+
<A> __<G, A> apply1(__<F, A> input);
8+
9+
default <H> Function1<F, H> andThen(Function1<G, H> that) {
10+
return new Function1<F, H>() {
11+
@Override
12+
public <A> __<H, A> apply1(__<F, A> input) {
13+
return that.apply1(Function1.this.apply1(input));
14+
}
15+
};
16+
}
17+
18+
static <M> Function1<M, M> identity() {
19+
return new Function1<M, M>() {
20+
@Override
21+
public <A> __<M, A> apply1(__<M, A> input) {
22+
return input;
23+
}
24+
};
25+
}
26+
}

src/main/java/org/highj/typeclass2/injective/Injective1.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@
1111
* @param <F> the source type constructor
1212
* @param <G> the target type constructor
1313
*/
14-
public interface Injective1<F, G> {
14+
public interface Injective1<F, G> extends Function1<F,G> {
1515

1616
<A> __<G, A> to(__<F, A> input);
1717

1818
default <A> Injective<__<F,A>, __<G,A>> to() {
1919
return this::to;
2020
}
2121

22+
@Override
23+
default <A> __<G, A> apply1(__<F, A> input) {
24+
return to(input);
25+
}
26+
2227
default <H> Injective1<F, H> andThen(Injective1<G, H> that) {
2328
return new Injective1<F, H>() {
2429
@Override

0 commit comments

Comments
 (0)