11 Higher Kinded Types and Beyond

 

In this chapter

  • Applying map() to various other types
  • Encapsulating error propagation
  • Monads and their applications
  • Resources for further study

Throughout the book we looked at various versions of a very common algorithm, map(), and in chapter 10 we saw how iterators provide an abstraction that allows us to reuse it across various data structures. In this chapter we’ll see how we can extend this beyond iterators and provide an even more general version. This powerful algorithm allows us to mix-and-match generic types and functions and can help by providing a uniform way to handle errors.

After we go over a few examples, we’ll provide a definition for this broadly applicable family of functions, known as functors. We’ll also explain what higher kinded types are and how they help us define such generic functions. We’ll look at the limitations we run into with languages which lack support for higher kinded types.

Next, we’ll look at monads. The term shows up in multiple places, and while it might sound intimidating, the concept is straight-forward. We’ll explain what a monad is, and go over multiple applications, from better error propagation to asynchronous code and sequence flattening.

11.1   An Even More General Map

11.1.1   Processing Results or Propagating Errors

11.1.2   Mix-and-match Function Application

11.1.3   Functors and Higher Kinded Types

11.1.4   Functors for Functions

11.1.5   Exercise

11.2   Monads

11.2.1   Result or Error

11.2.2   Difference Between map() and bind()

11.2.3   The Monad Pattern

11.2.4   The Continuation Monad

11.2.5   The List Monad

11.2.6   Other Monads

11.2.7   Exercise

11.3   Where to Next

11.3.1   Functional Programming

11.3.2   Generic Programming

11.3.3   Higher Kinded Types and Category Theory

11.3.4   Dependent Types

11.3.5   Linear Types

11.4   Summary

11.5   Answers to Exercises

sitemap