Simplifying Excessive Nesting
Dependent computations in VibeLang bind intermediate results to ordinary local variables with try, avoiding nested callbacks and Effect.Do record pipelines. Use effect { ... } when a combinator needs a multi-statement Effect value, and do { ... } when an expression position requires multiple statements.
Recognizing nesting in dependent pipelines
effect function elapsed<A, E, R>(program: A!E!R) {
const started = try Clock.currentTimeMillis
const value = try program
const finished = try Clock.currentTimeMillis
try Console.log(`completed in ${finished - started} ms`)
return value
}
effect function example() {
return try elapsed(effect {
try sleep("40 millis")
return "complete"
})
}
// Run in an effect function:
// provide TestClock.layer() {
// try TestClock.setTime(0)
// const worker = fork example()
// try TestClock.adjust("40 millis")
// try Console.log(join worker)
// }completed in 40 ms
completestarted, value, and finished remain in scope across subsequent statements with zero callback nesting. Parameterizing over A!E!R preserves program's success, error, and service requirements through elapsed.
Named bindings with do notation
effect function readInterval() {
return try Effect.Do.pipe(
Effect.bind("started", () => Clock.currentTimeMillis),
Effect.bind("value", () => effect {
try sleep("40 millis")
return "complete"
}),
Effect.bind("finished", () => Clock.currentTimeMillis)
)
}
// Returns { started, value, finished }.
// Run in an effect function:
// provide TestClock.layer() {
// try TestClock.setTime(0)
// const worker = fork readInterval()
// try TestClock.adjust("40 millis")
// const value = join worker
// try Console.log(try Effect.try(() => JSON.stringify(value)))
// }{"started":0,"value":"complete","finished":40}Effect.Do starts an Effect holding an empty object, and each Effect.bind appends a new field from an Effect-returning callback that sees all earlier fields. Keep this form only when maintaining existing TypeScript pipelines; prefer local const bindings in new .vibe code.
Introducing derived values
effect function intervalRecord() {
return try Effect.Do.pipe(
Effect.bind("started", () => Clock.currentTimeMillis),
Effect.bind("finished", () => effect {
try sleep("40 millis")
return try Clock.currentTimeMillis
}),
Effect.let("millis", ({ started, finished }) => finished - started)
)
}
// Run in an effect function:
// provide TestClock.layer() {
// try TestClock.setTime(0)
// const worker = fork intervalRecord()
// try TestClock.adjust("40 millis")
// const value = join worker
// try Console.log(try Effect.try(() => JSON.stringify(value)))
// }{"started":0,"finished":40,"millis":40}Effect.let adds a synchronous, non-failing derived field, whereas Effect.bind runs an Effect. In an effect function, both reduce to plain const bindings whose contracts the compiler infers.
The sequence as ordinary statements
effect function intervalRecord() {
const started = try Clock.currentTimeMillis
try sleep("40 millis")
const finished = try Clock.currentTimeMillis
const millis = finished - started
return { started, finished, millis }
}
// Run in an effect function:
// provide TestClock.layer() {
// try TestClock.setTime(0)
// const worker = fork intervalRecord()
// try TestClock.adjust("40 millis")
// const value = join worker
// try Console.log(try Effect.try(() => JSON.stringify(value)))
// }{"started":0,"finished":40,"millis":40}In .vibe, an effect function sequences steps with try just as TypeScript's Effect.gen sequences them with yield*. An effect function* yields into a Stream (consumed via for try), while a plain function* remains a standard JavaScript generator.
Expression blocks with do
do { … } runs ordinary statements where an expression is needed and gives you the block’s completion value. For example, do { const base = 3; base * 2 } evaluates to 6. See do expressions for completion and evaluation-order rules.
Coming from Effect
| Effect TypeScript | VibeLang |
|---|---|
Effect.Do + Effect.bind | Sequential const x = try ... statements |
Effect.let | Ordinary const y = ... expression |
Effect.gen(function* () { yield* ... }) | effect function with try |
Region finalizers observe the underlying generator result (undefined on normal completion or outward break/continue, and the returned value on return), while do receives the block's completion value without exposing compiler control records.
The matching Effect page: effect.website/docs/v4/code-style/do