【Note】fp-ts 库函数备忘
"个人关于 fp-ts 的库函数的一些笔记。"
1. chain = flatMap
chain
做的事情(mapping and then flattening):
- 检查是否有值
- 如果有值,则将该值传入函数,并返回一个新的被包装的类型
import * as O from 'fp-ts/Option'
pipe( ns, head, O.map(inverse) O.flatten)
// 等价于pipe( ns, head, O.chain(inverse))
2. fromPredicate
const isEven = (n: number) => n % 2 === 0
const getEven = O.fromPredicate(isEven) // Option<number>
type Shape = Circle | Square
const isCircle = (shape: Shape): shape is Circle => shape.kind === 'circle'
const getCircle = O.fromPredicate(isCircle) // Option<Shape>
【END】