

JQuery is a monad - henning
http://importantshock.wordpress.com/2009/01/18/jquery-is-a-monad/

======
jpcx01
Great strength, but also a great weakness of jQuery. My most common jQuery bug
is a rule that doesn't get applied correctly (wrong selector) and it just
fails quietly. It's very hard to write console.log statements, or even do
debugging breakpoints on a long method chain.

Probably a way to solve this problem. Maybe jQuery needs a monadic method that
logs the current chain object without interupting the flow.

$("div.blah > h1").logCurrent("Showing current item:")

where log current would display your message along with a representation of
the current state of the transitionary object, and then return it back so you
can continue on with the chain.

Something like this probably already exists. Or it wouldnt be too hard to
write a plugin.

~~~
shutter
Something like this would work:

    
    
        jQuery.fn.logCurrent = function (title) {
            console.log(title, $.makeArray(this).join(', '));
            return $(this);
        };
    

But it depends what you want to log for each element. I think Firebug's output
would give clickable links in that case, but you could alternatively
cherrypick class names or the like.

------
burke
I've casually skimmed a couple explanations of monads over the last little
while, but this one finally made it "click" for me. Great post.

~~~
russell
Likewise, I could never quite get monads. I was afraid that I might have to
learn Haskel to understand them. Now, I realize that they are a pattern that I
have used for decades but applied inconsistently. This was one of those "Ah
ha" moments where I realized that I had almost had it and probably would have
gotten it with a little more introspection about Jquery.

------
rcoder
There are a limited subset of the jQuery core functions which function much
like a monoid (though not strictly a monad), but the lack of strictness means
that you can escape the "monadic" container at any time. Utility functions,
global assignments, etc., further compound the issue.

That being said, presenting monadic designs in this fashion could well help
understand the basics, which is to be applauded.

------
andr
So monads are essentially decorators with method chaining?

~~~
eelco
It's (just) one of the usages of monads, but it's not what monads 'are'.

~~~
d0mine
As presented in the post they are.

 _Monads are abstract data types that satisfy three conditions: 1) They wrap
themselves around other data types 2) They have an operation, confusingly
called return, that actually performs the aforementioned wrapping 3) They have
an operation called bind that allows to feed the value wrapped inside the
monad into another function, as long as the function returns a monad._
</quote>

What condition is not satisfied by decorators with method chaining?

~~~
jerf
You've got eelco's point backwards... eelco is saying that there are things
that are not "decorators with method chaining" that are monads.

~~~
d0mine
You've got my point backwards.. I'm saying that iff "decorators with method
chaining" satisfy _all_ conditions presented in the paper then _any_ monad (as
they defined in the paper) _can_ be expressed as a "decorators with method
chaining".

A single counter-example (a monad in the above sense that can't be expressed
as a decorator) would suffice.

~~~
jerf
Monads don't need to be methods. In fact every Haskell monad is not a
"decorator with method chaining" because there are no methods in Haskell in
the traditional sense, and even if you count classes, there's no constraint
that a monadic value has to be involved with a class.

If you squint at Haskell to the point that you start seeing methods in all
monad uses, you're squinting so hard that the word "method" has lost all
meaning and become a synonym for "function". That is to say, I can see how
that argument goes, but it comes at the cost of totally devaluing the
distinction between "method" and "function", something I'm not willing to do.

------
invisible
One thing he mentions is inaccurate: jQuery IS limited to it's own built-in
functions; however, it defines functions that utilize a few Array methods
using apply (e.g. Array.prototype.map.apply(this, arguments) where this is the
jQuery object ).

This is possible because jQuery defines it's contents inside each object
numerically:

    
    
      var jQueryObj = $(".someClass");
      alert(jQueryObj[0].innerHTML);

