码迷,mamicode.com
首页 > 编程语言 > 详细

[Javascript] Advanced Reduce: Composing Functions with Reduce

时间:2016-01-15 06:21:17      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

 Learn how to use array reduction to create functional pipelines by composing arrays of functions.

 

const increase = (input) => {
  return input + 1;
}

const decrease = (input) => {
  return input - 1;
}

const double = (input) => {
  return input * 2;
}

const halven = (input) => {
  return input / 2;
}

let pipelines = [
  increase,
  increase,
  decrease,
  double,
  halven,
  increase
];

let init_value = 1;

let res = pipelines.reduce( (acc, fn) => {
  return fn(acc);
}, init_value );

console.log(res);

 

[Javascript] Advanced Reduce: Composing Functions with Reduce

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5132164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!