FuncType - v0.16.0
    Preparing search index...

    Function Do

    • Executes a generator-based monadic comprehension Returns the same monad type as the first yielded monad (Scala semantics)

      • Option comprehensions return Option (None on short-circuit)
      • Either comprehensions return Either (Left with error on short-circuit)
      • List comprehensions return List (empty or cartesian product)
      • Try comprehensions return Try (Failure with error on short-circuit)

      Type Inference Notes:

      • TypeScript infers the correct return type for homogeneous comprehensions
      • For mixed monad types, TypeScript returns a union type
      • Use DoTyped or type assertions for mixed scenarios

      Type Parameters

      • T

      Parameters

      • gen: () => Generator<OptionLike, T, unknown>

        Generator function that yields monads and returns a result

      Returns Option<T>

      The same monad type as the first yield

      // Option comprehension returns Option:
      const result = Do(function* () {
      const x = yield* $(Option(5));
      const y = yield* $(Option(10));
      return x + y;
      });
      // result: Option(15)

      // Either comprehension returns Either:
      const result = Do(function* () {
      const x = yield* $(Right(5));
      const y = yield* $(Left("error"));
      return x + y;
      });
      // result: Left("error") - error is preserved

      // List comprehension returns List with cartesian product:
      const result = Do(function* () {
      const x = yield* $(List([1, 2]));
      const y = yield* $(List([3, 4]));
      return x + y;
      });
      // result: List([4, 5, 5, 6])

      // Mixed types - use type assertion or DoTyped:
      const result = Do(function* () {
      const x = yield* $(Option(5));
      const y = yield* $(Right<string, number>(10));
      return x + y;
      }) as Option<number>;
      // result: Option(15)
    • Executes a generator-based monadic comprehension Returns the same monad type as the first yielded monad (Scala semantics)

      • Option comprehensions return Option (None on short-circuit)
      • Either comprehensions return Either (Left with error on short-circuit)
      • List comprehensions return List (empty or cartesian product)
      • Try comprehensions return Try (Failure with error on short-circuit)

      Type Inference Notes:

      • TypeScript infers the correct return type for homogeneous comprehensions
      • For mixed monad types, TypeScript returns a union type
      • Use DoTyped or type assertions for mixed scenarios

      Type Parameters

      • L
      • R

      Parameters

      • gen: () => Generator<EitherLike, R, unknown>

        Generator function that yields monads and returns a result

      Returns Either<L, R>

      The same monad type as the first yield

      // Option comprehension returns Option:
      const result = Do(function* () {
      const x = yield* $(Option(5));
      const y = yield* $(Option(10));
      return x + y;
      });
      // result: Option(15)

      // Either comprehension returns Either:
      const result = Do(function* () {
      const x = yield* $(Right(5));
      const y = yield* $(Left("error"));
      return x + y;
      });
      // result: Left("error") - error is preserved

      // List comprehension returns List with cartesian product:
      const result = Do(function* () {
      const x = yield* $(List([1, 2]));
      const y = yield* $(List([3, 4]));
      return x + y;
      });
      // result: List([4, 5, 5, 6])

      // Mixed types - use type assertion or DoTyped:
      const result = Do(function* () {
      const x = yield* $(Option(5));
      const y = yield* $(Right<string, number>(10));
      return x + y;
      }) as Option<number>;
      // result: Option(15)
    • Executes a generator-based monadic comprehension Returns the same monad type as the first yielded monad (Scala semantics)

      • Option comprehensions return Option (None on short-circuit)
      • Either comprehensions return Either (Left with error on short-circuit)
      • List comprehensions return List (empty or cartesian product)
      • Try comprehensions return Try (Failure with error on short-circuit)

      Type Inference Notes:

      • TypeScript infers the correct return type for homogeneous comprehensions
      • For mixed monad types, TypeScript returns a union type
      • Use DoTyped or type assertions for mixed scenarios

      Type Parameters

      • T

      Parameters

      • gen: () => Generator<ListLike, T, unknown>

        Generator function that yields monads and returns a result

      Returns List<T>

      The same monad type as the first yield

      // Option comprehension returns Option:
      const result = Do(function* () {
      const x = yield* $(Option(5));
      const y = yield* $(Option(10));
      return x + y;
      });
      // result: Option(15)

      // Either comprehension returns Either:
      const result = Do(function* () {
      const x = yield* $(Right(5));
      const y = yield* $(Left("error"));
      return x + y;
      });
      // result: Left("error") - error is preserved

      // List comprehension returns List with cartesian product:
      const result = Do(function* () {
      const x = yield* $(List([1, 2]));
      const y = yield* $(List([3, 4]));
      return x + y;
      });
      // result: List([4, 5, 5, 6])

      // Mixed types - use type assertion or DoTyped:
      const result = Do(function* () {
      const x = yield* $(Option(5));
      const y = yield* $(Right<string, number>(10));
      return x + y;
      }) as Option<number>;
      // result: Option(15)
    • Executes a generator-based monadic comprehension Returns the same monad type as the first yielded monad (Scala semantics)

      • Option comprehensions return Option (None on short-circuit)
      • Either comprehensions return Either (Left with error on short-circuit)
      • List comprehensions return List (empty or cartesian product)
      • Try comprehensions return Try (Failure with error on short-circuit)

      Type Inference Notes:

      • TypeScript infers the correct return type for homogeneous comprehensions
      • For mixed monad types, TypeScript returns a union type
      • Use DoTyped or type assertions for mixed scenarios

      Type Parameters

      • T

      Parameters

      • gen: () => Generator<TryLike, T, unknown>

        Generator function that yields monads and returns a result

      Returns Try<T>

      The same monad type as the first yield

      // Option comprehension returns Option:
      const result = Do(function* () {
      const x = yield* $(Option(5));
      const y = yield* $(Option(10));
      return x + y;
      });
      // result: Option(15)

      // Either comprehension returns Either:
      const result = Do(function* () {
      const x = yield* $(Right(5));
      const y = yield* $(Left("error"));
      return x + y;
      });
      // result: Left("error") - error is preserved

      // List comprehension returns List with cartesian product:
      const result = Do(function* () {
      const x = yield* $(List([1, 2]));
      const y = yield* $(List([3, 4]));
      return x + y;
      });
      // result: List([4, 5, 5, 6])

      // Mixed types - use type assertion or DoTyped:
      const result = Do(function* () {
      const x = yield* $(Option(5));
      const y = yield* $(Right<string, number>(10));
      return x + y;
      }) as Option<number>;
      // result: Option(15)
    • Executes a generator-based monadic comprehension Returns the same monad type as the first yielded monad (Scala semantics)

      • Option comprehensions return Option (None on short-circuit)
      • Either comprehensions return Either (Left with error on short-circuit)
      • List comprehensions return List (empty or cartesian product)
      • Try comprehensions return Try (Failure with error on short-circuit)

      Type Inference Notes:

      • TypeScript infers the correct return type for homogeneous comprehensions
      • For mixed monad types, TypeScript returns a union type
      • Use DoTyped or type assertions for mixed scenarios

      Type Parameters

      • T

      Parameters

      • gen: () => Generator<OptionLike | EitherLike | ListLike | TryLike, T, unknown>

        Generator function that yields monads and returns a result

      Returns Reshapeable<T>

      The same monad type as the first yield

      // Option comprehension returns Option:
      const result = Do(function* () {
      const x = yield* $(Option(5));
      const y = yield* $(Option(10));
      return x + y;
      });
      // result: Option(15)

      // Either comprehension returns Either:
      const result = Do(function* () {
      const x = yield* $(Right(5));
      const y = yield* $(Left("error"));
      return x + y;
      });
      // result: Left("error") - error is preserved

      // List comprehension returns List with cartesian product:
      const result = Do(function* () {
      const x = yield* $(List([1, 2]));
      const y = yield* $(List([3, 4]));
      return x + y;
      });
      // result: List([4, 5, 5, 6])

      // Mixed types - use type assertion or DoTyped:
      const result = Do(function* () {
      const x = yield* $(Option(5));
      const y = yield* $(Right<string, number>(10));
      return x + y;
      }) as Option<number>;
      // result: Option(15)
    • Executes a generator-based monadic comprehension Returns the same monad type as the first yielded monad (Scala semantics)

      • Option comprehensions return Option (None on short-circuit)
      • Either comprehensions return Either (Left with error on short-circuit)
      • List comprehensions return List (empty or cartesian product)
      • Try comprehensions return Try (Failure with error on short-circuit)

      Type Inference Notes:

      • TypeScript infers the correct return type for homogeneous comprehensions
      • For mixed monad types, TypeScript returns a union type
      • Use DoTyped or type assertions for mixed scenarios

      Type Parameters

      • T

      Parameters

      • gen: () => Generator<unknown, T, unknown>

        Generator function that yields monads and returns a result

      Returns unknown

      The same monad type as the first yield

      // Option comprehension returns Option:
      const result = Do(function* () {
      const x = yield* $(Option(5));
      const y = yield* $(Option(10));
      return x + y;
      });
      // result: Option(15)

      // Either comprehension returns Either:
      const result = Do(function* () {
      const x = yield* $(Right(5));
      const y = yield* $(Left("error"));
      return x + y;
      });
      // result: Left("error") - error is preserved

      // List comprehension returns List with cartesian product:
      const result = Do(function* () {
      const x = yield* $(List([1, 2]));
      const y = yield* $(List([3, 4]));
      return x + y;
      });
      // result: List([4, 5, 5, 6])

      // Mixed types - use type assertion or DoTyped:
      const result = Do(function* () {
      const x = yield* $(Option(5));
      const y = yield* $(Right<string, number>(10));
      return x + y;
      }) as Option<number>;
      // result: Option(15)