Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#8379 closed feature (duplicate)

Missing reduce()

Reported by: [email protected] Owned by:
Priority: low Milestone: 1.next
Component: core Version: 1.5.1
Keywords: Cc:
Blocked by: Blocking:


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 Rick Waldron

Component: unfiledcore
Keywords: needsreview added
Priority: undecidedlow
Status: newopen
Type: bugfeature

This could be interesting to look into... Feel free to share thoughts or draft implementations here.

comment:2 Changed 12 years ago by jitter

Sounds a little feature creepy to me. Why not simply underscore.js?

comment:3 Changed 12 years ago by addyosmani

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 Rick Waldron

Keywords: needsreview removed
Resolution: wontfix
Status: openclosed

@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 dmethvin

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 jk

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 Rick Waldron

: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 Rick Waldron

Resolution: wontfix
Status: closedreopened

comment:9 Changed 12 years ago by Rick Waldron

Resolution: duplicate
Status: reopenedclosed

comment:10 Changed 12 years ago by Rick Waldron

Duplicate of #1886.

Note: See TracTickets for help on using tickets.