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
102
votes
2 answers
Where is the "Fold" LINQ Extension Method?
I found in MSDN's Linq samples a neat method called Fold() that I want to use. Their example:
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
double product =
doubles.Fold((runningProduct, nextFactor) => runningProduct * nextFactor);…

Ken
- 5,337
- 3
- 28
- 19
98
votes
4 answers
Difference between reduce and foldLeft/fold in functional programming (particularly Scala and Scala APIs)?
Why do Scala and frameworks like Spark and Scalding have both reduce and foldLeft? So then what's the difference between reduce and fold?

samthebest
- 30,803
- 25
- 102
- 142
85
votes
8 answers
When do reduce tasks start in Hadoop?
In Hadoop when do reduce tasks start? Do they start after a certain percentage (threshold) of mappers complete? If so, is this threshold fixed? What kind of threshold is typically used?

Slayer
- 2,391
- 4
- 21
- 18
79
votes
7 answers
Why does TypeScript infer the 'never' type when reducing an Array with concat?
Code speaks better than language, so:
['a', 'b', 'c'].reduce((accumulator, value) => accumulator.concat(value), []);
The code is very silly and returns a copied Array...
TS complains on concat's argument: TS2345: Argument of type 'string' is not…

millsp
- 1,259
- 1
- 10
- 23
74
votes
6 answers
Using the reduce function to return an array
Why is it that when I want to use the push function inside the reduce function to return a new array I get an error. However, when I use the concat method inside the reduce function, it returns a new array with no problem.
All I'm trying to do is…

2K01B5
- 1,011
- 1
- 10
- 23
55
votes
2 answers
Why Array.prototype.reduce() is not taking an empty array as accumulator?
I am trying to filter all the elements in an array which are bigger than 10 to a new array. I am intentionally not using Array.prototype.filter() since I want to learn the reduce() method. Here is the code I was playing with
var collection = [3,…

segmentationfaulter
- 1,045
- 2
- 12
- 13
52
votes
1 answer
Scala : fold vs foldLeft
I am trying to understand how fold and foldLeft and the respective reduce and reduceLeft work. I used fold and foldLeft as my example
scala> val r = List((ArrayBuffer(1, 2, 3, 4),10))
scala> r.foldLeft(ArrayBuffer(1,2,4,5))((x,y) => x --…

thlim
- 2,908
- 3
- 34
- 57
50
votes
9 answers
python histogram one-liner
There are many ways to write a Python program that computes a histogram.
By histogram, I mean a function that counts the occurrence of objects in an iterable and outputs the counts in a dictionary. For example:
>>> L = 'abracadabra'
>>>…

mykhal
- 19,175
- 11
- 72
- 80
48
votes
7 answers
How does reduce_sum() work in tensorflow?
I am learning tensorflow, I picked up the following code from the tensorflow website. According to my understanding, axis=0 is for rows and axis=1 is for columns.
How are they getting output mentioned in comments? I have mentioned output according…

Bhaskar Dhariyal
- 1,343
- 2
- 13
- 31
46
votes
16 answers
Using reduce() to find min and max values?
I have this code for a class where I'm supposed to use the reduce() method to find the min and max values in an array. However, we are required to use only a single call to reduce. The return array should be of size 2, but I know that the reduce()…

Alyssa June
- 635
- 2
- 6
- 11
46
votes
4 answers
How to call reduce on an empty Kotlin array?
Simple reduce on an empty array will throw:
Exception in thread "main" java.lang.UnsupportedOperationException: Empty iterable can't be reduced.
The same exception when chaining:
val a = intArrayOf()
val b = a.reduce({ memo, next -> memo + next })…

dippe
- 561
- 1
- 4
- 4
45
votes
14 answers
Removing duplicate objects (based on multiple keys) from array
Assuming an array of objects as follows:
const listOfTags = [
{id: 1, label: "Hello", color: "red", sorting: 0},
{id: 2, label: "World", color: "green", sorting: 1},
{id: 3, label: "Hello", color: "blue", sorting: 4},
{id: 4, label:…

Andy
- 549
- 1
- 6
- 12
43
votes
9 answers
How does reduce function work?
As far as I understand, the reduce function takes a list l and a function f. Then, it calls the function f on first two elements of the list and then repeatedly calls the function f with the next list element and the previous result.
So, I define…

Divya
- 2,594
- 1
- 17
- 29
42
votes
5 answers
Count the number of true members in an array of boolean values
New to javascript and I'm having trouble counting the number of trues in an array of boolean values. I'm trying to use the reduce() function. Can someone tell me what I'm doing wrong?
//trying to count the number of true in an array
myCount…

gitmole
- 421
- 1
- 4
- 3
41
votes
6 answers
Reduce Hash Values
I am having trouble with the syntax for reduce. I have a hash of the following format:
H = {"Key1" => 1, "Key2" => 2}
I would like to use reduce to find the sum of the values in this function.
Something Like
H.reduce(0) {|memo, elem|…

richsoni
- 4,188
- 8
- 34
- 47