Using Effect.filterOrElse to handle conditional logic can be a bit tricky, especially when you wa...
I've been working on some Effect code for AOC day 3:
I'm trying to make the code in the
But that doesn't match the output properly. Any clue on what direction I should be looking at?
const checkState = (state: State, instruction: string) => pipe(
instruction,
Effect.succeed,
Effect.flatMap(instr =>
Match.value(instr).pipe(
Match.when("do()", () => {
console.log("Enabling multiplications")
return Effect.succeed({ ...state, enabled: true })
}),
Match.when("don't()", () => {
console.log("Disabling multiplications")
return Effect.succeed({ ...state, enabled: false })
}),
Match.orElse(() => {
if (!state.enabled) {
console.log(`Skipping disabled multiplication: ${instruction}`)
return Effect.succeed(state)
}
const newState = parseInput(state, instruction)
console.log(`Processing ${instruction}: ${state.sum} -> ${newState.sum}`)
return Effect.succeed(newState)
})
)
)
)const checkState = (state: State, instruction: string) => pipe(
instruction,
Effect.succeed,
Effect.flatMap(instr =>
Match.value(instr).pipe(
Match.when("do()", () => {
console.log("Enabling multiplications")
return Effect.succeed({ ...state, enabled: true })
}),
Match.when("don't()", () => {
console.log("Disabling multiplications")
return Effect.succeed({ ...state, enabled: false })
}),
Match.orElse(() => {
if (!state.enabled) {
console.log(`Skipping disabled multiplication: ${instruction}`)
return Effect.succeed(state)
}
const newState = parseInput(state, instruction)
console.log(`Processing ${instruction}: ${state.sum} -> ${newState.sum}`)
return Effect.succeed(newState)
})
)
)
)I'm trying to make the code in the
orElseorElse block more effectful. I was trying to do it with something like this:Match.orElse(() =>
pipe(
Effect.succeed(state),
Effect.filterOrElse(
s => s.enabled,
() => Effect.succeed(state),
),
Effect.map(() => parseInput(state, instr)),
)
)Match.orElse(() =>
pipe(
Effect.succeed(state),
Effect.filterOrElse(
s => s.enabled,
() => Effect.succeed(state),
),
Effect.map(() => parseInput(state, instr)),
)
)But that doesn't match the output properly. Any clue on what direction I should be looking at?
