fn: add transpositions for Option and Result

This commit is contained in:
Keagan McClelland
2024-08-13 16:59:18 -07:00
parent 8971c4c3ae
commit 5dec35426c
4 changed files with 74 additions and 0 deletions

View File

@@ -223,3 +223,15 @@ func AndThen2[A, B, C any](ra Result[A], rb Result[B],
})
})
}
// TransposeResOpt transposes the Result[Option[A]] into a Option[Result[A]].
// This has the effect of leaving an A value alone while inverting the Result
// and Option layers. If there is no internal A value, it will convert the
// non-success value to the proper one in the transposition.
func TransposeResOpt[A any](r Result[Option[A]]) Option[Result[A]] {
if r.IsErr() {
return Some(Err[A](r.right))
}
return MapOption(Ok[A])(r.left)
}