Reduce refers to the operation, where a array of items is reduced to a single value. Use with language tag like [c++], [google-sheets-formula]. Do NOT use alone.
Questions tagged [reduce]
3548 questions
667
votes
24 answers
Finding the average of a list
How do I find the mean average of a list in Python?
[1, 2, 3, 4] ⟶ 2.5

Carla Dessi
- 9,086
- 9
- 39
- 53
377
votes
23 answers
How to call reduce on an array of objects to sum their properties?
Say I want to sum a.x for each element in arr.
arr = [ { x: 1 }, { x: 2 }, { x: 4 } ];
arr.reduce(function(a, b){ return a.x + b.x; }); // => NaN
I have cause to believe that a.x is undefined at some point.
The following works fine
arr = [ 1, 2, 4…

YXD
- 31,741
- 15
- 75
- 115
366
votes
7 answers
How to use filter, map, and reduce in Python 3
This is how I am accustomed to filter, map, and reduce working in Python 2:
>>> def f(x):
return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]
>>> def cube(x):
return x*x*x
>>> map(cube, range(1,…

Dick Lucas
- 12,289
- 14
- 49
- 76
259
votes
15 answers
Javascript reduce() on Object
There is nice Array method reduce() to get one value from the Array. Example:
[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
What is the best way to achieve the same with objects?…

Pavel S.
- 11,892
- 18
- 75
- 113
251
votes
5 answers
How to map/reduce/filter a Set in JavaScript?
Is there any way to map/reduce/filter/etc a Set in JavaScript or will I have to write my own?
Here's some sensible Set.prototype extensions
Set.prototype.map = function map(f) {
var newSet = new Set();
for (var v of this.values())…

Mulan
- 129,518
- 31
- 228
- 259
239
votes
7 answers
NameError: name 'reduce' is not defined in Python
I'm using Python 3.2. Tried this:
xor = lambda x,y: (x+y)%2
l = reduce(xor, [1,2,3,4])
And got the following error:
l = reduce(xor, [1,2,3,4])
NameError: name 'reduce' is not defined
Tried printing reduce into interactive console - got this…

Sergey
- 47,222
- 25
- 87
- 129
202
votes
3 answers
Reduce, fold or scan (Left/Right)?
When should I use reduceLeft, reduceRight, foldLeft, foldRight, scanLeft or scanRight?
I want an intuition/overview of their differences - possibly with some simple examples.

Marc Grue
- 5,865
- 3
- 16
- 23
201
votes
7 answers
Difference between fold and reduce in Kotlin, When to use which?
I am pretty confused with both functions fold() and reduce() in Kotlin, can anyone give me a concrete example that distinguishes both of them?

TapanHP
- 5,969
- 6
- 37
- 66
174
votes
8 answers
What is the 'pythonic' equivalent to the 'fold' function from functional programming?
What is the most idiomatic way to achieve something like the following, in Haskell:
foldl (+) 0 [1,2,3,4,5]
--> 15
Or its equivalent in Ruby:
[1,2,3,4,5].inject(0) {|m,x| m + x}
#> 15
Obviously, Python provides the reduce function, which is an…

mistertim
- 5,123
- 4
- 20
- 27
161
votes
11 answers
JavaScript array .reduce with async/await
Seem to be having some issues incorporating async/await with .reduce(), like so:
const data = await bodies.reduce(async(accum, current, index) => {
const methodName = methods[index]
const method = this[methodName]
if (methodName == 'foo') {
…

benhowdle89
- 36,900
- 69
- 202
- 331
157
votes
17 answers
How to early break reduce() method?
How can I break the iteration of reduce() method?
for:
for (var i = Things.length - 1; i >= 0; i--) {
if(Things[i] <= 0){
break;
}
};
reduce()
Things.reduce(function(memo, current){
if(current <= 0){
//break ???
//return; <-- this…

Julio Marins
- 10,039
- 8
- 48
- 54
142
votes
5 answers
Difference between fold and reduce?
Trying to learn F# but got confused when trying to distinguish between fold and reduce. Fold seems to do the same thing but takes an extra parameter. Is there a legitimate reason for these two functions to exist or they are there to accommodate…

Wallace
- 5,319
- 4
- 17
- 9
138
votes
9 answers
Main difference between map and reduce
I used both methods but I am quite confused regarding the usage of both methods.
Is anything that map can do but reduce can not and vice versa?
Note: I know how to use both methods I am questioning for main difference between these method and when…

Nishant Dixit
- 5,388
- 5
- 17
- 29
119
votes
3 answers
Is inject the same thing as reduce in ruby?
I saw that they were documented together here. Are they the same thing? Why does Ruby have so many aliases (such as map/collect for arrays)? Thanks a lot.

Jacky
- 8,619
- 7
- 36
- 40
110
votes
4 answers
Reduce array to set in Swift
I am trying to reduce an array of objects to a set in Swift and this is my code:
objects.reduce(Set()) { $0.insert($1.URL) }
However, I get an error:
Type of expression is ambiguous without more context.
I do not understand what the…

Banana
- 4,010
- 9
- 33
- 49