fn: add transpositions for Option and Result

This commit is contained in:
Keagan McClelland committed 2024-11-06 16:40:36 -07:00
1 parent 8971c4c3ae
commit 5dec35426c
4 files changed
+74

No files matched your search

+12
View File
@@ -251,3 +251,15 @@ func (o Option[A]) SomeToOkf(errString string, args ...interface{}) Result[A] {
OptionToLeft[A, A, error](o, fmt.Errorf(errString, args...)),
}
}
// TransposeOptRes transposes the Option[Result[A]] into a Result[Option[A]].
// This has the effect of leaving an A value alone while inverting the Option
// and Result layers. If there is no internal A value, it will convert the
// non-success value to the proper one in the transposition.
func TransposeOptRes[A any](o Option[Result[A]]) Result[Option[A]] {
if o.IsNone() {
return Ok(None[A]())
}
return Result[Option[A]]{MapLeft[A, error](Some[A])(o.some.Either)}
}