Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Either: Add Map helper function #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ Constructors:
- `mo.Left()` [doc](https://pkg.go.dev/github.com/samber/mo#Left)
- `mo.Right()` [doc](https://pkg.go.dev/github.com/samber/mo#Right)

Helper:

- `mo.Map()` [doc](https://pkg.go.dev/github.com/samber/mo#Map)

Methods:

- `.IsLeft()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.IsLeft)
Expand Down
11 changes: 11 additions & 0 deletions either.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ func Right[L any, R any](value R) Either[L, R] {
}
}

// Map executes the given function, depending of value is Left or Right, and returns result.
func Map[L any, R any, T any](e Either[L, R], onLeft func(L) T, onRight func(R) T) T {
if e.IsLeft() {
return onLeft(e.left)
} else if e.IsRight() {
return onRight(e.right)
}

panic(eitherShouldBeLeftOrRight)
}

// Either respresents a value of 2 possible types.
// An instance of Either is an instance of either A or B.
type Either[L any, R any] struct {
Expand Down
15 changes: 15 additions & 0 deletions either_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ func ExampleRight() {
// Output: world 42
}

func ExampleMap() {
left := Left[string, int]("hello")
result := Map(left,
func(s string) float64 {
return 21.21
},
func(i int) float64 {
return 1.1
},
)

fmt.Println(result)
// Output: 21.21
}

func ExampleEither_IsLeft_left() {
left := Left[string, int]("hello")
result := left.IsLeft()
Expand Down
29 changes: 29 additions & 0 deletions either_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,35 @@ func TestEitherRight(t *testing.T) {
is.Equal(Either[int, bool]{left: 0, right: true, isLeft: false}, right)
}

func TestEitherMap(t *testing.T) {
is := assert.New(t)

e1 := Map(Left[int, string](42),
func(a int) float64 {
is.Equal(42, a)
return 21.21
},
func(b string) float64 {
is.Fail("should not enter here")
return 1.1
},
)

e2 := Map(Right[int, string]("foobar"),
func(a int) float64 {
is.Fail("should not enter here")
return 21.21
},
func(b string) float64 {
is.Equal("foobar", b)
return 1.1
},
)

is.Equal(21.21, e1)
is.Equal(1.1, e2)
}

func TestEitherIsLeftOrRight(t *testing.T) {
is := assert.New(t)

Expand Down