Hello! Today, we will explore functional programming with the concepts of functors, applicatives, and monads. We will discuss what they are and why they matter one step at a time. Note that all the examples will be in Haskell, but you’re not required to know Haskell to read this post.I’ve also added a section at the end following a pretty disappointing interaction with someone associated with the Haskell foundation.This is a closed box:Of course, as with every closed box, you can’t really know what’s inside unless you open it, right? So let’s open it, and… surprise! The box contains the answer to the Ultimate Question of Life, The Universe, and Everything:Now, let’s translate the box analogy into programming. A box can be any data structure surrounded by a context. Said differently, a wrapper or container type that adds additional information or behavior to the underlying data.In this example, we will say that blue boxes represent the possibility of an optional value, which in Haskell is denoted by the Maybe type. But, this concept exists in many languages: Rust with Option, Go with sql.NullString, Java with Optional, etc.Yet, other types of boxes also exist. For example:A box representing that the wrapped value can be either from one type or another. For example, in Haskell, Either to represent a value that is either Left or Right, or in Rust, Result to represent either a success or an error.More generally, most classic data structures we can think of such as lists, maps, trees, or even strings. Those data structures can contain zero, one, or multiple values inside. For example, a string can be composed of zero, one, or multiple characters.We already know how to apply a function to a simple value; for example, applying a function that adds one to a given int:Here, the white square represents a function that takes an integer and adds one to it.But what if we want to apply the same function to a value inside a box? We could open the box, extract the value out of it, ...
First seen: 2025-03-30 17:34
Last seen: 2025-03-30 22:36