-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Mastering Clojure
By :
Next, let's take a look at functors. A functor is essentially a value in a container or a computational context. The fmap function must be implemented by a functor. This function applies a supplied function to the value contained in a functor. In object-oriented terminology, a functor can be thought of as a generic type with a single abstract method fmap. In a way, reference types, such as refs and atoms, can be thought of as functors that save results, as a reference type applies a function to its contained value in order to obtain the new value that should be stored in it.
The following examples can be found in src/m_clj/c6/ functors.clj of the book's source code.
The fmap function from the cats.core namespace takes two arguments: a function and a functor. A functor itself defines what happens when an instance of the functor is passed to the fmap function. The cats library extends vectors as functors. When a vector is passed to the fmap function along with a...