StaticrunRuns one do block, given the underlying monad's of.
The block's monad comes from of, not from the yielded values, so a
block that yields nothing still has a monad to return.
If gen throws, the error surfaces as the underlying monad's bind surfaces it.
The value the generator returns.
A Monad<A> for the whole block; cast it to the concrete monad.
import { Do, Either } from '@b-jones-rfd/func-help'
const sum = Do.run(Either.of, function* () {
const a = yield* Either.right<number, string>(1) // number
const b = yield* Either.right<number, string>(2) // number
return a + b
}) as Either<string, number>
sum.fold((e) => e, (n) => `${n}`) // '3'
StaticwithFixes the underlying monad's of, giving a runner for blocks in that
monad.
Binding of once saves passing it to every block, as Haskell's do
infers the monad from context.
A throw in the generator surfaces as in Do.run.
The underlying monad's of, used to start each block and
wrap its return value.
A function that takes a generator function and returns a
Monad<A> for the block, as Do.run does; cast it to the
concrete monad.
Runs a generator function as a monadic
doblock, for a monad of your own.For the package's own types, use their
doinstead (Either.do,Except.do,IO.do,AsyncIO.do,State.do,Reader.do): it types every binding and returns the concrete type, with no casts.You yield monadic values inside a generator, and the helper stitches the
bind/mapcalls together. It stays generic by taking the underlying monad'sofup front, becauseMonad<T>has no staticofto call.The result is typed
Monad<A>, because the helper cannot know which monadofbuilds; cast it to the concrete monad (as IO<number>) to reachrun()orfold().yield* mon a monad with a[Symbol.iterator]method that yields the monad and returns the value it is resumed with (as the package's types have) binds the value with its real type. A plainyield mworks on anyMonad, but the binding is typedany, because TypeScript cannot relate a yield's type to the value sent back in; annotate it.Unlike Haskell, the compiler does not check that every monad yielded in one block is the same kind as
ofbuilds; mixing kinds fails at run time.A throw in the generator surfaces the way the underlying monad surfaces a throw in
bind: fromrun()for IO, State or Reader, as a rejection ofrun()'s promise for AsyncIO, and from theDocall itself for Either or Except.Example