zoukankan      html  css  js  c++  java
  • [Javascript] Advanced Reduce: Composing Functions with Reduce

     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);
  • 相关阅读:
    Android随笔
    Android随笔
    阅读笔记
    Android随笔
    Android随笔
    Android随笔
    Android随笔
    Java随笔
    Android随笔
    NC20265 着色方案(dp)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5132164.html
Copyright © 2011-2022 走看看