Why monads don't make sense in JavaScript

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

Monads are useful for all programming as well as pure functional programming. All a monad is the general version of the apply / call method in JavaScript; and the "then" in promises/futures, pipe many JavaScript libraries, and the flatMap in lists. In OOP you are forced to place methods in one object, but if you think about it instead of putting "then" in futures/promises/callbacks you can put them on functions and call it "from". You can also put "then" on all Objects so you can say [1, 2, 3].then((i) => i % 4 === 3). By using then for both synchronous and asynchronous programming you can easily change your function to be async or sync or both, or add other semantics to your program. By replacing your function with another callable object you can keep context and change your program in powerful ways (this is the same thing as Monads as apply/call is just then flipped). Also in TypeScript the Monads can be type checked btw. Because then, pipe, apply(flipped), flatMap all have the same "name" or interface in Haskell you can create the Monad map(aka traverse) and sequence functions that work on all computations not just one. Instead of calling them Monads you can call them "Binders", "Pipes", "Thenables", btw.


let Monad = {}; // A Monad is any type of chainable computation
Monad.prototype.sequence = <blah>
Monad.prototype.map = <blah>


Object.prototype.toMonad = (obj) => {
Object.create(Monad, {bind: obj.bind || obj.then || obj.flatMap || obj.pipe || obj.call);
}

etc...

aoeu
Автор

I just think you don’t understand what a monad is. Functions too are monads. They add the effect of laziness to normal values. Monads are ubiquitous

kasozivincent
Автор

I actually asked my functional programming professor about this when I took the "Functional Languages" elective at university. He said he believes the primary advantages of monads arise when you have a strong type system (the class was taught in Haskell, so he definitely liked his types), however, he said it is possible that the "plumbing" of your code could potentially be improved if you wrote it in a monadic style. So perhaps looking at the code and the way the data flows through could be made easier in some circumstances if monads are used.

Overall though I think you are right when you say that people are largely doing this to be cool.

aerialdude
Автор

Thanks for your thoughts on the matter. But I disagree.
Whether you guys like it or not, functional programming is coming back. OOP in modern world has lots of problems and it just simply can't keep up with the things we have to deal with today: microservices, scalability etc. Classical inheritance pattern is a dinosaur that will eventually go extinct. It's just not good.
The arguments about types. Well, in functional programming you can't mutate data. So, you don't have to be worried about types. Types control is just enforced on you due to the immutability concept. There's a library called immutable.js which will essentially resolve most if not all of your worries over types control in JS.
The problem with functional programming (with concepts like monads etc) is just like you said, it wasn't mainstream for a long time. So the language which is used in explaining functional programming is more mathematical and academic. But it's changing. Again, like you said there is lots of good material that explains functional programming (including monads) in understandable common folks' language today.
But we have to keep up. In 5 years from now, I believe most of the JS jobs will require a solid understanding of functional programming, including monads. So, let's talk more about it. Let's educate ourselves and help each other to embrace functional programming. If we want to be relevant as JS developers in 5 years from now, we have to learn functional programming today. Functional, composable JS is the future of JS.

alexandersobolev
Автор

That thing about Maybe should be used in every single input just shows how little you know about the way it they are used. Even in Haskell, for example, not every function uses a Monad. Monads just encapsulate with additional information values. So, please don't advice about what you really don't understand.

JesusEnriqueFrancoMartinez
Автор

Apparently, arrays and Promises are monads. So you don't use them?

badwolf
welcome to shbcf.ru