Trying to understand monads

1 minute read

Trying to understand monads? Yey! me too!! Let me tell you this, if anyone tells you monads are simple don’t beleive him. If anyone tells you if have unit and bind you have a monad you see that is simple, you get what unit is right? you get what bind is right? so you understand monad! err wrong!

Why monads the first question to ask is could we be doing well without monads why do we need them at all? The answer for me was rather surprising. It’s not to enhance composeability, not beause its so cool to do map / flatMap. It’s because functional programming has a major deficiency! Yes. Although we love fp it has one major drawback (which is the best feature we all love in fp). it should have in its pure form no side effects! being pure is great we love it. But! this means you can’t change any real state! and sorry to acknowledge the truth state is part of the world! when we write some io to disk we change state. (you could argue that the current disk is different than 1 sec ago disk and those are two disks but that would be too much for an argument at this point). So you need to change the state of the disk great, how to do that without changing the state? well we have a workaround and its called a monad! that is one of the major reasons we need them. What we are going to do is extract the operation that changes the disk state and have a function that returns it. But once we have a function that returns that it may not compose into the next function in our pipeline. No-one said the next function in pipeline is going to take an argument of type a function which changes the state of the disk. The monad is going to resolve exactly this. So this is what monads are going to solve and its also referred to as the monad tutorial falasy (most of the tutorials deal with the technicalities of it). The next step is to go through how to build a monad which I plan to do soon: you could have invented a monad

Categories: , ,


Leave a Comment