Open dbkaplun opened 10 years ago
Kind of. I think it would be awesome if _.pluck('length').max()
returned function (val) { _(val).pluck('length').max(); }
.
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.
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.
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.