使用 ECMASCRIPT 6 Generator/Functions 对数组求和的最佳方法是什么
What is the best way to sum arrays using ECMASCRIPT 6 Generator/Functions
有没有更好的方法来代替使用生成器函数作为闭包来增加数组的值?
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}
这似乎不是生成器应该解决的问题,所以我不会在这里使用生成器。
直接使用reduce
(ES5)似乎更合适:
let sum = [1,2,3,4].reduce((sum, x) => sum + x);
作为函数:
function sum(arr) {
return arr.reduce((sum, x) => sum + x);
}
如果你真的想在多个函数调用中对多个数组求和,那么return一个普通函数:
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
保持简单。
这里使用了for/of循环和箭头函数,
const sumArray = myArray => {
let sum = 0;
for(const value of myArray)
sum+= value;
return sum;
}
const myArray = [1, 2, 5];
console.log(sumArray(myArray));
有没有更好的方法来代替使用生成器函数作为闭包来增加数组的值?
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}
这似乎不是生成器应该解决的问题,所以我不会在这里使用生成器。
直接使用reduce
(ES5)似乎更合适:
let sum = [1,2,3,4].reduce((sum, x) => sum + x);
作为函数:
function sum(arr) {
return arr.reduce((sum, x) => sum + x);
}
如果你真的想在多个函数调用中对多个数组求和,那么return一个普通函数:
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
保持简单。
这里使用了for/of循环和箭头函数,
const sumArray = myArray => {
let sum = 0;
for(const value of myArray)
sum+= value;
return sum;
}
const myArray = [1, 2, 5];
console.log(sumArray(myArray));