What is the best way to sum arrays using ECMASCRIPT 6 Generator/Functions

Blauharley

Is there a better way instead of adding values of arrays up using a generator function as closure?

var sumArrays = function(){
  var sum = 0;
  return function*(){
    while(true){
      var array = yield sum;
      if(array.__proto__.constructor === Array){
        sum += array.reduce(function(val,val2){ return val+val2; });
      }
      else sum=0;
    }
  };
};

var gen = sumArrays();
// is this step required to make a generator or could it be done at least differently to spare yourself from doing this step?
gen = gen();

// sum some values of arrays up
console.log('sum: ',gen.next()); // Object { value=0,  done=false}
console.log('sum: ',gen.next([1,2,3,4])); // Object { value=10,  done=false}
console.log('sum: ',gen.next([6,7])); // Object { value=23,  done=false}

// reset values
console.log('sum: ',gen.next(false)); // Object { value=0,  done=false}
console.log('sum: ',gen.next([5])); // Object { value=5,  done=false}
Felix Kling

This doesn't seem to be a problem generators are supposed to solve, so I would not use a generator here.

Directly using reduce (ES5) seems to be more appropriate:

let sum = [1,2,3,4].reduce((sum, x) => sum + x);

As a function:

function sum(arr) {
  return arr.reduce((sum, x) => sum + x);
}

If you really want to sum multiple arrays across multiple function calls, then return a normal function:

function getArraySummation() {
  let total = 0;
  let reducer = (sum, x) => sum + x;
  return arr => total + arr.reduce(reducer);
}

let sum = getArraySummation();
console.log('sum:', sum([1,2,3])); // sum: 6
console.log('sum:', sum([4,5,6])); // sum: 15

Keep it simple.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What's the best way to document an array of arrays of arrays using JSDoc?

What's the best way to initialize an object with defaults where values are omitted in Ecmascript 6?

What is the best way of using Arrays.asList() to initialize a List

What is the best way to combine two arrays using javascript?

What is the most stable way to use React, ECMAscript 6, and Gulp?

ECMAScript 6: what is WeakSet for?

What is the best way to merge 2 byte arrays?

What is the best way to merge nested arrays

What is the "best" way to get 3 different SUM using a single query in MYSQL

What’s the best way to iterate 2 arrays correspondingly when using numpy?

What is the best way to limit concurrency when using ES6's Promise.all()?

What is the best way to render a search bar component that takes in a props in another component using react v6

what is the best way to join two tables and use sum function

What's the best way to sum all values in a Pandas dataframe?

What is the best way to transfer a file using Java?

What is the best way of using two comparators?

What is the best way to refresh a list using Firebase

What is the best way to implement this using Qt

What is the best way for pagination on mongodb using java

What is the best way to do this parsing using streams?

What is the best way to parse a line using Java?

What is the best way of using database host?

What is the best way to implement a DEMUX using VHDL?

What's the best way to using nosql in this situation?

What is the best way to "merge" 2 arrays by each value?

What is a best way to intersect multiple arrays with numpy array?

What is the best way to iteratively instantiate a class from a dictionary of lists/arrays?

What is the best way to label user-inputted arrays?

What is the best way to hold multiple 3D arrays in SQL?