fn: breaking - replace AndThen2 with more principled LiftA2Result

This commit is contained in:
Keagan McClelland
2024-08-14 18:24:17 -07:00
parent 1480287cfc
commit 4d16d5ff15

View File

@@ -189,16 +189,22 @@ func AndThen[A, B any](r Result[A], f func(A) Result[B]) Result[B] {
return FlatMap(r, f) return FlatMap(r, f)
} }
// AndThen2 applies a function that returns a Result[C] to the success values // LiftA2Result lifts a two-argument function to a function that can operate
// of two Result types if both exist. // over results of its arguments.
func AndThen2[A, B, C any](ra Result[A], rb Result[B], func LiftA2Result[A, B, C any](f func(A, B) C,
f func(A, B) Result[C]) Result[C] { ) func(Result[A], Result[B]) Result[C] {
return AndThen(ra, func(a A) Result[C] { return func(ra Result[A], rb Result[B]) Result[C] {
return AndThen(rb, func(b B) Result[C] { if ra.IsErr() {
return f(a, b) return Err[C](ra.right)
}) }
})
if rb.IsErr() {
return Err[C](rb.right)
}
return Ok(f(ra.left, rb.left))
}
} }
// Sink consumes a Result, either propagating its error or processing its // Sink consumes a Result, either propagating its error or processing its