r/functionalprogramming • u/Zyansheep • Jun 26 '22
JavaScript Really good explanation of a monad in under 100 seconds
https://www.youtube.com/watch?v=VgA4wCaxp-Q
125
Upvotes
r/functionalprogramming • u/Zyansheep • Jun 26 '22
20
u/Migeil Jun 27 '22
This seems to explain Functor, not Monad.
The bind method here is just a map, not a flatMap or actual bind.
What bind does in a monad, is it takes a function which returns the same monad, and then flattens it, hence flatMap.
Look at the types in Haskell:
fmap :: (a -> b) -> f a -> f b(>>=) :: m a -> (a -> m b) -> m bThe bind method in this video has the first type. It takes an implicit
f a(the object instance) and a functiona -> band returnsf b. There's no flattening. If I were to pass it a function which would return aMaybe, the result would be aMaybe (Maybe b). An actual monad would return just aMaybe b.9
u/nvnehi Jun 27 '22
Great example, concisely described, abundantly clear, and I can't wait to read why it's wildly inaccurate, clearly offensive to burritos, and how I'll be wrong for thinking that it's well described.
In all honesty, I haven't programmed in quite a while, and while I recognize this pattern I must admit that something feels off. I'm willing to admit that it may be because of all of the examples I've seen, and read over the years being jumbled up in my head. Is this considered a valid description of a Monad, and how it works?
In the past it always felt that Monads sparked arguments with the predominant distinction being one side was more worried about what they technically are, and the other side being more worried about describing their application, with both sides pointing at each other screaming that they were clearly wrong while those in the middle became even further confused.
I only know that Monad's are clearly burritos. However, I do not know whether that burrito is edible, or simply a picture of one.
3
u/josephjnk Jun 26 '22
I’m not sure that this is a good way to present monads. A monad is a type and functions defined on the type which have a specific type signature and have specific properties. Nothing more, nothing less.
7
u/KyleG Jun 27 '22 edited Jun 27 '22
Monad in four words:
It is a flatmappable.
If you can do flatmap with arrays, then you understand the totality of what a monad is.
All other features are features of other things, not a monad.
Edit OK I suppose technically if you know what a functor is, then learning "flatmap" groks u a monad.
6
u/dun-ado Jun 26 '22
Not even wrong.
4
u/FloweyTheFlower420 Jun 27 '22
A monad is just a monoid in the category of endofunctors
2
u/dun-ado Jun 28 '22
Not even wrong.
3
u/[deleted] Jun 26 '22
Maybe not: https://www.youtube.com/watch?v=YR5WdGrpoug&t=2611s