concept functor in category javascript

appears as: functor, functor, functors, A functor
The Joy of JavaScript

This is an excerpt from Manning's book The Joy of JavaScript.

Furthermore, notice that this version of countBlocksInFile favors a more fluent design guided by sequential applications of map methods over new Validation objects. The power of these ADTs is encoded in its own implementation of map. When map abides by a certain contract, an implementing type goes by the name of functor. You just saw how to use a functor in the short snippet above. Chapter 5 goes deeper into this and also teaches you another important FP pattern called a monad.

A book that focuses on the joy of programming with FP isn’t complete without a proper dose of functors. A functor is something (e.g. an object) that can be mapped over or that implements the map interface properly. For example, arrays in JavaScript are very close to being functors. And as you learned in section 5.3, compose makes functions functors too. So, you’ve used them quite a bit already without realizing it!

So, a functor has essentially two components: a map interface, and these simple rules. As you learned in chapter 3, the best way to apply reusable interfaces to any type is via a mixin. So, Let’s refactor Id to take advantage of this instead of linking from Array:

class Id {
   #val 
   constructor (value) {      
      this.#val = value
   }
 
   static of(value) { //#A
      return new Id(value)
   }
      
   get() { //#B
      return this.#val
   }
 
   equals (otherValidation) {  //#C
      return this.#val === otherValidation.value
   }
 }

Functor is a functional mixin with a map declaration, as shown in listing 5.3 below. map applies the given function f to the wrapped value, and stores the result back into the same container type:

Functional Programming in JavaScript

This is an excerpt from Manning's book Functional Programming in JavaScript.

5.2.2. Functors explained

In essence, a functor is nothing more than a data structure that you can map functions over with the purpose of lifting values into a wrapper, modifying them, and then putting them back into a wrapper. It’s a design pattern that defines semantics for how fmap should work. Here’s the general definition of fmap:

fmap :: (A -> B) -> Wrapper(A) -> Wrapper(B)                 #1

The function fmap takes a function (from A -> B) and a functor (wrapped context) Wrapper(A) and returns a new functor Wrapper(B) containing the result of applying said function to the value and closing it once more. Figure 5.3 shows a quick example that uses the increment function as a mapping function from A -> B (except in this case, A and B are the same types).

Figure 5.3. A value of 1 is contained within Wrapper. The functor is called with the wrapper and the increment function, which transforms the value internally and closes it back into a container.

Notice that because fmap basically returns a new copy of the container at each invocation, much as lenses (chapter 2) work, it can be considered immutable. In figure 5.3, mapping the increment over Wrapper(1) returns a completely new object, Wrapper(2). Let’s go over a simple example before you begin applying functors to solve more-practical problems. Consider a simple 2 + 3 = 5 addition using functors. You can curry an add function to create a plus3 function: