Science Knowings: JavaScript Course For Social Media


Functors and Applicatives

In the previous session, we explored functors, which allow us to map functions over data structures. Now, let's take a step further into the realm of applicatives.

What are Applicatives?

Applicatives are a generalization of functors that give us more power in working with nested structures.

Applicatives Apply Applicatives

The key characteristic of an applicative is the ap method. ap takes an applicative containing a function and applies it to another applicative containing data.

Applicatives are Monoids in the Category of Endofunctors

Applicatives satisfy monoid laws in the category of endofunctors. This means that they have an identity element and a composition operation.

identity.ap(fa).equals(fa)  // left identity
fa.ap(identity).equals(fa)  // right identity
fa.ap(g.ap(f)).equals(g.ap(fa.ap(f)))  // associativity

Free Applicative Functors

Free applicative functors allow us to create applicatives for custom data structures.

Using Applicatives in JavaScript

JavaScript has many libraries that provide applicative functionality, such as Ramda, fp-ts, and Sanctuary.

Creating your own Applicatives

You can also create your own applicatives by implementing the ap method.

class MyList {  constructor(list) {    this.list = list;  }  ap(other) {    return new MyList( => f.ap(other).list));  }}

Applicative Laws

Applicatives must adhere to the following laws:

  • Identity: fa.ap(pure(identity)).equals(fa)
  • Homomorphism: pure(f).ap(pure(x)).equals(pure(f(x)))
  • Interchange: pure(f).ap(fa).equals(fa.ap(pure(f.ap)))
  • Identity Applicative

    The identity applicative wraps a value in its own context, providing the simplest form of an applicative.

    Compose Applicatives

    Applicatives can be composed to create more complex applicatives.

    composeA(fa, fb) = fa.ap(fb)

    Product Applicative

    The product applicative combines two applicatives into a new applicative that contains a tuple of their values.

    Either Applicative

    The either applicative represents either a successful value or an error. It provides methods for handling both cases.

    Validation Applicative

    The validation applicative is similar to the either applicative but allows for multiple errors.

    IO Applicative

    The IO applicative represents an effectful computation. It provides methods for performing I/O operations.

    Reader Applicative

    The reader applicative represents a dependency on an environment. It provides methods for accessing the environment.

    State Applicative

    The state applicative represents a computation that modifies state. It provides methods for getting and setting state.

    Writer Applicative

    The writer applicative represents a computation that produces a log. It provides methods for getting and appending to the log.

    Next Topic: Monads in JavaScript

    In the next session, we'll explore monads in JavaScript, a more powerful generalization of applicatives. Follow us to learn more!