fp-ts/function

120 阅读1分钟

flow

执行从左到右的函数组合。第一个参数可以是任意整数,其余参数必须是一元。

const len = (s: string): number => s.length
const double = (n: number): number => n * 2

const f = flow(len, double)

console.log(f('aaa')) // 6

pipe

flow类似,区别就是pipe第一个参数接受一个值,最终返回的是结果,不是函数。

const len = (s: string): number => s.length
const double = (n: number): number => n * 2

const withoutPipe = double(len('aaa'))

console.log(withoutPipe) // 6

const withPipe = pipe('aaa', len, double)

console.log(withPipe) // 6

flow也可以通过pipe去改写

const pipeToFlow = (s: string) => pipe(s, len, double)

console.log(pipeToFlow('sss')) // 6