| name | effect-patterns-streams-getting-started |
| description | Effect-TS patterns for Streams Getting Started. Use when working with streams getting started in Effect-TS applications. |
Effect-TS Patterns: Streams Getting Started
This skill provides 4 curated Effect-TS patterns for streams getting started.
Use this skill when working on tasks related to:
- streams getting started
- Best practices in Effect-TS applications
- Real-world patterns and solutions
🟢 Beginner Patterns
Your First Stream
Rule: Use Stream to process sequences of data lazily and efficiently.
Good Example:
import { Effect, Stream } from "effect"
const numbers = Stream.make(1, 2, 3, 4, 5)
const fromArray = Stream.fromIterable([10, 20, 30])
const single = Stream.succeed("hello")
const program = numbers.pipe(
Stream.map((n) => n * 2),
Stream.filter((n) => n > 4),
Stream.runCollect
)
Effect.runPromise(program).then((chunk) => {
console.log([...chunk])
})
Anti-Pattern:
Don't use regular arrays when you need lazy processing or async operations:
const numbers = [1, 2, 3, 4, 5]
const doubled = numbers.map((n) => n * 2)
const filtered = doubled.filter((n) => n > 4)
This loads everything into memory immediately. Use Stream when:
- Data is large or potentially infinite
- Data arrives asynchronously
- You need backpressure or resource management
Rationale:
A Stream is a lazy sequence of values that can be processed one at a time. Create streams with Stream.make, Stream.fromIterable, or Stream.succeed.
Streams are Effect's answer to processing sequences of data. Unlike arrays which hold all values in memory at once, streams produce values on demand. This makes them ideal for:
- Large datasets - Process millions of records without loading everything into memory
- Async data - Handle data that arrives over time (files, APIs, events)
- Composable pipelines - Chain transformations that work element by element
Stream vs Effect - When to Use Which
Rule: Use Effect for single values, Stream for sequences of values.
Good Example:
import { Effect, Stream } from "effect"
const fetchUser = (id: string) =>
Effect.tryPromise(() =>
fetch(`/api/users/${id}`).then((r) => r.json())
)
const loadConfig = Effect.tryPromise(() =>
fetch("/config.json").then((r) => r.json())
)
const fileLines = Stream.fromIterable([
"line 1",
"line 2",
"line 3",
])
const events = Stream.make(
{ type: "click", x: 10 },
{ type: "click", x: 20 },
{ type: "scroll", y: 100 },
)
const effectToStream = Stream.fromEffect(fetchUser("123"))
const streamToEffect = Stream.runCollect(fileLines)
const processAll = fileLines.pipe(
Stream.runForEach((line) => Effect.log(`Processing: ${line}`))
)
Rationale:
Use Effect when your operation produces a single result. Use Stream when your operation produces multiple values over time.
Both Effect and Stream are lazy and composable, but they serve different purposes:
| Aspect | Effect | Stream |
|---|
| Produces | One value | Zero or more values |
| Memory | Holds one result | Processes incrementally |
| Use case | API call, DB query | File lines, events, batches |
Running and Collecting Stream Results
Rule: Choose the right Stream.run* method based on what you need from the results.
Good Example:
import { Effect, Stream, Option } from "effect"
const numbers = Stream.make(1, 2, 3, 4, 5)
const collectAll = numbers.pipe(
Stream.map((n) => n * 10),
Stream.runCollect
)
Effect.runPromise(collectAll).then((chunk) => {
console.log([...chunk])
})
const processEach = numbers.pipe(
Stream.runForEach((n) =>
Effect.log(`Processing: ${n}`)
)
)
Effect.runPromise(processEach)
const withSideEffects = numbers.pipe(
Stream.tap((n) => Effect.log(`Saw: ${n}`)),
Stream.runDrain
)
const getFirst = numbers.pipe(
Stream.runHead
)
Effect.runPromise(getFirst).then((option) => {
if (Option.isSome(option)) {
console.log(`First: ${option.value}`)
}
})
const getLast = numbers.pipe(
Stream.runLast
)
Effect.runPromise(getLast).then((option) => {
if (Option.isSome(option)) {
console.log(`Last: ${option.value}`)
}
})
const sum = numbers.pipe(
Stream.runFold(0, (acc, n) => acc + n)
)
Effect.runPromise(sum).then((total) => {
console.log(`Sum: ${total}`)
})
const count = numbers.pipe(Stream.runCount)
Effect.runPromise(count).then((n) => {
console.log(`Count: ${n}`)
})
Rationale:
Streams are lazy - nothing happens until you run them. Choose your run method based on what you need: all results, per-item effects, or just completion.
Effect provides several ways to consume a stream, each optimized for different use cases:
| Method | Returns | Use When |
|---|
| runCollect | Chunk<A> | Need all results in memory |
| runForEach | void | Process each item for side effects |
| runDrain | void | Run for side effects, ignore values |
| runHead | Option<A> | Only need first value |
| runLast | Option<A> | Only need last value |
| runFold | S | Accumulate into single result |
Take and Drop Stream Elements
Rule: Use take/drop to control stream size, takeWhile/dropWhile for conditional limits.
Good Example:
import { Effect, Stream } from "effect"
const numbers = Stream.make(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
const firstThree = numbers.pipe(
Stream.take(3),
Stream.runCollect
)
Effect.runPromise(firstThree).then((chunk) => {
console.log([...chunk])
})
const skipThree = numbers.pipe(
Stream.drop(3),
Stream.runCollect
)
Effect.runPromise(skipThree).then((chunk) => {
console.log([...chunk])
})
const page2 = numbers.pipe(
Stream.drop(3),
Stream.take(3),
Stream.runCollect
)
Effect.runPromise(page2).then((chunk) => {
console.log([...chunk])
})
const untilFive = numbers.pipe(
Stream.takeWhile((n) => n < 5),
Stream.runCollect
)
Effect.runPromise(untilFive).then((chunk) => {
console.log([...chunk])
})
const afterFive = numbers.pipe(
Stream.dropWhile((n) => n < 5),
Stream.runCollect
)
Effect.runPromise(afterFive).then((chunk) => {
console.log([...chunk])
})
const untilSix = numbers.pipe(
Stream.takeUntil((n) => n === 6),
Stream.runCollect
)
Effect.runPromise(untilSix).then((chunk) => {
console.log([...chunk])
})
const fileLines = Stream.make(
"# Header",
"# Comment",
"data1",
"data2",
"data3"
)
const dataOnly = fileLines.pipe(
Stream.dropWhile((line) => line.startsWith("#")),
Stream.runCollect
)
Effect.runPromise(dataOnly).then((chunk) => {
console.log([...chunk])
})
Rationale:
Use take to limit how many elements to process. Use drop to skip elements. Add While variants for condition-based limits.
Streams can be infinite or very large. These operators let you:
- Limit processing - Only take what you need
- Skip headers - Drop first N elements
- Conditional limits - Take/drop based on predicates
- Pagination - Implement skip/limit patterns