Skip to main content

Bug Tracker

Side navigation

#8379 closed feature (duplicate)

Opened February 25, 2011 12:30AM UTC

Closed February 25, 2011 08:12PM UTC

Last modified February 25, 2011 08:12PM UTC

Missing reduce()

Reported by: jk@frozen-doe.net 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
Attachments (0)
Change History (10)

Changed February 25, 2011 12:56AM UTC by rwaldron comment:1

component: unfiledcore
keywords: → needsreview
priority: undecidedlow
status: newopen
type: bugfeature

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

Changed February 25, 2011 09:35AM UTC by jitter comment:2

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

Changed February 25, 2011 05:51PM UTC by addyosmani comment:3

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.

Changed February 25, 2011 06:05PM UTC by rwaldron comment:4

keywords: needsreview
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.

Changed February 25, 2011 06:38PM UTC by dmethvin comment:5

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.

Changed February 25, 2011 06:59PM UTC by jk comment:6

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).

Changed February 25, 2011 07:45PM UTC by rwaldron comment:7

: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

Changed February 25, 2011 08:10PM UTC by rwaldron comment:8

resolution: wontfix
status: closedreopened

Changed February 25, 2011 08:12PM UTC by rwaldron comment:9

resolution: → duplicate
status: reopenedclosed

Changed February 25, 2011 08:12PM UTC by rwaldron comment:10

Duplicate of #1886.