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

Fix the Map for Option type #41

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: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ option1.
FlatMap(func (value int) Option[int] {
return Some(value%2)
}).
FlatMap(func (value int) Option[int] {
return Some(value+21)
Map(func (value int) int {
return value+21
}).
OrElse(1234)
// 21
Expand Down
9 changes: 4 additions & 5 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,12 @@ func (o Option[T]) Match(onValue func(value T) (T, bool), onNone func() (T, bool
return TupleToOption(onNone())
}

// Map executes the mapper function if value is present or returns None if absent.
// Play: https://go.dev/play/p/mvfP3pcP_eJ
func (o Option[T]) Map(mapper func(value T) (T, bool)) Option[T] {
// Map executes the mapper function for a value.
// Play: https://go.dev/play/p/-A5IMTdRLI-
func (o Option[T]) Map(mapper func(value T) T) Option[T] {
if o.isPresent {
return TupleToOption(mapper(o.value))
return Some(mapper(o.value))
}

return None[T]()
}

Expand Down
8 changes: 4 additions & 4 deletions option_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ func ExampleOption_Match_none() {

func ExampleOption_Map_some() {
some := Some(42)
result := some.Map(func(i int) (int, bool) {
return 1234, true
result := some.Map(func(i int) int {
return 1234
})

fmt.Println(result.IsPresent(), result.OrEmpty())
Expand All @@ -250,8 +250,8 @@ func ExampleOption_Map_some() {

func ExampleOption_Map_none() {
none := None[int]()
result := none.Map(func(i int) (int, bool) {
return 1234, true
result := none.Map(func(i int) int {
return 1234
})

fmt.Println(result.IsPresent(), result.OrEmpty())
Expand Down
8 changes: 4 additions & 4 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ func TestOptionMatch(t *testing.T) {
func TestOptionMap(t *testing.T) {
is := assert.New(t)

opt1 := Some(21).Map(func(i int) (int, bool) {
return i * 2, true
opt1 := Some(21).Map(func(i int) int {
return i * 2
})
opt2 := None[int]().Map(func(i int) (int, bool) {
opt2 := None[int]().Map(func(i int) int {
is.Fail("should not be called")
return 42, true
return 42
})

is.Equal(Option[int]{value: 42, isPresent: true}, opt1)
Expand Down