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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM