Maybe
monad, IO
Monad and State
Monad is an example of a widely used monad.
In this article, you’ll learn how to use them, why they’re useful, and how to build them with Typescript.
Monads are a concept in the world of functional programming and category theory.
Monads help you structure your programs in a functional and configurable way.
It can also help manage conditions and side effects. Yes, you can do a lot of things.
It’s time to get to know them.
Monads can be thought of as boxes. That is, you can take a value and insert it inside a box, also known as a box. Just
.
If there is no value, the value is represented as an empty box. Nothing
.
next, Maybe
it can take the form of either Just
or Nothing
.
with this, Maybe
As a tool, the question is: what can you do with it?
One of the great features of monads is their composability, which is achieved by using special functions known as . bind
(sometimes called as:) flatMap
or chain
).
bind
This allows you to take a function, apply it to a monad, and receive a new monad in return.
In the example below, the monad does not contain any values.after use bind
It will remain without value:
we will just being together there is nothing (pun intended).
You can continue binding with more functions without worrying about missing values.
That’s why this tool is so useful. Monads allow you to write cleaner code with fewer conditional checks for the existence of values.
Now, let’s write some code!
first, Maybe
class:
What we define is abstract
Class with a single method – bind
.
bind
takes a function, and that function operates on the current value inside. Box
create a new one Maybe
Monad.
Next, you need to implement the following class: Just
and Nothing
class:
of Just
Class holds private properties − value
.
of bind
A function takes a function as input and applies it to a private value, resulting in a new value. Maybe
.
On the other hand, when implemented, Nothing
it’s simple.it always comes back Nothing
!
What you need is a function as input to. bind
.
Create a prop
Functions that access properties within objects:
The definition may seem a little complicated, mainly because of the types involved. However, this function essentially returns: Just
if the object has the specified property, and Nothing
Otherwise.
An example of usage is shown below. prop
with Maybe
Monad:
Notice how prop
Returns a function that accesses values in a monad and creates a new monad.
With a few changes to the input, it looks like this: Nothing
.
We’ve created a secure way to access any object.
Also, if you look closely, it may look a lot like something else.
Yes, yes, this is similar to a chain of options, effectively Maybe
Monad. This is very exciting, at least for me.
You can keep binding more functions, but Maybe
Monads, sometimes you need to extract values from a monad. For example, print the data or save it to a database.
To get values from a monad, you can use a function called . match
(In Haskell, it simply uses pattern matching.)
The monad is Just
, isJust
The function is executed. Nothing
(As you can imagine) ifNothing
The function is executed.
The definition is: match
function:
of ifJust
The function gets the value that was inside the monad, but ifNothing
Works without value.
What determines the return value? ifJust
or ifNothing
Return value.
In most cases, the returned value will look like this: void
Because monads retain their values until the side effect is executed.
The example below prints the value if it exists. Otherwise it will log “Oh, no value”.
Extracting values from a monad is important, but it’s safer for your code to defer extraction until it’s absolutely necessary.
You can try out the complete example here.
Having investigated this far, Maybe
Monads, you can apply this knowledge to various other “boxes”.
state monad
The State monad is used for state management and manipulation. It’s a way to encapsulate stateful computations without relying on mutable variables or global state.
IO monad
IO Monad is used to manage side effects. Examples of side effects include reading or writing files, interacting with the network, or performing other impure actions.
either monad
Both monads provide a way to propagate and handle errors through a set of computations without requiring explicit error checking or branching in your code.
These monads share similar concepts with monads. Maybe
Since it’s a monad, you can now explore it on your own.
What’s interesting is that we can discover even more monads.
Our journey started with exploring the box metaphor, which helped us understand how it works. Maybe
You can think of a monad as putting values into a box.
we learned about bind
a tool that allows you to configure various calculations in a pure and seamless way.
I then proceeded to build my own full version. Maybe
You created a monad in TypeScript and applied it to implement your own chain of options.
We provided a brief overview of other monads and discussed the use cases for each.
After reading this article, I hope you’re no longer afraid of the “M word” and are inspired to explore and write more pure, composable, and beautiful code.
Let’s have fun learning!
resource:
The illustration was drawn at https://whimsical.com/.
Editing credit: Keren Haham.