forked from palatable/lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEqualityAwareLens.java
More file actions
49 lines (40 loc) · 1.63 KB
/
EqualityAwareLens.java
File metadata and controls
49 lines (40 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package testsupport;
import com.jnape.palatable.lambda.functor.Applicative;
import com.jnape.palatable.lambda.functor.Functor;
import com.jnape.palatable.lambda.functor.builtin.Const;
import com.jnape.palatable.lambda.lens.Lens;
import java.util.Objects;
import java.util.function.Function;
public final class EqualityAwareLens<S, T, A, B> implements Lens<S, T, A, B> {
private final S s;
private final Lens<S, T, A, B> lens;
public EqualityAwareLens(S s, Lens<S, T, A, B> lens) {
this.s = s;
this.lens = lens;
}
@Override
public <F extends Functor, FT extends Functor<T, F>, FB extends Functor<B, F>> FT apply(
Function<? super A, ? extends FB> fn, S s) {
return lens.apply(fn, s);
}
@Override
public <U> EqualityAwareLens<S, U, A, B> fmap(Function<? super T, ? extends U> fn) {
return new EqualityAwareLens<>(s, lens.fmap(fn));
}
@Override
public <U> EqualityAwareLens<S, U, A, B> pure(U u) {
return new EqualityAwareLens<>(s, lens.pure(u));
}
@Override
public <U> EqualityAwareLens<S, U, A, B> zip(
Applicative<Function<? super T, ? extends U>, Lens<S, ?, A, B>> appFn) {
return new EqualityAwareLens<>(s, lens.zip(appFn));
}
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object other) {
return other instanceof Lens
&& Objects.equals(((Lens<S, T, A, B>) other).<Const<A, ?>, Const<A, T>, Const<A, B>>apply(Const::new, s).runConst(),
this.<Const<A, ?>, Const<A, T>, Const<A, B>>apply(Const::new, s).runConst());
}
}