happy-rusty
    Preparing search index...

    Interface ControlFlow<B, C>

    Used to tell an operation whether it should exit early or go on as usual.

    This is the return type of try_fold and similar iterator methods that support short-circuiting. It can also be used in custom control flow scenarios.

    Use cases:

    • Short-circuiting iterators
    • Signaling early termination in fold-like operations
    • Implementing custom control flow patterns

    1.6.0

    // Using ControlFlow to short-circuit a search
    function findFirstNegative(numbers: number[]): Option<number> {
    let result: Option<number> = None;

    for (const n of numbers) {
    const flow = n < 0 ? Break(n) : Continue();
    if (flow.isBreak()) {
    result = Some(flow.breakValue().unwrap());
    break;
    }
    }

    return result;
    }
    // Using ControlFlow in a custom fold operation
    function tryFold<T, Acc>(
    arr: T[],
    init: Acc,
    f: (acc: Acc, item: T) => ControlFlow<Acc, Acc>
    ): ControlFlow<Acc, Acc> {
    let acc = init;
    for (const item of arr) {
    const flow = f(acc, item);
    if (flow.isBreak()) {
    return flow;
    }
    acc = flow.continueValue().unwrap();
    }
    return Continue(acc);
    }
    interface ControlFlow<B, C = void> {
        "[ControlFlowKindSymbol]": "Break" | "Continue";
        "[toStringTag]": "ControlFlow";
        breakOk(): Result<B, C>;
        breakValue(): Option<B>;
        continueOk(): Result<C, B>;
        continueValue(): Option<C>;
        intoValue(this: ControlFlow<B, B>): B;
        isBreak(): boolean;
        isContinue(): boolean;
        mapBreak<T>(fn: (value: B) => T): ControlFlow<T, C>;
        mapContinue<T>(fn: (value: C) => T): ControlFlow<B, T>;
        toString(): string;
    }

    Type Parameters

    • B

      The type of the value returned on Break (early exit).

    • C = void

      The type of the value returned on Continue (default: void).

    Index

    Properties

    "[ControlFlowKindSymbol]": "Break" | "Continue"

    A unique symbol property used to identify the variant of this ControlFlow. Returns 'Break' if the ControlFlow signals early exit, or 'Continue' if it signals to proceed as normal.

    This is used internally by the isControlFlow utility function to verify that an object is a valid ControlFlow instance, and to distinguish between Break and Continue variants without calling methods.

    Note: The symbol itself is not exported as part of the public API. Use the isControlFlow utility function or the isBreak()/isContinue() methods for type checking.

    "[toStringTag]": "ControlFlow"

    The well-known symbol Symbol.toStringTag used by Object.prototype.toString(). Returns 'ControlFlow' so that Object.prototype.toString.call(flow) produces '[object ControlFlow]'.

    This enables reliable type identification even across different execution contexts (e.g., iframes, different module instances).

    const x = Break(5);
    console.log(Object.prototype.toString.call(x)); // '[object ControlFlow]'

    Methods

    • Converts the ControlFlow into a Result which is Ok if the ControlFlow was Break and Err otherwise.

      Returns Result<B, C>

      Ok(breakValue) if Break, Err(continueValue) if Continue.

      console.log(Break(3).breakOk()); // Ok(3)
      console.log(Continue('still going').breakOk()); // Err('still going')
    • Converts the ControlFlow into an Option which is Some if the ControlFlow was Break and None otherwise.

      Returns Option<B>

      Some(value) if Break, None if Continue.

      console.log(Break(3).breakValue()); // Some(3)
      console.log(Continue().breakValue()); // None
    • Converts the ControlFlow into a Result which is Ok if the ControlFlow was Continue and Err otherwise.

      Returns Result<C, B>

      Ok(continueValue) if Continue, Err(breakValue) if Break.

      console.log(Continue(5).continueOk()); // Ok(5)
      console.log(Break('stopped').continueOk()); // Err('stopped')
    • Converts the ControlFlow into an Option which is Some if the ControlFlow was Continue and None otherwise.

      Returns Option<C>

      Some(value) if Continue, None if Break.

      console.log(Continue(5).continueValue()); // Some(5)
      console.log(Break(3).continueValue()); // None
    • Extracts the value from a ControlFlow<T, T> where both type parameters are the same.

      This method is only available when B and C are the same type. It returns the contained value regardless of whether this is a Break or Continue.

      Parameters

      Returns B

      The contained value.

      const breakFlow: ControlFlow<number, number> = Break(5);
      console.log(breakFlow.intoValue()); // 5

      const continueFlow: ControlFlow<number, number> = Continue(10);
      console.log(continueFlow.intoValue()); // 10
    • Returns true if this is a Break variant.

      Returns boolean

      console.log(Break(3).isBreak()); // true
      console.log(Continue().isBreak()); // false
    • Returns true if this is a Continue variant.

      Returns boolean

      console.log(Continue().isContinue()); // true
      console.log(Break(3).isContinue()); // false
    • Maps ControlFlow<B, C> to ControlFlow<T, C> by applying a function to the break value in case it exists.

      Type Parameters

      • T

        The type of the new break value.

      Parameters

      • fn: (value: B) => T

        A function to apply to the break value.

      Returns ControlFlow<T, C>

      A new ControlFlow with the mapped break value.

      const flow = Break(3);
      console.log(flow.mapBreak(v => v * 2).breakValue()); // Some(6)
    • Maps ControlFlow<B, C> to ControlFlow<B, T> by applying a function to the continue value in case it exists.

      Type Parameters

      • T

        The type of the new continue value.

      Parameters

      • fn: (value: C) => T

        A function to apply to the continue value.

      Returns ControlFlow<B, T>

      A new ControlFlow with the mapped continue value.

      const flow = Continue(5);
      console.log(flow.mapContinue(v => v * 2).continueValue()); // Some(10)
    • Custom toString implementation that uses the ControlFlow's contained value.

      Returns string

      console.log(Break(5).toString()); // 'Break(5)'
      console.log(Continue('ok').toString()); // 'Continue(ok)'