Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added two function methods _.curry and _.attach (+ tests + docs) #467

Closed
wants to merge 3 commits into from
Closed

Conversation

alexindigo
Copy link

Developing project with node.js and underscore and toolbox for both client- and server-side.

Very quickly realized that I'm missing 'curry' method back from my prototype days,
so decided to migrate one here. I believe many would benefit from such beautiful concept.

Another big part of my project is flatiron/director, which very nice, although requires some dances around when you want to separate your request handlers and environment setup. To make certain variables available within the handlers but without passing them as extra parameters (which looks ugly if it's more than one), I decided to make them part of the context, but I can't use bind, since I need access to the original context of a handler for stuff like this.req and this.res, so the only way is to _.attach my variables to the original context. With growing usage of node.js and libs like flatiron/director it would be very convenient to have _.attach as core method.

@jashkenas
Copy link
Owner

Thanks for the pull request -- in the future, please don't edit the documentation or rebuild the minified version of the JS when sending patches...

Underscore.js already includes curry: http://underscorejs.org/#bind

As for _.attach, I really don't understand the use case for your source code:

// Attaches new properties to the context of the call
// in other words
// Extends passed object(s) with 'this' and makes it context
_.attach = function(func)
{
  var obj = _.reduce(_.rest(arguments, 1), function(obj, arg)
    {
      return _.extend(obj, arg);
    }, {});
  return function()
  {
    var thisArg = _.extend(_.clone(this), obj); 
    return func.apply(thisArg, _.rest(arguments, 0));
  };
};

I don't see how that would be a function that most Underscore users would find handy. Feel free to open a new pull request with just the attach function included if you'd like to motivate it further.

@jashkenas jashkenas closed this Feb 10, 2012
@alexindigo
Copy link
Author

  1. There is no dev docs on how to send patches, one supposed to guess.
  2. With bind you're altering context as well, in some cases I want to preserve the context.

As for attach, modified example from flatiron/director

// api.js
  function helloWorld(route) {
    this.res.writeHead(200, { 'Content-Type': 'text/plain' })
    this.res.end('hello world from (' + route + ')');
  }

// router.js
  var router = new director.http.Router({
    '/hello': {
      get: helloWorld
    }
  });

And I want to provided to my handlers stuff like db-resourse, application home path, etc.,
without expanding list of function parameters, but rather adding to the context.

Using _.attach

// api.js
  function helloWorld(route) {
    fs.readFile(this.apphome+'/etc/passwd', function (err, data) {
      this.res.writeHead(200, { 'Content-Type': 'text/plain' })
      this.res.end(data);
    });
  }

// router.js
  var apphome = '/var/www';
  var router = new director.http.Router({
    '/hello': {
      get: _.attach(helloWorld, {apphome: apphome})
    }
  });

@alexindigo
Copy link
Author

@jashkenas Please check my example for _.curry and _.bind difference http://jsfiddle.net/CqG7n/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants