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
23
votes
6 answers
Mapping values from two array in Ruby
I'm wondering if there's a way to do what I can do below with Python, in Ruby:
sum = reduce(lambda x, y: x + y, map(lambda x, y: x * y, weights, data))
I have two arrays of equal sizes with the weights and data but I can't seem to find a function…

Tim Trueman
- 1,513
- 3
- 17
- 28
23
votes
13 answers
Sum JavaScript object propertyA values with the same object propertyB in an array of objects
How would one take a JavaScript array of objects, such as
objArr = [
{key:"Mon Sep 23 2013 00:00:00 GMT-0400", val:42},
{key:"Mon Sep 24 2013 00:00:00 GMT-0400", val:78},
{key:"Mon Sep 25 2013 00:00:00 GMT-0400", val:23},
{key:"Mon…

AlecPerkey
- 659
- 2
- 11
- 21
22
votes
2 answers
Is there any real benefit for using javascript Array reduce() method?
Most use cases of the reduce() method can be easily rewritten with a for loop. And testing on JSPerf shows that reduce() is usually 60%-75% slower, depending on the operations performed inside each iteration.
Is there any real reason to use reduce()…

Evan You
- 2,701
- 1
- 24
- 15
22
votes
7 answers
In Stream reduce method, must the identity always be 0 for sum and 1 for multiplication?
I proceed with java 8 learning.
I have found an interesting behavior:
let's see code sample:
// identity value and accumulator and combiner
Integer summaryAge = Person.getPersons().stream()
//.parallel() //will return surprising result
…

gstackoverflow
- 36,709
- 117
- 359
- 710
22
votes
2 answers
how to use reduce with dictionary
I have some problem understanding how to use reduce with dictionaries in python. For example I have the following dictionary.
{1: 3, 2: 1, 3: 2}
and I am trying to calculate the following:
s = 0
for i in h:
s += h[i] * (h[i] - 1)
This works as…

Salvador Dali
- 214,103
- 147
- 703
- 753
22
votes
3 answers
Reduce function with three parameters
How does reduce function work in python3 with three parameters instead of two.
So, for two,
tup = (1,2,3)
reduce(lambda x, y: x+y, tup)
I get this one. This would just sum up all the elements in tup. However, if you give reduce function three…

chanpkr
- 895
- 2
- 10
- 21
21
votes
5 answers
push() won't work as expected in reduce()
Why doesn't a.push(b) work in my Array.reduce()? a=a.push(b) where b is a string, turns a to an integer.?!
getHighestValuesInFrequency: function(frequency) {
//Input:var frequency = {mats: 1,john: 3,johan: 2,jacob: 3};
//Output should become…

Lasse Karagiannis
- 471
- 2
- 6
- 12
20
votes
1 answer
CouchDB Reduce Check Box in Futon
I created a small test database in CouchDB and I'm creating a temporary view in Futon. I wrote the mapper and the reducer. The mapper works, but the check box for the reducer never shows up. I know that there should be a check box because I've seen…

Jason Marcell
- 2,785
- 5
- 28
- 41
20
votes
2 answers
Calling reduce to sum array of objects returns NaN
I have code similar to this:
var temp = [ { "y": 32 }, { "y": 60 }, { "y": 60 } ];
var reduced = temp.reduce(function(a, b) {
return a.y + b.y;
});
console.log(reduced); // Prints NaN
Why does it print NaN instead of 152?

Akhilesh Kumar
- 9,085
- 13
- 57
- 95
20
votes
4 answers
JavaScript array reduce start from index
This problem has been bugging me for a while now and I can't seem to find an answer in web.
Is it possible to use Array reduce method starting from a certain index?
simple example
var studentGrades = ["John Doe", "Some School", 6, 7, 8, 7, 9,…

JanisSt
- 305
- 1
- 2
- 7
20
votes
4 answers
Does OCaml have general map()/reduce() functions?
In Python map() works on any data that follows the sequence protocol. It does The Right Thing^TM whether I feed it a string or a list or even a tuple.
Can't I have my cake in OCaml too? Do I really have no other choice but to look at the…

mbac32768
- 11,453
- 9
- 34
- 40
20
votes
5 answers
String array reduce
I am trying to join the elements of a String array via the reduce function. A tried for a bit now, but I can't get what the problem exactly is. This is what I believe should do the trick. I have tried other alternatives too, but given the huge…

Nicola Miotto
- 3,647
- 2
- 29
- 43
19
votes
6 answers
Java 8: stop reduction operation from examining all Stream elements
I am trying to understand if there is a way to terminate reduction operation without examining the whole stream and I cannot figure out a way.
The use-case is roughly as follows: let there be a long list of Integers which needs to be folded into an…

quantum
- 3,000
- 5
- 41
- 56
17
votes
2 answers
Implement fibonacci in Clojure using map/reduce
Is it possible to implement the fibonacci series in Clojure efficiently using reduce? What would the "accumulator" contain?
I imagine that it will have to be lazy. It's obvious how to do it using recursion or loop/recur.

Ralph
- 31,584
- 38
- 145
- 282
17
votes
6 answers
Simplifying / optimizing a chain of for-loops
I have a chain of for-loops that works on an original list of strings and then gradually filtering the list as it goes down the chain, e.g.:
import re
# Regex to check that a cap exist in string.
pattern1 = re.compile(r'\d.*?[A-Z].*?[a-z]')
vocab =…

alvas
- 115,346
- 109
- 446
- 738