Generator function that yields monads and returns a result
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)
Type Inference Notes:
Generator function that yields monads and returns a result
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)
Type Inference Notes:
Generator function that yields monads and returns a result
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)
Type Inference Notes:
Generator function that yields monads and returns a result
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)
Type Inference Notes:
Generator function that yields monads and returns a result
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)
Type Inference Notes:
Generator function that yields monads and returns a result
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)
Type Inference Notes: