
JQuery Combinators: Make any function into a jQuery method - toni
http://github.com/raganwald/JQuery-Combinators
======
dugmartin
Very nice.

Although I think the documentation would be better served by first showing the
code. Its very short and very self explanatory. It's double awesome that it
hooks into Functional Javascript.

Here is the code:

    
    
        ;(function ($) {
          $.fn.K = function (fn) {
            fn = typeof(Functional) != 'undefined' ? Functional.lambda(fn) : fn;
            fn(this);
            return this;
          };
          if (typeof($.fn.tap) == 'undefined')
            $.fn.tap = $.fn.K;
          $.fn.T = function (fn) {
            fn = typeof(Functional) != 'undefined' ? Functional.lambda(fn) : fn;
            return fn(this);
          };
          if (typeof($.fn.into) == 'undefined')
          $.fn.into = $.fn.T;
        })(jQuery);

------
d0m
I think he should use simpler and more direct examples. I mean, Go only make
the example more complicated with the intersection and stuff. It's like if I'm
trying to show how filter works by using a 20 lines example of a game.. why
not use a simple <p> and show what you can do with it in a simple one liner?

Ok, I know why, since it's functional stuff and more than that, COMBINATOR
STUFF, it _needs_ to be more complicated than what it should be.

~~~
raganwald
Or it could be that those are examples from his own code and he simply copied
and pasted them without thinking that they might not be as useful as
contriving more simple examples.

Do you have some examples you can suggest?

~~~
d0m
Actually, yes. At the end of his "tutorial", he explains in more succinct
words what those functions do, and he use simpler examples. I suggest that
instead of hiding them somewhere at the end in the middle of a paragraph, he
put them in emphasis at the begin of the document.

~~~
alnayyir
You're talking to the author.

~~~
jamesbritt
Shhhh!

This is good. :)

------
lhorie
I was playing a bit with this code last night and made a slight variation for
some code I was writing, which I find easier to use

    
    
      //K variation
      jQuery.fn.run = function(fn) {
        var args = [].slice.call(arguments)
        args[0] = this
        fn.apply(this, args)
        return this
      }
      //contrived example
      var add = function(el, a, b) {console.log(a + b)}
      //no need to curry
      $(".foo").run(add, 12, 34).addClass("bar")

~~~
raganwald
Your wish is a contributor's command:

[http://github.com/raganwald/JQuery-
Combinators/commit/728713...](http://github.com/raganwald/JQuery-
Combinators/commit/728713ee8bb1477d3ba383b7f63361298217bb3c)

I avoid passing a function and its arguments in the same list but it's purely
a personal preference and adding the code doesn't remove any existing
functionality, so it's in.

~~~
lhorie
Yeah, looking in retrospect, binding the this keyword to the function already
exposes the jQuery object inside the function, so the first argument in my
version was redundant.

