How to reduce an array of effects (TaskEither) #1732
Answered
by
florianbepunkt
florianbepunkt
asked this question in
Q&A
-
How to reduce an array of TaskEithers? Given an array of effects I would like to reduce them to a single TaskEither, piping the result of each effect into the next one and terminating at the earliest failure export type PreHook<Command extends AnyCommand, State extends BaseState> = ({
command,
state,
}: {
command: Command;
state: State;
}) => TE.TaskEither<Std.Error.Err, { command: Command; state: State }>;
// code below is obviously not working, trying to illustrate the goal
const applyPreHooks =
<Command extends AnyCommand, State extends BaseState>(preHooks: PreHook<Command, State>[]) =>
({ command, state }: { command: Command; state: State }) => {
preHooks.reduce(
({ command, state }, hook) => {
const a = hook({ command, state });
return { command, state };
},
{ command, state }
);
}; |
Beta Was this translation helpful? Give feedback.
Answered by
florianbepunkt
Jul 9, 2022
Replies: 1 comment
-
Okay, the answer is rather simple.. export const applyPreHooks =
<Command extends AnyCommand, State extends BaseState>(preHooks: PreHook<Command, State>[]) =>
(initialValues: { command: Command; state: State }) =>
preHooks.reduce(
(acc, hook) => TE.chain(hook)(acc),
TE.of(initialValues) as TE.TaskEither<Std.Error.Err, { command: Command; state: State }>
); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
florianbepunkt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Okay, the answer is rather simple..