3 ms·
jQuery's add function has a few different formulations depending on what argument you pass, but it's basically either M a -> a -> M a (if you pass it an html st
by nbouscal 13y ago
jQuery's add function has a few different formulations depending on what argument you pass, but it's basically either M a -> a -> M a (if you pass it an html string, an element, or a selector) or M a -> M a -> M a (if you pass it another jQuery object) where here M refers to the jQuery wrapper and the first argument refers to `this` (obviously these type signatures are an approximation since JavaScript doesn't really work this way). join is M (M a) -> M a, which is very different.
The reason I picked join is because (in my view, anyway) it's the easiest demonstration of how jQuery is not a monad. There's no way to even get something of type M (M a) in jQuery, because the jQuery lifting operation is idempotent. The same exact problem exists with return, but I've found that people have a hard time seeing it with that example.
The problem I have with you referring to jQuery as a monad, even by analogy, is that jQuery really really isn't a monad. It's only very loosely analogous, and that analogy is more likely to teach people the wrong things about monads rather than the right ones.
- TheZenPsycho 13y agoWell that is a fair enough concern, and I notice I used the word "Monadic" to describe jquery carelessly, to mean "Monad-like" though that isn't really what "Monadic" means. For the case in hand though, you can get to almost but not quite really technically a monad, and still cure most of the nested callback headaches. One way I think about monads is they are a strategy for turning nested functions into sequentially composed functions. Which is, pretty much what we want, right?
- nbouscal 13y agoI just don't think it's useful to use the word monad to describe the strategy you're suggesting. I'm not criticizing your suggestion, I just don't think it has anything to do with monads. More to the point, I don't understand why we (HN and the JS community at large) keep having this discussion. Coroutines have been around since the 60s. This is a solved problem.
- TheZenPsycho 13y agobecause you can't use coroutines in client-side javascript yet, without some backpiler, and people will disagree on what the proper reaction to that should be.