Implementation for Either

Now that we have familiarized ourselves with Monoid and had a look at how it is used in the context of simple types such as integer, let's take a look at our previous example, the example of Either with a generic type of LeftEither[L, A]. How can we define the Applicative instance for a generic Left type? Previously, we saw that the body of the ap function for a generic Left type is not very different from the body of this function for the list. The only problem was that we didn't know how to combine two arbitrary types.

This combination sounds like exactly the task for Monoid. So, if we bring the implicit dependency on Monoid into scope, we can define the ap and Applicative type class for the Either type as follows: ...

Get Mastering Functional Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.