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
No known key found for this signature in database
GPG Key ID: FA7E65C951F12439

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)
}
// AndThen2 applies a function that returns a Result[C] to the success values
// of two Result types if both exist.
func AndThen2[A, B, C any](ra Result[A], rb Result[B],
f func(A, B) Result[C]) Result[C] {
// LiftA2Result lifts a two-argument function to a function that can operate
// over results of its arguments.
func LiftA2Result[A, B, C any](f func(A, B) C,
) func(Result[A], Result[B]) Result[C] {
return AndThen(ra, func(a A) Result[C] {
return AndThen(rb, func(b B) Result[C] {
return f(a, b)
})
})
return func(ra Result[A], rb Result[B]) Result[C] {
if ra.IsErr() {
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