Applicative Functors
Like Functors, Applicative Functors are a sort of container and defines two operations:
(defprotocol Applicative (pure [av v]) (fapply [ag av]))
The pure
function is a generic way to put a value inside an Applicative Functor. So far, we have been using the option
helper function for this purpose. We will be using it a little later.
The fapply
function will unwrap the function contained in the Applicative ag
and apply it to the value contained in the applicative av
.
The purpose of both the functions will become clear with an example, but first, we need to promote our Option
Functor into an Applicative Functor:
(extend-protocol fkp/Applicative Some (pure [_ v] (Some. v)) (fapply [ag av] (if-let [v (:v av)] (Some. ((:v ag) v)) (None.))) None (pure [_ v] (Some. v)) (fapply [ag av] (None.)))
The implementation of pure
is the simplest. All it does is wrap the value v
into an instance of Some
. Equally simple is the implementation of...