[Javascript] Write a function pipeline

來源:互聯網
上載者:User

標籤:asc   utils   code   imp   work   blog   with   from   style   

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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.