dtao / lazy.js

Like Underscore, but lazier
http://danieltao.com/lazy.js/
MIT License
6.01k stars 267 forks source link

Feature: function creation #76

Open dbkaplun opened 10 years ago

dbkaplun commented 10 years ago

It would be really cool if I could do _.pluck('length').max() and get a function which I can pass a parameter to that will return the result having passed it in the first place.

akre54 commented 10 years ago

Are you talking something like _.partial or _.compose from Underscore?

dbkaplun commented 10 years ago

Kind of. I think it would be awesome if _.pluck('length').max() returned function (val) { _(val).pluck('length').max(); }.

akre54 commented 10 years ago

Ah, so sort of more like _.property then. jashkenas/underscore#1582 is considering implementing a more flexible version of this across a few of the core Underscore functions. maybe Lazy could do something similar.

dtao commented 10 years ago

There's actually already something like this, but only for functions that return sequences (not values, so not max); and the interface isn't too great so it will probably change.

But there is a method called apply which will treat a sequence as a sort of "query" and get the result of applying that query to an underlying source.

For example:

var query = Lazy()
  .map(function(x) { return x * -1; })
  .filter(function(x) { return x > 0; });

query.apply([1, -2, 5, -6]);
// => [2, 6]

I think implementing a more full-fledged version of this, with support for things like max as you suggest, is an interesting idea. I'll think on it.