Skip to content

Commit d8191ea

Browse files
committed
Contravariant arrives
1 parent 57214ff commit d8191ea

5 files changed

Lines changed: 60 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1111
- `And`, `Or`, and `Xor` all gain `BiPredicate<Boolean, Boolean>` properties
1212
- `LambdaOptional` and `LambdaIterable`, adapters for `Optional` and `Iterable` that support lambda types
1313
- `Lambda`, providing static factory methods for `LambdaOptional` and `LambdaIterable`
14+
- `Contravariant`, an interface representing functors that map contravariantly over their parameters
15+
- `Profunctor` extends `Contravariant`
1416

1517
### Removed
1618
- `Fn1#then(Function<? super B, ? extends C>)`, deprecated in previous release

‎src/main/java/com/jnape/palatable/lambda/functions/Fn1.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ default <Z, C> Fn1<Z, C> diMap(Function<? super Z, ? extends A> lFn, Function<?
118118
return lFn.andThen(this).andThen(rFn)::apply;
119119
}
120120

121+
@Override
122+
default <Z> Fn1<Z, B> contraMap(Function<? super Z, ? extends A> fn) {
123+
return (Fn1<Z, B>) Profunctor.super.<Z>contraMap(fn);
124+
}
125+
121126
/**
122127
* Override of {@link Function#compose(Function)}, returning an instance of <code>Fn1</code> for compatibility.
123128
* Right-to-left composition.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.jnape.palatable.lambda.functor;
2+
3+
import java.util.function.Function;
4+
5+
/**
6+
* The contravariant functor (or "co-functor"); that is, a functor that maps contravariantly (<code>A &lt;- B</code>)
7+
* over its parameter.
8+
* Contravariant functors are not necessarily {@link Functor}s.
9+
* <p>
10+
* For more information, read about <a href="https://hackage.haskell.org/package/contravariant-1.4/docs/Data-Functor-Contravariant.html"
11+
* target="_top">Contravariant Functors</a>.
12+
*
13+
* @param <A> the type of the parameter
14+
* @param <C> the unification parameter
15+
* @see Profunctor
16+
*/
17+
public interface Contravariant<A, C extends Contravariant> {
18+
19+
/**
20+
* Contravariantly map <code>A &lt;- B</code>.
21+
*
22+
* @param fn the mapping function
23+
* @param <B> the new parameter type
24+
* @return the mapped Contravariant functor instance
25+
*/
26+
<B> Contravariant<B, C> contraMap(Function<? super B, ? extends A> fn);
27+
}

‎src/main/java/com/jnape/palatable/lambda/functor/Profunctor.java‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
99

1010
/**
11-
* A dually-parametric functor that maps contravariantly over the left parameter and covariantly over the
12-
* right.
11+
* A dually-parametric functor that maps contravariantly over the left parameter and covariantly over the right.
1312
* <p>
1413
* For more information, read about <a href="https://en.wikipedia.org/wiki/Profunctor" target="_top">Profunctors</a>.
1514
*
@@ -18,11 +17,24 @@
1817
* @param <PF> The unification parameter
1918
* @see Functor
2019
* @see Bifunctor
20+
* @see Contravariant
2121
* @see Fn1
2222
* @see Lens
2323
*/
2424
@FunctionalInterface
25-
public interface Profunctor<A, B, PF extends Profunctor> {
25+
public interface Profunctor<A, B, PF extends Profunctor> extends Contravariant<A, Profunctor<?, B, PF>> {
26+
27+
/**
28+
* Dually map contravariantly over the left parameter and covariantly over the right parameter. This is isomorphic
29+
* to <code>diMapL(lFn).diMapR(rFn)</code>.¬
30+
*
31+
* @param <Z> the new left parameter type
32+
* @param <C> the new right parameter type
33+
* @param lFn the left parameter mapping function
34+
* @param rFn the right parameter mapping function
35+
* @return a profunctor over Z (the new left parameter type) and C (the new right parameter type)
36+
*/
37+
<Z, C> Profunctor<Z, C, PF> diMap(Function<? super Z, ? extends A> lFn, Function<? super B, ? extends C> rFn);
2638

2739
/**
2840
* Contravariantly map over the left parameter.
@@ -48,14 +60,10 @@ default <C> Profunctor<A, C, PF> diMapR(Function<? super B, ? extends C> fn) {
4860
}
4961

5062
/**
51-
* Dually map contravariantly over the left parameter and covariantly over the right parameter. This is isomorphic
52-
* to <code>diMapL(lFn).diMapR(rFn)</code>.
53-
*
54-
* @param <Z> the new left parameter type
55-
* @param <C> the new right parameter type
56-
* @param lFn the left parameter mapping function
57-
* @param rFn the right parameter mapping function
58-
* @return a profunctor over Z (the new left parameter type) and C (the new right parameter type)
63+
* @inheritDoc
5964
*/
60-
<Z, C> Profunctor<Z, C, PF> diMap(Function<? super Z, ? extends A> lFn, Function<? super B, ? extends C> rFn);
65+
@Override
66+
default <Z> Profunctor<Z, B, PF> contraMap(Function<? super Z, ? extends A> fn) {
67+
return diMapL(fn);
68+
}
6169
}

‎src/main/java/com/jnape/palatable/lambda/lens/Lens.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.jnape.palatable.lambda.functor.Applicative;
55
import com.jnape.palatable.lambda.functor.Functor;
66
import com.jnape.palatable.lambda.functor.Profunctor;
7+
import com.jnape.palatable.lambda.monad.Monad;
78

89
import java.util.function.BiFunction;
910
import java.util.function.Function;
@@ -195,6 +196,11 @@ default <R, U> Lens<R, U, A, B> diMap(Function<? super R, ? extends S> lFn, Func
195196
return this.<R>mapS(lFn).mapT(rFn);
196197
}
197198

199+
@Override
200+
default <R> Lens<R, T, A, B> contraMap(Function<? super R, ? extends S> fn) {
201+
return (Lens<R, T, A, B>) Profunctor.super.<R>contraMap(fn);
202+
}
203+
198204
/**
199205
* Contravariantly map <code>S</code> to <code>R</code>, yielding a new lens.
200206
*

0 commit comments

Comments
 (0)