personal-page/_master_wiki/Readwise/What Is a Monad - The Last Monad Intro You'll Ever Need.md

36 lines
4 KiB
Markdown

# What Is a Monad? - The Last Monad Intro You'll Ever Need
![rw-book-cover](https://i.ytimg.com/vi/HIBTu-y-Jwk/maxresdefault.jpg)
## Metadata
- Author: [[走歪的工程師James]]
- Full Title: What Is a Monad? - The Last Monad Intro You'll Ever Need
- Category: #articles
- Document Tags: [[dev]] [[dev/design-patterns]]
- URL: https://youtube.com/watch?v=HIBTu-y-Jwk&si=-FZ-FHMBTJpAp2rx
- Archive: https://web-archive.alecodes.page/bookmarks?bf=1&search=&title=What%20Is%20a%20Monad%3F%20-%20The%20Last%20Monad%20Intro%20You%27ll%20Ever%20Need
> [!tldr]
> Monads are a programming concept that helps manage and chain operations in functional programming. They consist of two main operations: `flatMap`, which allows for combining results, and `unit`, which wraps a value in the monad's context. Overall, monads simplify handling complex data flows and operations.
## Highlights
what actually is a monad? As we discussed in the last episode, a functor is a type that
supports the map operation and also satisfies certain mathematical properties. Similarly, a monad is just the type that implements flatMap, which is an operation of this type. In addition to flatMap, a monad also needs to implement another operation called unit, which encapsulates a value within the monad's context. [View Highlight](https://read.readwise.io/read/01jaczd48m97ra9dses5j54b7t))
What is the relation between monads and functors? Previously, we learned that flatMap is a more powerful operation than map in that we can do everything map can do with flatMap. So basically, every monad is also a functor because it supports the map operation but not every functor is a monad [View Highlight](https://read.readwise.io/read/01jaczhyqxbgyhz38wt0jv4aca))
In functional languages, we have this thing called do notation, which is very similar to async await. The only difference is that do notation is applicable to all monads, not just promises. [View Highlight](https://read.readwise.io/read/01jaczksr7myw4y2dcy3srh8a8))
> [!note]
> Q: What "do notation" is in functional programming?
> A: Do notation is a syntactic sugar used in functional programming languages to simplify the chaining of monadic operations, making it easier to read and write code that involves monads. It allows developers to sequence operations without the boilerplate of explicitly calling flatMap or bind for each step, similar to how async/await works with promises in JavaScript.
We're going to introduce this function called tell, which creates a WithLogs instance with an arbitrary log entry, but no value. This function will allow us to insert
any random log message at any point, almost as though we have a global state, yet the entire function remains pure. If you're somewhat puzzled by how this seemingly works like magic, try converting it back to using flat map. And you'll see how it all works under the hood. [View Highlight](https://read.readwise.io/read/01jad02te3gcntke37t3x5fb1e))
> [!note]
> Q: Is this the power and why monads are used in functional programming to maintain a function pure?
> A: Monads provide a way to chain operations while explicitly managing side effects, allowing functions to remain pure by encapsulating effects in types. This ensures that all side effects are controlled and expressed, promoting functional purity and making reasoning about code easier. Thus, monads facilitate the composition of functions without compromising their purity.
Let's do a recap of what we've learned throughout this video. Initially, we wanted to define all side effects in the type system. But we realized that we couldn't chain effects together. That is, unless we use flat map, flat map allows us to chain effectul operations together. And the types that implement flat map are
called monads. So that's basically it. [View Highlight](https://read.readwise.io/read/01jad08vb06vn6mxf0a7ban7g7))
monads allow us to chain operations together under the restriction that all effects must be expressed in the types. [View Highlight](https://read.readwise.io/read/01jad09672gsxmwapnqzfh0d30))