@effect-native/patterns
v0.1.0
Published
Worked examples showing Effect module patterns (TypeId, dual, Equal.symbol, Hash.symbol).
Readme
@effect-native/patterns
Hands-on study material for the Effect Native codebase. Each module in this
package illustrates the expectations described in the normative pattern library
(.patterns/*.md).
Thing
Thing is a deliberately small data type that demonstrates several of the
library-wide rules:
- Type identifiers: every instance carries a stable
TypeIdsymbol so it can be recognised safely across module boundaries. - Structural protocols: the prototype implements
Equal.symbolandHash.symbol, usingHash.cachedplus sorted, deduplicated tags to keep hash codes deterministic. - Dual combinators: higher-order helpers like
mapValueandaddTagare exposed withdualso they support both data-first and data-last styles. - Pipeable: the prototype delegates to Effect's
pipeArgumentshelper so instances compose naturally with.pipe(...).
The accompanying tests in test/Thing.test.ts follow the
@effect/vitest conventions from .patterns/testing-patterns.md, showing how
structured data (Data.struct) integrates with Equal/Hash.
Usage
import * as Thing from "@effect-native/patterns/Thing"
const todo = Thing.make({
id: "add-patterns-docs",
label: "Write README",
value: { done: false }
})
const updated = todo.pipe(
Thing.mapValue((value) => ({ ...value, done: true })),
Thing.addTag("docs")
)Run pnpm --filter @effect-native/patterns test inside nix develop to execute
the worked examples.
List
List models a functional, singly-linked sequence with all of the idioms we
lean on in production code:
- Pipeable structure: every list is
Pipeable, so you can chain transformations without reaching for helpers. - Structural equality: the prototype implements
Equal.symbolandHash.symbolby walking the nodes to ensure hashing follows value semantics. - Dual combinators: helpers like
cons,append,map,reduce, andforEachEffectall usedualso they work in both data-first and data-last styles. - Effect traversal:
forEachEffectdemonstrates how to loop over the structure withinEffect.gen, yielding each node sequentially.
The tests in test/List.test.ts include examples covering both pure and
Effectful usage while applying the guardrails in .patterns/testing-patterns.md.
Tree
Tree demonstrates how to build richer recursive data models:
- Structured construction:
makeaccepts immutable child collections, and every node tracks its subtree size for quick introspection. - Deep equality / hashing: the prototype walks child trees to implement
Equal.symbolandHash.symbol, ensuring structural comparison works across nested data. - Dual helpers:
appendChild,map, andreduceall support data-first and data-last usage viadual, mirroring the conventions in the Effect standard library. - Effectful traversal:
forEachEffectproduces depth-first visitation with index paths, showing how to integrate recursion withEffect.gen.
See test/Tree.test.ts for executable examples that combine pure and Effectful
workloads.
