Skip to content

Dual APIs

A dual function accepts either the data followed by options (data-first) or the options alone, returning a function that takes the data later (data-last). Use direct data-first calls for single operations, |> with % for multi-step VibeLang pipelines, and data-last overloads when composing reusable stages with pipe.

Data-first and data-last calls

const source = Stream.make(5, 10, 15)
const direct = Stream.map(source, (n) => n / 5)
const stage = Stream.map((n: number) => n / 5)
const piped = pipe(source, stage)
 
effect function compare() {
  return [try Stream.runCollect(direct), try Stream.runCollect(piped)]
}
// Both arrays are [1, 2, 3].
// Run in an effect function:
//   {
//     const jsonValue = try compare()
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
[[1,2,3],[1,2,3]]

The direct call passes source immediately; the data-last call builds a reusable transformation stage that pipe applies to source. Both produce an identical lazy Stream value and perform no work until consumed.

Module functions over data types

const missing = Cause.fail("missing port")
 
effect function methods() {
  const amount = try Effect.try(() => BigDecimal.fromNumberUnsafe(-2.5))
  return {
    error: Option.getOrElse(Cause.findErrorOption(missing), () => "none"),
    amount: BigDecimal.format(BigDecimal.abs(amount)),
    pairs: try Stream.runCollect(Stream.zipLeft(Stream.make(1, 2), Stream.make(3, 4)))
  }
}
// Run in an effect function:
//   {
//     const jsonValue = try methods()
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
{"error":"missing port","amount":"2.5","pairs":[1,2]}

Effect data types are transformed through module functions rather than instance methods, while built-in object methods such as .pipe retain their standard behavior. Calling a binary operator data-first (such as Stream.zipLeft(source, other)) returns the transformed value; calling it data-last (Stream.zipLeft(other)) returns a stage for pipe.

ModuleRepresentative functions
BigDecimalabs, format, isInteger, isNegative, isPositive, isZero, negate, round, sign, toExponential, toNumberUnsafe
CausefindErrorOption, pretty
DurationtoHrTime, toNanosUnsafe
EffectannotateLogs, annotateSpans, tx, withExecutionPlan, withLogger, withLogSpan, withTracer, withTracerEnabled, withTracerTiming
ExitfindDefect, findError
ScheduletoStep, toStepWithMetadata, toStepWithSleep
Streamcross, crossWith, mergeLeft, mergeRight, race, toChannel, zipLatest, zipLatestWith, zipLeft, zipRight, zipWith, zipWithArray

Direct calls and nested programs

effect function lookup() {
  try sleep("10 millis")
  return "found"
}
 
effect function directLookup() {
  return try timeout(lookup(), "1 second")
}
 
effect function nestedLookup() {
  return try timeout(retry(lookup(), { times: 2 }), "1 second")
}
// Run in an effect function: try Console.log(try directLookup(), try nestedLookup())
found found

Combinators like retry and timeout operate on Effect values, not settled results. Here retry re-executes lookup() on failure, while the outer timeout bounds the entire retried sequence.

Choosing a form

effect function boundedLookup() {
  return try retry(lookup(), { times: 2 })
}
 
const reusableLookup = lookup()
  |> retry(%, { times: 2 })
  |> timeout(%, "1 second")
// Run in an effect function: try Console.log(try boundedLookup(), try reusableLookup)
found found

Use a direct call for a single operator, and the pipe operator |> (where % stands for the left-hand value) when chaining several operators. Binding an Effect value to a constant makes it reusable—each try reusableLookup runs the workflow anew—while plain local variables and statements replace Effect.map and Effect.flatMap for post-processing results.


The matching Effect page: effect.website/docs/v4/code-style/dual