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) #476

Closed
wants to merge 1 commit into from
Closed

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

wants to merge 1 commit into from

Conversation

alexindigo
Copy link

  • _.curry – Curries (burns in) arguments to a function, without forcing new context (inspired by prototypejs).

Difference between _.curry and _.bind: http://jsfiddle.net/CqG7n/

  • _.attach – Attaches new properties to the context of the call, in other words it extends passed object(s) with 'this' and makes it context.

To illustrate my use case I 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

@lvivski Partial is specific to the language implementation, like Haskel, where you can pass partial number of parameters and in return get function for remaining parameters. I think it could be implemented in javascript. But I don't see practical value for that. And on javascript side curry is well-known in the community in this particular implementation because of PrototypeJS. I believe it would be less confusing to have it this way rather than trying to make Haskel out of javascript.

@liamks
Copy link

liamks commented Mar 27, 2012

Great idea! Curry is an important part of functional programming.

@prust
Copy link
Contributor

prust commented Mar 27, 2012

FWIW, I agree that it would be nice to have _.curry. Our team migrated from prototypejs to underscore and this is one of the holes in underscore. I find myself using fn.bind(null, arg1) instead, which isn't as readable/explicit.

@alexindigo
Copy link
Author

Curry is handy for many different ways, and fn.bind(null, arg1) is not 100% replacement, as I showed here: http://jsfiddle.net/CqG7n/

@alexindigo
Copy link
Author

And recently I bumped into another library that might replace underscore for some scenarios, it has many goodies including curry: https://github.com/ded/valentine

@jashkenas
Copy link
Owner

Let's move this conversation over into #474...

@jashkenas jashkenas closed this Apr 2, 2012
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.

4 participants