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
1
vote
0 answers
OpenCL Sum reduction across different work-groups gives the wrong result
So I'm currently trying to write a kernel in OpenCL with the goal of sum reducing each row of a matrix (g_idata) into an array (g_odata). Said matrix is represented by a float array with column_count * row_count length, and the resulting array has a…

João Borrego
- 95
- 6
1
vote
2 answers
Sum between array's elements and creation of new array in Cypher language
I need to sum an element of an array with the element before and create a new array with this elements.
In manual way now I am using this code:
match (a:User)
with collect(a.capital) as cap
with cap as cap, length(cap) as len
return cap[0],
…

raf
- 137
- 2
- 12
1
vote
3 answers
Using reduce to group by and sum
I want to return an array grouped by the team with gp, win, loss summed up. I'm trying to accomplish this with reduce, however, the totals are not adding up. Here's my code...
const myArr = [
{team: 'Red', gp: 3, win:2, loss:1},
{team:…

gvon79
- 642
- 3
- 9
- 18
1
vote
2 answers
Is it possible to resize an image by its bytes rather than width and height?
I wanted to reduce an image to a smaller size for easy sharing and faster uploading.
But I realized if I just reduce the size by its h&w, it doesn't really do the trick because a large image file may have a smaller h&w, and small image file may…

moomoochen
- 355
- 1
- 7
- 15
1
vote
4 answers
reduce array of objects
I have an array of objects. I would concatenate items as follow:
obj2 = [{ rowNumber:33, rows:[{item1:'ItemOne'}]}, { rowNumber:44, rows:[{item2:'ItemTwo'}]}]
===> new obj = [{ rowNumber:77, rows:[{item1:'ItemOne'},{item2:'ItemTwo'}]}]
Here's my…

infodev
- 4,673
- 17
- 65
- 138
1
vote
3 answers
Reduce and sum tuples by key
In my Spark Scala application I have an RDD with the following format:
(05/05/2020, (name, 1))
(05/05/2020, (name, 1))
(05/05/2020, (name2, 1))
...
(06/05/2020, (name, 1))
What I want to do is group these elements by date and sum the tuples that…

matrix
- 161
- 2
- 12
1
vote
1 answer
Why does reducing getitem over a nested data structure fail?
Setup: I wanted to write a method that would take a nested data object and a path string, and attempt to use the path components to dereference a location inside the data object.
For example, you'd have a path like /alpha/bravo/0/charlie, and the…

nballenger
- 43
- 1
- 4
1
vote
0 answers
Apply Reduce on matrix
I am trying to apply the Reduce function with | (OR) and accumulate = TRUE on the columns of a matrix.
Example input:
m <- matrix(c(1,0,0,0,1,0,0,0,1), nrow = 3)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
The…

scs
- 567
- 6
- 22
1
vote
2 answers
Javascript: understanding map and reduce applied to a tip calculator
I created a tip calculator using if-else statements as below:
var billRestaurant1 = 124;
var billRestaurant2 = 48;
var billRestaurant3 = 268;
function myTip (bill) {
if (bill < 50) {
var tip = bill * 20 / 100;
return tip;
…

Marco Giuseppe de Pinto
- 605
- 2
- 12
- 29
1
vote
2 answers
abc.filter().map() ==> to reduce() How should I use it? JavaScript
there is an array:
let x = [12,2,3.5,4,-29];
let squared = x.filter((a) => a>0 && Number.isInteger(a)).map((a) => a**2);
please, how to write this using reduce()? The point is -- get squared numbers in given array (only integers), greater than '0'.…

Ievgen S.
- 191
- 2
- 6
1
vote
2 answers
Swift Array Extension to replace value of index n by the sum of the n-previous values
I am trying to write an extension for Array Types that sums the n-previous indexes in the index n.
let myArray = [1, 2, 3, 4, 5]
let mySumArray = myArray.sumNIndex()
print(mySumArray)
// returns [1,3,6,10,15]
I have tried various approaches which…

Chris
- 305
- 1
- 12
1
vote
2 answers
How to create/merge object from splitted string array in TypeScript?
I have an array of objects like below;
const arr1 = [
{"name": "System.Level" },
{"name": "System.Status" },
{"name": "System.Status:*" },
{"name": "System.Status:Rejected" },
{"name": "System.Status:Updated" }
]
I am trying to split name…

hrn
- 111
- 1
- 1
- 12
1
vote
4 answers
javascript reduce in function
I am brand new to javascript and have only an entry level java college course under my belt. I am taking a course online to learn JS and have run into a hiccup. I am supposed to create a function that parses a string and finds the longest word.
I…

Devin Miller
- 179
- 1
- 10
1
vote
1 answer
reduce function does not initialize accumulator to zero?
reduce does not initialize accumulator to zero? Because the example below happens, I can not understand.
const r = (acc, curr) => acc |= (1 << curr);
let x;
x |= (1 << 0);
console.log(x); // -> 1 - OK
console.log([0].reduce(r)); // -> 0 - ???
And…

PerduGames
- 1,108
- 8
- 19
1
vote
2 answers
Using map with reduce in javascript to filter objects in an array
There is a question from freecodecamp and detail are as follows:
Requirement:
Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument).
For…

Pak Hang Leung
- 389
- 5
- 15