fp-ts函数式编程 - pipe与flow

fp-ts是typescript下的函数式编程框架,抱着了解函数式编程的目的,记录下fp-ts的学习过程。此文为第一部分:pipe与flow
mkdir [项目名称]
cd [项目名称]
npm init --y
npm install typescript fp-ts ts-node --save

新建index.ts, 代码如下

import { pipe, flow } from 'fp-ts/lib/function';

function add10(num: number): number {
    return num+1;
}

function double(num: number): number {
    return num*2;
}

const result = pipe(3, add10, double);
const result2 = pipe(1, add10, double, toString);

flow与pipe类似,区别在于pipe第一个参数是传入的值,而flow的初始化参数是一组函数,再调用时传入数值

pipe(1, flow(add10, double, toString))
flow(add10, double, toString)(1) // 两者相等

参考:
https://dev.to/ryanleecode/practical-guide-to-fp-ts-pipe-and-flow-4e9n

posted @ 2021-10-11 21:35  老胡Andy  阅读(1072)  评论(0编辑  收藏  举报