• [Javascript] Write a function pipeline


    const _pipe = (f, g) => (...args) => g(f(...args))
    
    export const pipe = (...fns) => fns.reduce(_pipe)
    import {partial, pipe} from './utils'
    
    const add = (a, b) => a + b
    const addThree = (a, b, c) => a + b + c
    const inc = (num) => num + 1
    const dbl = (num) => num * 2
    
    test('pipe passes the results of inc to dbl', () => {
      const pipeline = pipe(inc, dbl) // => dbl(inc(2)) OR g(f(...args))
      const result = pipeline(2)
      expect(result).toBe(6)
    })
    
    test('pipe passes the results of dbl to inc', () => {
      const pipeline = pipe(dbl, inc) // => inc(dbl(2))
      const result = pipeline(2)
      expect(result).toBe(5)
    })
    
    test('pipe works with more than 2 functions', () => {
      const pipeline = pipe(add, inc, dbl, inc) // => inc(dbl(inc(add(1,2))))
      const result = pipeline(1,2)
      expect(result).toBe(9)
    })
  • 相关阅读:
    javaweb学习2
    javaweb学习1
    Java学习17
    python 正则表达式
    python 常用库
    python 常用内置函数
    python 函数高级功能
    python 正则表达式findall
    tcpdump
    安卓软件学习进度_1
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6361519.html
Copyright © 2020-2023  润新知