This content originally appeared on Bits and Pieces - Medium and was authored by Nikhil Shinde
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.
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
- How We Build Micro Frontends
- How we Build a Component Design System
- The Bit Blog
- 5 Ways to Build a React Monorepo
- How to Create a Composable React App with Bit
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.
This content originally appeared on Bits and Pieces - Medium and was authored by Nikhil Shinde
Nikhil Shinde | Sciencx (2022-11-23T13:09:15+00:00) A Guide to Monads in JavaScript. Retrieved from https://www.scien.cx/2022/11/23/a-guide-to-monads-in-javascript/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.