Hacker News new | past | comments | ask | show | jobs | submit login
JQuery Combinators: Make any function into a jQuery method (github.com/raganwald)
44 points by toni on June 22, 2010 | hide | past | favorite | 10 comments



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);


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.


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?


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.



You're talking to the author.


Shhhh!

This is good. :)


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")


Your wish is a contributor's command:

http://github.com/raganwald/JQuery-Combinators/commit/728713...

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.


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.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: