This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-06-03
Channels
- # aws (12)
- # beginners (12)
- # biff (10)
- # calva (1)
- # cider (10)
- # cljfx (1)
- # clojure (2)
- # clojure-conj (1)
- # clojure-europe (25)
- # clojure-madison (1)
- # clojure-nl (1)
- # clojure-norway (12)
- # clojure-sweden (4)
- # clojure-uk (6)
- # datomic (11)
- # dev-tooling (3)
- # emacs (5)
- # gratitude (1)
- # introduce-yourself (7)
- # java (3)
- # jobs (1)
- # london-clojurians (2)
- # lsp (23)
- # off-topic (4)
- # practicalli (9)
- # quil (6)
- # re-frame (3)
- # reagent (4)
- # remote-jobs (1)
- # ring (1)
- # shadow-cljs (18)
- # squint (67)
- # tools-deps (5)
- # xtdb (4)
- # yamlscript (12)
Trying to implement category theory things in Java (why not), I get tripped up on java syntax and generics Thought I'd start with interfaces for Functor, Applicative and Monad, then go from there Starting off, Functor:
public interface Functor<A> {
// which is the correct signature for map?
<B extends Functor<B>> Functor<B>map(Function<A,B> f);
<B> Functor<B>map(Function<A,B> f);
}
The comment says it. In theory, we can map
over every functor, but do I need to express somehow that the receiver and return value can be anything that extends Functor?
It gets more complicated when Applicative provides a default implementation for functor:
@Override
default <B> Functor<B> map(Function<A, B> f) {
return this.apply(pure(f));
}
Is this right? Does map over Applicative return Functor? Can I express somehow that it returns anything which extends Functor<B>?
I have the same problem with Monad but figuring out the syntax / correct typing for applicative will probably carry overI'm not really good at this. But take a look at https://typelevel.org/cats/typeclasses/applicative.html it may cast some light on what is happening there with map
and why you need ap
(or product
as an alternative reasoning) as a supportive construct https://typelevel.org/cats/typeclasses/applicative.html#what-is-ap So the way I see it is that Applicative is a Functor where you teach your map an additional superpower to be able to handle things like Optional types. I'm not sure if Java type system is capable to express this.