技能 编程开发 函数式管道速查

函数式管道速查

v20260306
fp-pipe-ref
提供 fp-ts 中 `pipe` 与 `flow` 的速查指南,说明如何直接处理值或组合可复用数据管道,并包括 Option 与 Array 链式操作示例。
获取技能
334 次下载
概览

pipe & flow Quick Reference

pipe - Transform a Value

import { pipe } from 'fp-ts/function'

// pipe(startValue, fn1, fn2, fn3)
// = fn3(fn2(fn1(startValue)))

const result = pipe(
  '  hello world  ',
  s => s.trim(),
  s => s.toUpperCase(),
  s => s.split(' ')
)
// ['HELLO', 'WORLD']

flow - Create Reusable Pipeline

import { flow } from 'fp-ts/function'

// flow(fn1, fn2, fn3) returns a new function
const process = flow(
  (s: string) => s.trim(),
  s => s.toUpperCase(),
  s => s.split(' ')
)

process('  hello world  ') // ['HELLO', 'WORLD']
process('  foo bar  ')     // ['FOO', 'BAR']

When to Use

Use When
pipe Transform a specific value now
flow Create reusable transformation

With fp-ts Types

import * as O from 'fp-ts/Option'
import * as A from 'fp-ts/Array'

// Option chain
pipe(
  O.fromNullable(user),
  O.map(u => u.email),
  O.getOrElse(() => 'no email')
)

// Array chain
pipe(
  users,
  A.filter(u => u.active),
  A.map(u => u.name)
)

Common Pattern

// Data last enables partial application
const getActiveNames = flow(
  A.filter((u: User) => u.active),
  A.map(u => u.name)
)

// Reuse anywhere
getActiveNames(users1)
getActiveNames(users2)
信息
Category 编程开发
Name fp-pipe-ref
版本 v20260306
大小 1.5KB
更新时间 2026-03-07
语言