A Guide to Monads in JavaScript

In this series of blogs I’ll try my best to walk you through the Monads basics & implementations, So stay tuned for the next part!Now, without further ado, let’s understand what a Monad is!So what is a Monad?Monad is a design pattern used to descri…

In this series of blogs I’ll try my best to walk you through the Monads basics & implementations, So stay tuned for the next part!

Now, without further ado, let’s understand what a Monad is!

So what is a Monad?

Monad is a design pattern used to describe computations as a series of steps.

With monads, we can define a pipeline, a series of computational steps, that allow us to reuse more of our code, to write it in terms of highly composable parts.

Ultimately, they are just boxes that wrap the value and obey a set of Monadic laws.

They are extensively used in purely functional programming languages like Haskell to manage side effects but can also be used in multiparadigm languages to control complexity.

A rough explanation will look something like this.

Without Monad

With Monad

Yeah, that was the basic definition of Monad in general, if we talk in terms of Functional Programming.

3 Laws of Monad

So as we discussed, Monads are just boxes with values that must obey a set of laws.

Now, what are those laws that define a Monad?

There are mainly 3 Monadic Laws, stated as Left Identity, Right Identity & Associativity.

1. Left Identity

If you have a box (monad) with a value in it and a function that takes the same type of value and returns the same type of box, then flat mapping it on the box or just simply applying it to the value should yield the same result.

Let’s understand with the example.

2. Right Identity

If you have a box (monad) with a value in it and you have a function that takes the same type of value and wraps it in the same kind of box untouched, then after flatMapping that function on your box should not change it.

Let’s understand with an example.

3. Associativity

If you have a box (monad) and a chain of functions that operates on it as the previous two did, then it should not matter how you nest the flatMappings of those functions.

Let’s understand with the example.

That was it! I hope you had your ‘Eureka !!!’ moment in the end!

In the next part, we’ll go on with the implementation of Monads in JavaScript and TypeScript

Until next time! 🙂

Build apps with reusable components like Lego

Bit’s open-source tool help 250,000+ devs to build apps with components.

Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.

Learn more

Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:

Micro-Frontends

Design System

Code-Sharing and reuse

Monorepo

Learn more


A Guide to Monads in JavaScript was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
Nikhil Shinde | Sciencx (2024-03-29T15:36:58+00:00) » A Guide to Monads in JavaScript. Retrieved from https://www.scien.cx/2022/11/23/a-guide-to-monads-in-javascript/.
MLA
" » A Guide to Monads in JavaScript." Nikhil Shinde | Sciencx - Wednesday November 23, 2022, https://www.scien.cx/2022/11/23/a-guide-to-monads-in-javascript/
HARVARD
Nikhil Shinde | Sciencx Wednesday November 23, 2022 » A Guide to Monads in JavaScript., viewed 2024-03-29T15:36:58+00:00,<https://www.scien.cx/2022/11/23/a-guide-to-monads-in-javascript/>
VANCOUVER
Nikhil Shinde | Sciencx - » A Guide to Monads in JavaScript. [Internet]. [Accessed 2024-03-29T15:36:58+00:00]. Available from: https://www.scien.cx/2022/11/23/a-guide-to-monads-in-javascript/
CHICAGO
" » A Guide to Monads in JavaScript." Nikhil Shinde | Sciencx - Accessed 2024-03-29T15:36:58+00:00. https://www.scien.cx/2022/11/23/a-guide-to-monads-in-javascript/
IEEE
" » A Guide to Monads in JavaScript." Nikhil Shinde | Sciencx [Online]. Available: https://www.scien.cx/2022/11/23/a-guide-to-monads-in-javascript/. [Accessed: 2024-03-29T15:36:58+00:00]
rf:citation
» A Guide to Monads in JavaScript | Nikhil Shinde | Sciencx | https://www.scien.cx/2022/11/23/a-guide-to-monads-in-javascript/ | 2024-03-29T15:36:58+00:00
https://github.com/addpipe/simple-recorderjs-demo