#8379 closed feature (duplicate)
Missing reduce()
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | low | Milestone: | 1.next |
Component: | core | Version: | 1.5.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
We have map(), each(), filter(), but there is no fold() nor reduce() functions.
Example - sum of all rows:
$('input.row').reduce(function(a,b) {return parseInt(a.val()) + b; }, 0); // result is sum of all values in .rows
Example - "Are all inputs empty?":
$('input').reduce(function(a,b) { return (a.val() == '') & b;}, true); // result is true if all fields are empty
Change History (10)
comment:1 Changed 12 years ago by
Component: | unfiled → core |
---|---|
Keywords: | needsreview added |
Priority: | undecided → low |
Status: | new → open |
Type: | bug → feature |
comment:2 Changed 12 years ago by
Sounds a little feature creepy to me. Why not simply underscore.js?
comment:3 Changed 12 years ago by
I would second jitter's concerns here. Although it's an interesting idea for jQuery to offer further utility methods, I think those people that really want to use these will likely end up either opting for Underscore or their own homebrewed code.
comment:4 Changed 12 years ago by
Keywords: | needsreview removed |
---|---|
Resolution: | → wontfix |
Status: | open → closed |
@jitter - I'm actually 100% in agreement. Initially I had written a similar response to yours but then decided to see what others had to say...
A major issue with writing a reduce function is in the question, "what are we reducing?" if I pass in a collection of divs, what will be reduced? Arrays, object values and even input element values are obvious - but outside of that it becomes very gray.
I think 3 agreeing comments is sufficient to close.
comment:5 Changed 12 years ago by
We've generally added/documented utility methods because they are needed internally by jQuery, similar to the rules for jQuery.support. We don't need reduce or fold internally afaik.
comment:6 Changed 12 years ago by
Every functional language have set of basic/elemental functions to operate with arrays or lists. These are:
- each() ... to iterate an array
- map() ... to convert array into another
- first(), split() and similar ... to cut array into pieces
- filter() .... to remove elements from array
- fold()/reduce() ... to convert array into scalar value
jQuery miss only last one.
what are we reducing?
Answer is simple: reducing acts like binary operator. Left operand is div from a collection, right one is anything else. Return value is of the same type as right operand, becouse right operand is return value of processed part of collection (array).
comment:7 Changed 12 years ago by
:D I know HOW to reduce...
What I'm saying is how do we determine WHAT values will be passed as arguments to the reduction?
Anyways, I'd love to see an implementation with use cases and tests covering a wide breadth of reductions
comment:8 Changed 12 years ago by
Resolution: | wontfix |
---|---|
Status: | closed → reopened |
comment:9 Changed 12 years ago by
Resolution: | → duplicate |
---|---|
Status: | reopened → closed |
This could be interesting to look into... Feel free to share thoughts or draft implementations here.