首页 > 代码库 > [Javascript] Write a function pipeline
[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 + bconst addThree = (a, b, c) => a + b + cconst inc = (num) => num + 1const dbl = (num) => num * 2test(‘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)})
[Javascript] Write a function pipeline
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。