@@ -842,6 +842,51 @@ export function getsSecond<S extends unknown[], B>(
842842 return async ( ...s ) => [ E . left ( await fsa . apply ( fsa , s ) ) , ...s ] ;
843843}
844844
845+ /**
846+ * Extract an Either value from the current state using a function. Unlike
847+ * `gets` which always returns a Right (success), or `getsSecond` which always
848+ * returns a Left (error), `getsEither` allows the function to return either
849+ * a Left or a Right based on the state. The state is passed through unchanged.
850+ *
851+ * This is useful when you need to perform validation or conditional logic on
852+ * the state that can result in either success or failure.
853+ *
854+ * @example
855+ * ```ts
856+ * import * as Eff from "./effect.ts";
857+ * import * as E from "./either.ts";
858+ *
859+ * // Synchronous Either
860+ * const validatePositive = Eff.getsEither((n: number) =>
861+ * n > 0 ? E.right(n * 2) : E.left("Number must be positive")
862+ * );
863+ *
864+ * const result1 = await validatePositive(21);
865+ * // [E.right(42), 21]
866+ *
867+ * const result2 = await validatePositive(-5);
868+ * // [E.left("Number must be positive"), -5]
869+ *
870+ * // Asynchronous Either
871+ * const asyncValidate = Eff.getsEither((n: number) =>
872+ * Promise.resolve(n > 0 ? E.right(n * 2) : E.left("Must be positive"))
873+ * );
874+ *
875+ * const result3 = await asyncValidate(21);
876+ * // [E.right(42), 21]
877+ *
878+ * const result4 = await asyncValidate(-5);
879+ * // [E.left("Must be positive"), -5]
880+ * ```
881+ *
882+ * @since 3.0.0-rc.4
883+ */
884+ export function getsEither < S extends unknown [ ] , A , B > (
885+ fsab : ( ...s : S ) => Either < B , A > | Promise < Either < B , A > > ,
886+ ) : Effect < S , B , A , S > {
887+ return async ( ...s ) => [ await fsab . apply ( fsab , s ) , ...s ] ;
888+ }
889+
845890/**
846891 * Transform the current state using a function. The transformed value
847892 * becomes both the success value and the new state.
0 commit comments