博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
01 - Execise About Array.prototype.reduce()
阅读量:4957 次
发布时间:2019-06-12

本文共 2376 字,大约阅读时间需要 7 分钟。

Description:

Write a generic function chainer

Write a generic function chainer that takes a starting value, and an array of functions to execute on it (array of symbols for ruby).

The input for each function is the output of the previous function (except the first function, which takes the starting value as it's input). Return the final value after execution is complete.

1 function add(num) { 2   return num + 1 3 } 4  5 function mult(num) { 6   return num * 30 7 } 8  9 chain(2, [add, mult]);10 // returns 90;

 

我的答案是:

function chain(input, fs) {  var result = input;  for(var i = 0; i < fs.length; i++) {    result = fs[i](result);  }  return result;}

 

 最优解是:

function chain(v, fns) {  return fns.reduce(function(v, fn) { return fn(v) }, v);}

 

然后引发了对reduce方法的关注,在MDN中

Array.prototype.reduce()的语法如下:arr.reduce(callback[,initialValue]);

callback
Function to execute on each value in the array, taking four arguments:
previousValue
The value previously returned in the last invocation of the callback, or
 initialValue, if supplied. (See below.)
currentValue
The current element being processed in the array.
currentIndex
The index of the current element being processed in the array.
array
The array
 reduce was called upon.
initialValue
Optional. Value to use as the first argument to the first call of the
 callback.

Example:

 

[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, currentIndex, array) {  return previousValue + currentValue;});

The callback would be invoked four times, with the arguments and return values in each call being as follows:

  previousValue currentValue currentIndex arr return value
first call 0 1 1 [0, 1, 2, 3, 4] 1
second call 1 2 2 [0, 1, 2, 3, 4] 3
third call 2 3 3 [0, 1, 2, 3, 4] 6
fourth call 6 4 4 [0, 1, 2, 3, 4] 10

 

If you were to provide an initial value as the second argument to reduce, the result would look like this:

[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, currentIndex, array) {  return previousValue + currentValue;}, 10);

 

  previousValue currentValue currentIndex array return value
first call 10 0 0 [0, 1, 2, 3, 4] 10
second call 10 1 1 [0, 1, 2, 3, 4] 11
third call 11 2 2 [0, 1, 2, 3, 4] 13
fourth call 13 3 3 [0, 1, 2, 3, 4] 16
fifth call 16 4 4 [0, 1, 2, 3, 4] 20

The final call return value (20) is returned as a result of the reduce function.

 

 

 

转载于:https://www.cnblogs.com/qq1105151861/p/5428705.html

你可能感兴趣的文章
SQL Server-5种常见的约束
查看>>
硬件之美
查看>>
[转载]java开发中的23种设计模式
查看>>
表格的拖拽功能
查看>>
函数的形参和实参
查看>>
文字过长 用 ... 表示 CSS实现单行、多行文本溢出显示省略号
查看>>
1Caesar加密
查看>>
【TP SRM 703 div2 500】 GCDGraph
查看>>
MapReduce 重要组件——Recordreader组件 [转]
查看>>
webdriver api
查看>>
apache 实现图标缓存客户端
查看>>
揭秘:黑客必备的Kali Linux是什么,有哪些弊端?
查看>>
linux系统的远程控制方法——学神IT教育
查看>>
springboot+mybatis报错Invalid bound statement (not found)
查看>>
Linux环境下SolrCloud集群环境搭建关键步骤
查看>>
P3565 [POI2014]HOT-Hotels
查看>>
MongoDB的简单使用
查看>>
hdfs 命令使用
查看>>
prometheus配置
查看>>
【noip2004】虫食算——剪枝DFS
查看>>