FP for Sceptics: Intuitive guide to map/flatmap

Backbone of Functional Programming

map & flatmap form the backbone of Functional Progamming. It is very important be comfortable with these two concepts and this guide will help you develop an intuition for them.

This guide is language agnostic but I expect you to be familiar with basic programming concepts like functions, types, data type/objects and list.

Nomenclature

Let me introduce you to some basic terms we will be using to talk about map/flatmap.

  • Types are data constructs and they can be Basic Types like Int, String etc.
  • Container is a special type which contains another type. For example a List of Ints.
  • Function is a function whose input is of Type 1 and output is of Type 2. Type 1 & Type 2 can be same type.

Visualize

Though I have defined three terms it will be easier to think about them visually.

Basic types (Int, String etc) will be represented by shapes

Basic Types.



Containers will be represented by shapes

Empty Containers

In reality, we will always use

Container for a Type

Square for Triangle

etc.

Functions will be denoted as

f : Triangle -> Star

Verbalize

Let's see how to verbalize these terms:

Square for Triangle Container for Triangle

Square for Circle for Triangle Square for Circle for Triangle

f : Triangle -> Star f is a function from Triangle to Star

g : Triangle -> Square for Triangle g is a function from Triangle to Container of Triangle

Fundamentals

In this section, I want to introduce one basic concept along with map/flatmap.

Function Composition

Function Composition is chaining two or more functions such that output of one function feeds into to the input of second function and so on.

Derivation of g . f : Triangle to Container of Reverse Triangle

map

map is a function that takes a Container (C1) & a Function (F) as input and returns a new Container (C2) for Function's (F) output type.

Rules for map

  • Type contained by Container (C1) has to match Function's (F) input type.

map : (f : Triangle -> Reverse Triangle, Container for Triangle) -> Container for Reverse Triangle

flatmap

flatmap is a function that takes a Container (C1) & a Function (F) as input and returns a new Container (C2) for Function's (F) output type.

Rules for flatmap

  • Type contained by Container (C1) has to match Function's (F) input type.
  • Function (F) has to return a Container (C2)
  • C1 == C2. This is very important to remember as many people trip up on this.

flatmap : (f : Triangle -> Container for Reverse Triangle, Container for Triangle) -> Container for Reverse Triangle

Note: As you can see, both map & flatmap have the same written definition but the strictness is in the rules it has to adhere to.

Exercise Problems

Let's conclude with a few exercise problems.



Problem Set: Map

Problem Set: Map

Solutions

  1. Container for Triangle
  2. Container for Circle for Triangle
  3. Error! Breaks the rule for map
  4. Error! map works on a Container, not a Basic Type

Problem Set: FLatMap

Problem Set: FlatMap

Solutions

  1. Container for Triangle
  2. Container for Circle for Triangle
  3. Error! Breaks the 2nd Rule for flatmap

Problem Set: Advanced FlatMap

Problem Set: Advanced FlatMap

Solutions

  1. Triangle to Reverse Triangle
  2. Triangle to Container for Reverse Triangle
  3. Circle for Reverse Triangle to Square for Rev Triangle



Conclusion

I hope after going through this guide, you are more comfortable working with map/flatmap.

If you liked this guide, have a look at the rest of my posts on FP (in Scala) - FP for Sceptics


Would you like a free ebooklet?

FP for Sceptics: Algebraic Data Types