Thoughts on improving optics package
See original GitHub issue@jbgi When I updated the JavaDoc of the optics package, I had some ideas and questions:
- The composition methods look very repetitive. Could mixin inheritance help here? Of course a class
Foo<A>
can’t implement__<Foo.µ,A>
and a__<SuperFoo.µ,A>
at the same time, so we would need to split off interfaces from the current classes. - It would be cool to have some predefined lenses, e.g. for tuples.
- It seems Lens uses the conventional getter-setter implementation, not the van Laarhoven style lenses, which seem to be quite hip at the moment (and for a good reason, I suppose).
Implementing them is not difficult (I didn’t want to go polymorphic, which is certainly possible):
import org.derive4j.hkt.__;
import org.highj.data.structural.Const;
import org.highj.data.tuple.T1;
import org.highj.function.Functions;
import org.highj.typeclass1.functor.Functor;
import java.util.function.Function;
public interface VanLaarhovenLens<S,A> {
<F> Function<S, __<F, S>> apply(Functor<F> functor, Function<A, __<F, A>> targetFunction);
default Function<S, S> modify(Function<A,A> function) {
Function<A, __<T1.µ, A>> targetFunction = a -> T1.of(function.apply(a));
return s -> T1.narrow(apply(T1.monad, targetFunction).apply(s)).get();
}
default Function<S, A> view() {
Function<A, __<__<Const.µ, A>, A>> function = Const::new;
return s -> Const.narrow(apply(Const.functor(), function).apply(s)).get();
}
default Function<S,S> set(A value) {
return modify(Functions.constant(value));
}
}
An instance:
public static <A,B> VanLaarhovenLens<T2<A,B>, A> first() {
return new VanLaarhovenLens<T2<A, B>, A>() {
@Override
public <F> Function<T2<A, B>, __<F, T2<A, B>>> apply(Functor<F> functor, Function<A, __<F, A>> targetFunction) {
return pair -> functor.map(a -> T2.of(a,pair._2()), targetFunction.apply(pair._1()));
}
};
}
(code stolen from here: http://blog.jakubarnold.cz/2014/07/14/lens-tutorial-introduction-part-1.html )
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:12 (3 by maintainers)
Top Results From Across the Web
The Promise of Co-Packaged Optics - YouTube
The Promise of Co-Packaged Optics : Paving the Way for Improved Power Efficiency, Size, and Cost. 4,185 views 4.1K views. Oct 29, 2020....
Read more >Co-Packaged Optical-IO, the Promise and the Challenges
2.7K views 2 years ago SILICON VALLEY ... current status of the silicon photonics technology, as it applies to the in- package optical...
Read more >Co-Packaged Optics and an Open Ecosystem - Cisco Blogs
Bringing optics and switch silicon together in the same package creates a synergy between once disjoint and independent technologies thereby ...
Read more >Contribution of oversampling and improved optical resolution ...
Conclusions : An increase in optical resolution may be effective in improving OCTA image quality if the sampling resolution is adequate, which may...
Read more >Toward higher-radix switches with co-packaged optics for ...
Co-packaged optics come with two key advantages: (a) they can substantially increase the package escape BW by offering an extra dimension for wiring ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I see your point. Maybe we should instead pull the package one level up to
org.highj.optic
, it is kind of hard to spot insidedata
. Further, I think lenses deserve a Wiki page with a short introduction. Of course it makes no sense to repeat the monocle documentation, we can reference to it and just point out relevant differences (if there are any).@DanielGronau I think optics are so useful in so many use cases, in modern FP, that I think they should be easily accessible in base/prelude. They are currently underused and I fear that a separate optics sub-project will lower adoption rate.