mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-29 07:00:55 +02:00
fn: breaking - replace AndThen2 with more principled LiftA2Result
This commit is contained in:
24
fn/result.go
24
fn/result.go
@@ -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
|
||||||
|
Reference in New Issue
Block a user