#1886 closed enhancement (plugin)
Add jQuery.reduce()
Reported by: | genezys | Owned by: | |
---|---|---|---|
Priority: | trivial | Milestone: | |
Component: | core | Version: | 1.2.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
As jQuery already provides a Map algorithm for arrays, it would be nice to have a Reduce algorithm too. These too are the fundamentals of functionnal programming and jQuery already has a very good Map algorithm
I already did a Reduce implementation using jQuery, here it is:
jQuery.reduce = function(arr, valueInitial, fnReduce) { jQuery.each( arr, function(i, value) { valueInitial = fnReduce.apply(value, [valueInitial, i, value]); }); return valueInitial; }
This is not really long and can really enhance the meaning of some code.
I think it can also help clarify some jQuery internal code that concatenate to a single value like text() or html().
Thanks
Change History (30)
comment:1 Changed 15 years ago by
comment:2 Changed 15 years ago by
Resolution: | → wontfix |
---|---|
Status: | new → closed |
This won't be useful in the core, it can always be included in a plugin.
Reopen if you find places where it can reduce code size in the code, and please point them out.
Thanks
comment:3 Changed 15 years ago by
Milestone: | → 1.2.4 |
---|
comment:5 follow-up: 6 Changed 12 years ago by
Not that I want to revive a dead horse, but given that Array.reduce found its way into ECMA 1.8 I guess it wouldn't be too bold a move to include it also in Jquery with the same syntax:
comment:8 Changed 11 years ago by
reduce is available in native, ES5 compliant JavaScript implementations as well as many open-source utility libraries, see also: http://lodash.com/docs#reduce
comment:9 Changed 11 years ago by
reduce is not available in IE8: http://kangax.github.com/es5-compat-table/#. IE8 represents significant marketshare (~26%) and is probably exclusive in many Windows domain intranets. Why not include it?
comment:10 Changed 11 years ago by
There are plenty of libraries that specialize in utility functions, I recommend Lodash http://lodash.com/
comment:11 Changed 11 years ago by
Why are $.each and $.map in the core? Plugins/shims can be used for those, too. The point is that they are useful and it's nice to be able to rely on a common set of features everywhere that jQuery is available.
Sometimes adding plugins into a project isn't our call, or requires buy-in from a senior dev.
I'm mostly curious how $.each and $.map are ok, while $.reduce is completely orthogonal to jQuery's core values.
comment:12 Changed 11 years ago by
Why are $.each and $.map in the core?
Because we need and use them internally.
comment:13 Changed 10 years ago by
Suprised jQuery doesn't have this, I think it is now widely used as a standard language extension and as such it should have this basic function!
comment:15 Changed 10 years ago by
Even Java is going to add .reduce() now to the JDK 8 Collections API: http://cr.openjdk.java.net/~briangoetz/lambda/sotc3.html
I'd really love to see this in the jQuery core
comment:17 Changed 10 years ago by
Yeah, I hate to add the "me too", but this both jives for parity with other array-like methods, and in addition to use wth arrays, would be great for custom ways to serialize nodes, if present also on elements.
var hrefs = $('a').reduce('Your links: ', function (prev, curr) { return prev + '<br>' + $(curr).attr('href'); });
comment:18 Changed 10 years ago by
Milestone: | 1.2.4 |
---|---|
Resolution: | wontfix |
Status: | closed → reopened |
This can be done as a plugin. If you have created one, please post a link to it here.
comment:19 Changed 10 years ago by
Resolution: | → plugin |
---|---|
Status: | reopened → closed |
We wouldn't land this in jQuery unless we needed it internally.
comment:20 Changed 10 years ago by
So are we to interpret all $.xxx functions as internal only to be used by jQuery itself? Honestly. The line of argument for keeping this out doesn't work. map/reduce goes together.
comment:21 Changed 10 years ago by
@anonymous If the method is documented, it's not for internal use only. The functionality is prescribed by the needs of the core library. The argument for keeping this out is perfectly valid. If the method isn't useful inside jQuery, there is absolutely no reason to add it. jQuery is a DOM library, if reducing arrays doesn't help jQuery do it's job, then it doesn't belong.
comment:22 Changed 10 years ago by
I think we should have the "reduce" functionality included. This also bridges the gap between the old browsers and new browsers which have "reduce" function in built.I know there are libraries out there like underscore.js which has this functionality, but to use "underscore.js" just to have "reduce" functionality is a crazy idea :) After all we have all these awesome libraries to do better with less code, isn't ?
comment:23 follow-up: 24 Changed 10 years ago by
Given an implementation of "map", there ought to be a "reduce". Maybe it was an error to introduce "map" in the first place, as it creates an expectation that there is also a "reduce". But it is what it is, so add "reduce" and be done with it.
comment:24 Changed 10 years ago by
Replying to anonymous:
Given an implementation of "map", there ought to be a "reduce". Maybe it was an error to introduce "map" in the first place, as it creates an expectation that there is also a "reduce". But it is what it is, so add "reduce" and be done with it.
No.
comment:25 follow-up: 27 Changed 10 years ago by
The argument to not include "reduce" is invalid.
Just an example:
jQuery has "unique" (remove duplicates from an array) and works this way:
1) copies the array 2) sorts it 3) walks the whole array to find duplicates and copies into a new array the results.
Array unique using EC5 code (shorter and faster):
var arrayUnique = function(a) { return Array.isArray(a) ? a.reduce(function(p, c) { if (p.indexOf(c) < 0) p.push(c); return p; }, []) : undefined; };
reduce function can be used to improve lots of parts of the code.
comment:26 Changed 9 years ago by
I have created a gist that might be useful for anybody looking for a reduce implementation: https://gist.github.com/lutzissler/6822599
comment:27 Changed 9 years ago by
Replying to [email protected]…:
jQuery has "unique" (remove duplicates from an array) and works this way:
1) copies the array 2) sorts it 3) walks the whole array to find duplicates and copies into a new array the results.
Array unique using EC5 code (shorter and faster):
var arrayUnique = function(a) { return Array.isArray(a) ? a.reduce(function(p, c) { if (p.indexOf(c) < 0) p.push(c); return p; }, []) : undefined; };
This is a bad example. Your function has O(n**2)
(quadratic) complexity vs O(n log n)
for the algorithm you describe for jQuery. This means that it will scale badly and it's a bad place to use a reduce function.
comment:28 follow-up: 30 Changed 9 years ago by
A alternative to have reduce is reuse it from Array. Something like this:
(function($){ $.fn.reduce = function(callback, initial){ return Array.prototype.reduce.call(this, callback, initial); } })(jQuery);
comment:29 Changed 9 years ago by
As mentioned before, Array.prototype.reduce exists in ECMAScript 5.1
Rather than:
$(...) .map(...) .reduce(...)
In most modern browsers you can already do:
$(...) .map(...) .get() .reduce(...)
For a compatibility table and pollyfill see MDN
comment:30 Changed 9 years ago by
Replying to [email protected]…:
A alternative to have reduce is reuse it from Array. Something like this:
(function($){ $.fn.reduce = function(callback, initial){ return Array.prototype.reduce.call(this, callback, initial); } })(jQuery);
Or just:
$.fn.reduce = [].reduce;
A little smaller. :P
Yeah, I really want this. It would remove the need for a lot of silly for-loops in my code.