Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/moPublic

feat(option,result): add a map functionList method#35

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
zonewave wants to merge1 commit intosamber:master
base:master
Choose a base branch
Loading
fromzonewave:feat_maps
Open
Show file tree
Hide file tree
Changes fromall 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
16 changes: 16 additions & 0 deletionsoption.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -153,6 +153,22 @@ func (o Option[T]) Map(mapper func(value T) (T, bool)) Option[T] {
return None[T]()
}

func (o Option[T]) Maps(mapperList ...func(value T) (T, bool)) Option[T] {
if !o.isPresent {
return None[T]()
}
tmpValue := o.value
for _, mapper := range mapperList {
var ok bool
tmpValue, ok = mapper(tmpValue)
if !ok {
return None[T]()
}
}

return Some(tmpValue)
}

// MapNone executes the mapper function if value is absent or returns Option.
// Play: https://go.dev/play/p/_KaHWZ6Q17b
func (o Option[T]) MapNone(mapper func() (T, bool)) Option[T] {
Expand Down
28 changes: 28 additions & 0 deletionsoption_example_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -258,6 +258,34 @@ func ExampleOption_Map_none() {
// Output: false 0
}

func ExampleOption_Maps_some() {
some := Some(42)
result := some.Maps(
func(i int) (int, bool) {
return 1234, true
},
func(value int) (int, bool) {
return value + 1, true
},
)

fmt.Println(result.IsPresent(), result.OrEmpty())
// Output: true 1235
}
func ExampleOption_Maps_none() {
none := None[int]()
result := none.Maps(
func(i int) (int, bool) {
return 1234, true
},
func(value int) (int, bool) {
return value + 1, true
},
)

fmt.Println(result.IsPresent(), result.OrEmpty())
// Output: false 0
}
func ExampleOption_MapNone_some() {
some := Some(42)
result := some.MapNone(func() (int, bool) {
Expand Down
37 changes: 37 additions & 0 deletionsoption_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -173,6 +173,43 @@ func TestOptionMap(t *testing.T) {
is.Equal(Option[int]{value: 0, isPresent: false}, opt2)
}

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

mul2 := func(i int) (int, bool) {
return i * 2, true
}
inc1 := func(i int) (int, bool) {
return i + 1, true
}
returnFalse := func(i int) (int, bool) {
return 0, false
}
t.Run("maps 2 func ok ", func(t *testing.T) {
v := Some(21).Maps(mul2, inc1)
is.Equal(Option[int]{value: 43, isPresent: true}, v)
})
t.Run("maps first ok func, second failed func", func(t *testing.T) {
v := Some(21).Maps(mul2, returnFalse)
is.False(v.IsPresent())
})

t.Run("maps 2 failed func", func(t *testing.T) {
v := Some(21).Maps(returnFalse, func(i int) (int, bool) {
is.Fail("should not be called")
return 42, true
})
is.False(v.IsPresent())
})
t.Run("maps none", func(t *testing.T) {
v := None[int]().Maps(mul2, func(i int) (int, bool) {
is.Fail("should not be called")
return 42, true
})
is.False(v.IsPresent())
})
}

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

Expand Down
15 changes: 15 additions & 0 deletionsresult.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -146,6 +146,21 @@ func (r Result[T]) Map(mapper func(value T) (T, error)) Result[T] {

return Err[T](r.err)
}
func (r Result[T]) Maps(mapperList ...func(value T) (T, error)) Result[T] {
if r.isErr {
return Err[T](r.err)
}

tmpValue := r.value
for _, mapper := range mapperList {
var err error
tmpValue, err = mapper(tmpValue)
if err != nil {
return Err[T](err)
}
}
return Ok[T](tmpValue)
}

// MapErr executes the mapper function if Result is invalid. It returns a new Result.
// Play: https://go.dev/play/p/WraZixg9GGf
Expand Down
30 changes: 30 additions & 0 deletionsresult_example_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -299,6 +299,36 @@ func ExampleResult_Map_err() {
// Output: true 0 error
}

func ExampleResult_Maps_ok() {
ok := Ok(42)
result := ok.Maps(
func(i int) (int, error) {
return i * 2, nil
},
func(i int) (int, error) {
return i + 1, nil
},
)

fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
// Output: false 85 <nil>
}

func ExampleResult_Maps_err() {
ko := Err[int](err)
result := ko.Maps(
func(i int) (int, error) {
return i * 2, nil
},
func(i int) (int, error) {
return i + 1, nil
},
)

fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
// Output: true 0 error
}

func ExampleResult_MapErr_ok() {
ok := Ok(42)
result := ok.MapErr(
Expand Down
36 changes: 36 additions & 0 deletionsresult_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -174,6 +174,42 @@ func TestResultMap(t *testing.T) {
is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, opt2)
}

func TestResultMaps(t *testing.T) {
is := assert.New(t)
mul2 := func(i int) (int, error) {
return i * 2, nil
}
inc1 := func(i int) (int, error) {
return i + 1, nil
}
returnErr := func(i int) (int, error) {
return 0, assert.AnError
}
t.Run("maps 2 func ok ", func(t *testing.T) {
v := Ok(21).Maps(mul2, inc1)
is.Equal(Result[int]{value: 43, isErr: false, err: nil}, v)
})
t.Run("maps first ok func, second failed func", func(t *testing.T) {
v := Ok(21).Maps(mul2, returnErr)
is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, v)
})

t.Run("maps 2 failed func", func(t *testing.T) {
v := Ok(21).Maps(returnErr, func(i int) (int, error) {
is.Fail("should not be called")
return 42, nil
})
is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, v)
})
t.Run("errors maps ", func(t *testing.T) {
v := Err[int](assert.AnError).Maps(mul2, func(i int) (int, error) {
is.Fail("should not be called")
return 42, nil
})
is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, v)
})
}

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

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp