Add bindL to DoEither

This commit is contained in:
themodernhakr 2025-06-09 00:56:38 -05:00
parent d88075c169
commit 07a74315e1

View File

@ -106,6 +106,26 @@ export class DoEither<T extends Record<string, unknown>, 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<K extends string, A>(
key: K,
f: (scope: T) => Either<A, E>,
): DoEither<T & Record<K, A>, E> {
const newEither = eitherChain<T, T & Record<K, A>, E>((scope: T) =>
eitherMap<A, T & Record<K, A>, 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