Skip to main content
← Gists
typescript Jun 15, 2024

Pipe and Flow Composition with Effect

Composing operations left-to-right using Effect's pipe and flow utilities for readable data transformations.

Effect provides pipe and flow as first-class composition primitives. They replace deeply nested function calls with a readable, top-to-bottom pipeline.

pipe: apply value through functions

pipe.ts
import { pipe } from "effect"

const result = pipe(
" Hello, World! ",
(s) => s.trim(),
(s) => s.toLowerCase(),
(s) => s.replace(/\s+/g, "-"),
(s) => s.replace(/[^a-z0-9-]/g, ""),
)
// result: "hello-world"

pipe takes an initial value and threads it through each function. Each step receives the output of the previous one.

flow: compose functions without a value

flow.ts
import { flow } from "effect"

const slugify = flow(
(s: string) => s.trim(),
(s) => s.toLowerCase(),
(s) => s.replace(/\s+/g, "-"),
(s) => s.replace(/[^a-z0-9-]/g, ""),
)

slugify(" Hello, World! ")  // "hello-world"
slugify("  Foo  Bar  ")     // "foo--bar"

flow creates a reusable function without immediately applying it. Use pipe when you have a value; use flow when you want a composable function.