diff --git a/src/either.ts b/src/either.ts index 3e3e03c..c6977eb 100644 --- a/src/either.ts +++ b/src/either.ts @@ -106,6 +106,26 @@ export class DoEither, E> { return new DoEither(newEither) } + /** + * Binds a new value to the scope using a lazily evaluated Either + * @typeParam K - The key to bind to + * @typeParam A - The type of the value to bind + * @param key - The property name to bind to + * @param f - Function returning the Either value to bind (receives current scope) + * @returns A new DoEither instance with the extended scope + */ + bindL( + key: K, + f: (scope: T) => Either, + ): DoEither, E> { + const newEither = eitherChain, E>((scope: T) => + eitherMap, E>((a: A) => ({...scope, [key]: a}))( + f(scope), + ) + )(this.either) + return new DoEither(newEither) + } + /** * Returns the final result from the scope * @typeParam B - The return type