Functortown: Applicative

This series is focused on one important typeclass – Applicative – and what applicative functors are. The series starts from a motivating example, a somewhat stripped-down version of something you might have encountered in “real code” and shows how the problem is solved with “fmap plus a little something extra.” We go on to explore applicative functors in depth, working up to showing how the Applicative class provides us with some useful tools for concurrency and parsing, among other tasks.

We also cover in this series two typeclasses that are closely related to Applicative: Alternative and Traversable. If you have ever seen a Haskeller announce that “the answer is always traverse” and wondered how that could possibly be even jokingly true, then this series is a great place to start.

Prerequisites:

  • knowledge of basic types and also kinds;
  • some comprehension of typeclasses and their relationship with types; and
  • facility with Haskell syntax;
  • a good understanding of Functor and fmap.

Lesson descriptions

  • Maybe there’s a function – First we introduce a you might find in real everyday code: sometimes the function is embedded within a Maybe context because it might not be there. This is a problem fmap alone can’t solve for us. We need an fmap for times when our function is stuck inside a constructor. But every problem is an opportunity to invent a solution, so we invent one. Then meet the typeclass that is designed for handling that situation in a general way, Applicative.

  • Applicatives are monoidal! – This lesson starts with a comparison of different Applicative idioms and compares liftA2 directly with (<*>). Then we look at product types as applicative functors and the connection between monoids and applicatives.

  • Sequencing effects – Here we take up with the rest of the Applicative class, namely the so-called sequencing operators, (*>) and (<*). This leads us to consider carefully what is meant by effects when we talk about applicatives and gives us a new perspective on the connection, discussed in the previous lesson, of the connection between monoids and applicatives. We also give an example of applicative parsing techniques.

  • List and ZipList – Now you’re in for a surprise: lists are applicative functors in two ways. In this lesson we talk about why – which does lead us to talking about monoids again, just when you thought we were done with that!

  • The Reader context – This lesson revolves around a fun example: recruiter emails! As we did in the Functor series, we look at the function instance of Applicative as well as a newtype wrapper for functions called Reader and see how the applicative can make our tedious email chores more exciting.


Not yet written:

  • Applicatives compose (coming next: will cover Reader and IO)
  • Concurrency
  • The Applicative laws and property tests
  • A monoid for applicative functors
  • Traversable: what and why
  • Connection to Applicative
  • Examples of traverse usage
  • The Traversable laws and property tests

We expect to release one new lesson in this series every other week, until it is completed. When this series is completed, we will begin posting the Monad series.

Join Type Classes for courses and projects to get you started and make you an expert in FP with Haskell.