Skip to content

Commit

Permalink
fn: add note and hush partial bijections
Browse files Browse the repository at this point in the history
  • Loading branch information
ProofOfKeags committed Aug 6, 2024
1 parent 20a6762 commit 48a736b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
4 changes: 2 additions & 2 deletions fn/either_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestPropToOptionIdentities(t *testing.T) {
e = NewRight[int, string](s)

r2O := e.RightToOption() == Some(s)
o2R := e == OptionToRight[string, int, string](
o2R := e == OptionToRight[string, int](
Some(s), i,
)
l2O := e.LeftToOption() == None[int]()
Expand All @@ -109,7 +109,7 @@ func TestPropToOptionIdentities(t *testing.T) {
} else {
e = NewLeft[int, string](i)
l2O := e.LeftToOption() == Some(i)
o2L := e == OptionToLeft[int, int](Some(i), s)
o2L := e == OptionToLeft[int](Some(i), s)
r2O := e.RightToOption() == None[string]()

return l2O && o2L && r2O
Expand Down
17 changes: 14 additions & 3 deletions fn/option.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package fn

import "testing"
import (
"fmt"
"testing"
)

// Option[A] represents a value which may or may not be there. This is very
// often preferable to nil-able pointers.
Expand Down Expand Up @@ -216,7 +219,7 @@ func (o Option[A]) UnsafeFromSome() A {

// OptionToLeft can be used to convert an Option value into an Either, by
// 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] {
func OptionToLeft[O, R any](o Option[O], r R) Either[O, R] {
if o.IsSome() {
return NewLeft[O, R](o.some)
}
Expand All @@ -226,10 +229,18 @@ func OptionToLeft[O, L, R any](o Option[O], r R) Either[O, 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] {
func OptionToRight[O, L any](o Option[O], l L) Either[L, O] {
if o.IsSome() {
return NewRight[L, O](o.some)
}

return NewLeft[L, O](l)
}

// Note allows you to convert an Option value to a result with your own error
// message.
func (o Option[A]) Note(errString string, args ...interface{}) Result[A] {
return Result[A]{
OptionToLeft[A, error](o, fmt.Errorf(errString, args...)),
}
}
12 changes: 12 additions & 0 deletions fn/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,23 @@ func (r Result[T]) Option() Option[T] {
return r.Either.LeftToOption()
}

// Hush mutes the error value of the result.
func (r Result[T]) Hush() Option[T] {
return r.Either.LeftToOption()
}

// WhenResult executes the given function if the Result is a success.
//
// Deprecated: Use WhenOk instead.
func (r Result[T]) WhenResult(f func(T)) {
r.WhenLeft(f)
}

// WhenOk executes the given function if the Result is a success.
func (r Result[T]) WhenOk(f func(T)) {
r.WhenLeft(f)
}

// WhenErr executes the given function if the Result is an error.
func (r Result[T]) WhenErr(f func(error)) {
r.WhenRight(f)
Expand Down

0 comments on commit 48a736b

Please sign in to comment.