fn: reimplement internals of either, add tests

This commit is contained in:
Keagan McClelland committed 2024-07-10 15:37:27 -07:00
1 parent 1dd56f1b2a
commit c4df2f1dce
4 files changed
+228 -67

No files matched your search

+4 -16
View File
@@ -218,30 +218,18 @@ func (o Option[A]) UnsafeFromSome() A {
// providing the Right value that should be used if the Option value is None.
func OptionToLeft[O, L, R any](o Option[O], r R) Either[O, R] {
if o.IsSome() {
return Either[O, R]{
left: o,
right: None[R](),
}
return NewLeft[O, R](o.some)
}
return Either[O, R]{
left: None[O](),
right: Some(r),
}
return NewRight[O, R](r)
}
// OptionToRight can be used to convert an Option value into an Either, by
// providing the Left value that should be used if the Option value is None.
func OptionToRight[O, L, R any](o Option[O], l L) Either[L, O] {
if o.IsSome() {
return Either[L, O]{
left: None[L](),
right: o,
}
return NewRight[L, O](o.some)
}
return Either[L, O]{
left: Some(l),
right: None[O](),
}
return NewLeft[L, O](l)
}