Javascript 不反转阵列的ReduceRight

Javascript 不反转阵列的ReduceRight,javascript,arrays,ecmascript-6,Javascript,Arrays,Ecmascript 6,我已经为JavaScript编写了reduce本机实现。但我想看看我们是否能用这个reduce实现ReduceRight Array.prototype.myReduce = function(cb, initialVal) { if (!cb) throw new Error("No CB defined"); let [accumulator, ...arr] = initialVal === undefined ? [...this] : [initialVal, ...

我已经为JavaScript编写了reduce本机实现。但我想看看我们是否能用这个reduce实现ReduceRight

Array.prototype.myReduce = function(cb, initialVal) {
  if (!cb)
    throw new Error("No CB defined");

  let [accumulator, ...arr] = initialVal === undefined ? [...this] : [initialVal, ...this];

  for (var i = 0; i < this.length; i++) {
    accumulator = cb.call(undefined, accumulator, arr[i], i, arr);
  }

  return accumulator;
}

下面是我在个人库中使用的
reduceRight
的实现

代码:

Array.prototype.myReduceRight=函数(回调,initialValue){
变量
/*缓存上下文的长度*/
长度=此。长度>>>0,
/*在上下文中最后一个元素的索引处创建计数器默认值*/
计数器=长度-1;
/*检查是否给出了第二个参数*/
if(arguments.length<2){
/*重复此操作,直到计数器小于或等于计数器的长度
或当计数器作为索引存在于上下文中时*/
while(计数器>=0&!(此计数器中的计数器)){
/*减小计数器的数值*/
计数器--;
}
/*设置初始值*/
initialValue=此[计数器--];
}
/*重复此操作,直到计数器小于0*/
而(计数器>=0){
/*检查计数器是否作为索引存在于上下文中*/
如果(本节中的计数器){
/*将初始值设置为给定回调的返回值*/
initialValue=callback.call(this,initialValue,this[counter],counter,this);
}
/*减小计数器的数值*/
计数器--;
}
/*返回计算值*/
返回初始值;
}
常量数组=[1,2,3,4];

console.log(array.myReduceRight((acc,cur)=>acc+cur))以下是我在个人库中使用的
reduceRight
的实现

代码:

Array.prototype.myReduceRight=函数(回调,initialValue){
变量
/*缓存上下文的长度*/
长度=此。长度>>>0,
/*在上下文中最后一个元素的索引处创建计数器默认值*/
计数器=长度-1;
/*检查是否给出了第二个参数*/
if(arguments.length<2){
/*重复此操作,直到计数器小于或等于计数器的长度
或当计数器作为索引存在于上下文中时*/
while(计数器>=0&!(此计数器中的计数器)){
/*减小计数器的数值*/
计数器--;
}
/*设置初始值*/
initialValue=此[计数器--];
}
/*重复此操作,直到计数器小于0*/
而(计数器>=0){
/*检查计数器是否作为索引存在于上下文中*/
如果(本节中的计数器){
/*将初始值设置为给定回调的返回值*/
initialValue=callback.call(this,initialValue,this[counter],counter,this);
}
/*减小计数器的数值*/
计数器--;
}
/*返回计算值*/
返回初始值;
}
常量数组=[1,2,3,4];

console.log(array.myReduceRight((acc,cur)=>acc+cur))在函数式编程中,
reduce
aka
foldl
这样减少

foldl (fn, [head:tail], acc) = foldl (fn, tail, fn(acc, head))
这是什么

foldr (fn, [head:tail], acc) = fn (head, foldr(fn, tail, acc))
而JS
reduceRight
的参数是相反的:

reduceRight (fn, [head:tail], acc) = fn (reduceRight(fn, tail, acc), head)
这三种语言都使用javascript:

设nil=x=>x==nil;
让reduce=(fn,[h=nil,…t],a)=>nil(h)?a:减少(fn,t,fn(a,h));
设foldr=(fn,[h=nil,…t],a)=>nil(h)?a:fn(h,foldr(fn,t,a));
设reduceRight=(fn,[h=nil,…t],a)=>nil(h)?a:fn(还原右(fn,t,a),h);
////
xs=[1,2,3,4]
列表=(a,b)=>`(${a}+${b})`;
log(reduce(list,xs',);
log(foldr(list,xs',);

log(reduceRight(list,xs',)在函数式编程中,
reduce
aka
foldl
这样减少

foldl (fn, [head:tail], acc) = foldl (fn, tail, fn(acc, head))
这是什么

foldr (fn, [head:tail], acc) = fn (head, foldr(fn, tail, acc))
而JS
reduceRight
的参数是相反的:

reduceRight (fn, [head:tail], acc) = fn (reduceRight(fn, tail, acc), head)
这三种语言都使用javascript:

设nil=x=>x==nil;
让reduce=(fn,[h=nil,…t],a)=>nil(h)?a:减少(fn,t,fn(a,h));
设foldr=(fn,[h=nil,…t],a)=>nil(h)?a:fn(h,foldr(fn,t,a));
设reduceRight=(fn,[h=nil,…t],a)=>nil(h)?a:fn(还原右(fn,t,a),h);
////
xs=[1,2,3,4]
列表=(a,b)=>`(${a}+${b})`;
log(reduce(list,xs',);
log(foldr(list,xs',);
log(reduceRight(list,xs',)是一种还原性右侧多填料:

在解决方案中,它使用递减而不是递增的循环

// Production steps of ECMA-262, Edition 5, 15.4.4.22
// Reference: http://es5.github.io/#x15.4.4.22
if ('function' !== typeof Array.prototype.reduceRight) {
  Array.prototype.reduceRight = function(callback /*, initialValue*/) {
    'use strict';
    if (null === this || 'undefined' === typeof this) {
      throw new TypeError('Array.prototype.reduce called on null or undefined');
    }
    if ('function' !== typeof callback) {
      throw new TypeError(callback + ' is not a function');
    }
    var t = Object(this), len = t.length >>> 0, k = len - 1, value;
    if (arguments.length >= 2) {
      value = arguments[1];
    } else {
      while (k >= 0 && !(k in t)) {
        k--;
      }
      if (k < 0) {
        throw new TypeError('Reduce of empty array with no initial value');
      }
      value = t[k--];
    }
    for (; k >= 0; k--) {
      if (k in t) {
        value = callback(value, t[k], k, t);
      }
    }
    return value;
  };
}
//ECMA-262第5版15.4.4.22的生产步骤
//参考:http://es5.github.io/#x15.4.4.22
if('function'!==typeof Array.prototype.reduceRight){
Array.prototype.reduceRight=函数(callback/*,initialValue*/){
"严格使用",;
if(null==此| |“未定义”==此的类型){
抛出新的TypeError('Array.prototype.reduce在null或未定义时被调用');
}
if('function'!==回调类型){
抛出新类型错误(回调+'不是函数');
}
var t=Object(this),len=t.length>>>0,k=len-1,value;
如果(arguments.length>=2){
值=参数[1];
}否则{
而(k>=0&!(k in t)){
k--;
}
if(k<0){
抛出新的TypeError(“减少没有初始值的空数组”);
}
值=t[k--];
}
对于(;k>=0;k--){
if(k in t){
值=回调(值,t[k],k,t);
}
}
返回值;
};
}
是一种轻量聚填料:

在解决方案中,它使用递减而不是递增的循环

// Production steps of ECMA-262, Edition 5, 15.4.4.22
// Reference: http://es5.github.io/#x15.4.4.22
if ('function' !== typeof Array.prototype.reduceRight) {
  Array.prototype.reduceRight = function(callback /*, initialValue*/) {
    'use strict';
    if (null === this || 'undefined' === typeof this) {
      throw new TypeError('Array.prototype.reduce called on null or undefined');
    }
    if ('function' !== typeof callback) {
      throw new TypeError(callback + ' is not a function');
    }
    var t = Object(this), len = t.length >>> 0, k = len - 1, value;
    if (arguments.length >= 2) {
      value = arguments[1];
    } else {
      while (k >= 0 && !(k in t)) {
        k--;
      }
      if (k < 0) {
        throw new TypeError('Reduce of empty array with no initial value');
      }
      value = t[k--];
    }
    for (; k >= 0; k--) {
      if (k in t) {
        value = callback(value, t[k], k, t);
      }
    }
    return value;
  };
}
//ECMA-262第5版15.4.4.22的生产步骤
//参考:http://es5.github.io/#x15.4.4.22
if('function'!==typeof Array.prototype.reduceRight){
Array.prototype.reduceRight=函数(callback/*,initialValue*/){
"严格使用",;
if(null==此| |“未定义”==此的类型){
抛出新的TypeError('Array.prototype.reduce在null或未定义时被调用');
}
if('function'!==回调类型){
抛出新类型错误(回调+”不是fu