Questions tagged [reduce]

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.

Documentation in respective languages:

3548 questions
1
vote
1 answer

Stream API reduce used on ArrayList not synchronized

I am using stream API reduce to test a array list of String. for (int i = 0; i < 100; i++) { Stream s1 = Stream.of("aa", "ab", "c", "ad"); Predicate predicate = t -> t.contains("a"); List strings2…
Robin Sun
  • 45
  • 7
1
vote
1 answer

Understanding fold and reduce functions in scheme

I have generic question about scheme and lisp. How fold and reduce functions should work? In guile scheme using (use-modules (srfi srfi-1)) you can use this: guile> (fold cons '() '(1 2 3 4)) > (4 3 2 1) guile> (fold cons '(1 2 3 4) '()) > (1 2 3…
jcubic
  • 61,973
  • 54
  • 229
  • 402
1
vote
1 answer

How to array map instead of nested for loops

For my scenario, I need to push elements to an addresses array which contains objects. I'm working with vue.js. My current working function is: propagateCustomerInfo(selectedOption, id){ // Propagate addresses this.addresses =…
Luciano
  • 2,052
  • 1
  • 16
  • 26
1
vote
1 answer

How to group documents by a field and list unequal values with custom reduce function with CouchDB view

I have 1000's of staff documents in CouchDB, which in a simplified format look like so: { "_id": "1111", "departmentId": "dept_A", "siteId": "SITE_1" } { "_id": "2222", "departmentId": "dept_B", "siteId": "SITE_1" } { …
Super Hans
  • 128
  • 2
  • 11
1
vote
7 answers

Sum a negative number's digits

'Write a function named sumDigits which takes a number as input and returns the sum of each of the number's decimal digits.' How can I sum the digits with the first digit being negative? For example: sumDigits(-32); // -3 + 2 = -1; I was able…
pasha
  • 51
  • 6
1
vote
4 answers

How to initialize an object variable with 2 "steps": `objectVariable[step1][step2] = result;`

Original problem: I'm trying to create a new object, but the insert is a bit complex: I am generating an object inside an object. result = Object.values(window.datas).reduce( (newObj, dataRow) => { if ( (user.user_id == dataRow.user_id) &&…
1
vote
1 answer

Combine a Reduce and Map Data Structure in Javascript?

I am trying to use the reduce pattern on a Javascript Array of objects. But, instead of doing this in a single reducer, I want to be able to use a different reducer based on a condition set for each of the items in the array. So, it would look like…
svsav
  • 828
  • 3
  • 12
  • 29
1
vote
0 answers

R: how to Reduce() list from replicate() to matrices binding according to lowest-level element

I have used replicate() to produce a list of n_iter elements. A minimal working example with hacked code follows. I seek to produce the same output more efficiently, perhaps with Reduce() and do.call('rbind', list). I begin with a list of n_iter…
1
vote
1 answer

How to fix reducer types definition? (TS2345)

I have function that traverse array and find most likely pairs of id and differ characters counter. Incorrect values is returning as null and after mapping function i filter all of these nulls. export const findLikelyPairsId = (testingId: string,…
anton zlydenko
  • 265
  • 1
  • 2
  • 6
1
vote
4 answers

Creating key-value pairs with js reduce() and creating an array as the value

I am having a problem with javascript's reduce() function; I have to put arrays as the value. I can sucessfully create an array but can't add new values to it. There's an array with words and I have to create a 'map' whose keys are the first letters…
cyberkitty
  • 37
  • 1
  • 9
1
vote
4 answers

How do I add sums per group in two-dimensional array using enumerable functions?

I have a two-dimensional array as follows: ary = [["a", 10],["a", 20],["b", 9],["b",7],["c",12]] I want to sum the numeric values with the same key, building a hash like: desired_result = {"a"=>30, "b"=>16, "c"=>12} I can use a hash with a default…
1
vote
1 answer

Javascript .reduce Boolean Unexpected Return

Trying to create a function to check for a tall number (i.e., every digit is less than or equal to the digit to its right, such as 123, 059, etc.). Following code is the issue: const n = parseInt(readline()); if (n.toString().length === 1) …
Arcaster
  • 119
  • 8
1
vote
2 answers

PySpark Dataframe cast two columns into new column of tuples based value of a third column

As the subject describes, I have a PySpark Dataframe that I need to cast two columns into a new column that is a list of tuples based the value of a third column. This cast will reduce or flatten the dataframe by a key value, product id in this…
Gary C
  • 93
  • 1
  • 1
  • 5
1
vote
3 answers

Use ES6 reduce or map to compute sums and percentages in nested objects of an Array

Apologies if the title is not particularly clear. I have made a short reproducible example below of what I'm attempting to do: var arrayOfPeopleStats = [ person1 = { this: { makes: 10, attempts: 15, pct: .666667 }, that: { makes: 12,…
Canovice
  • 9,012
  • 22
  • 93
  • 211
1
vote
1 answer

Complex CouchDb View

I have the following documents in a database: { id: 1, text: 'Hello I had a big grannysmith apple today and a big pear' }, { id: 2, text: 'Hello I had a big apple today only' }, { id: 3, text: 'Hello I had a big apple today, a big pear…
newstoall
  • 13
  • 2