Skip to content

Commit

Permalink
integrate maybe (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Gennaro Del Sorbo <gdelsorbo@newrelic.com>
  • Loading branch information
jd78 and Gennaro Del Sorbo authored Oct 26, 2023
1 parent aae3455 commit cf595fb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,32 @@ result, err := patternmatch.ResultMatch[int, string](70).
// Result will be "> 50", nil

result, err := patternmatch.ResultMatch[int, string](2).
When(func(x int) bool { return x > 100 }, func() string { return "> 100" }).
When(func(x int) bool { return x > 50 }, func() string { return "> 50" }).
When(func(x int) bool { return x > 10 }, func() string { return "> 10" }).
Result()
When(func(x int) bool { return x > 100 }, func() string { return "> 100" }).
When(func(x int) bool { return x > 50 }, func() string { return "> 50" }).
When(func(x int) bool { return x > 10 }, func() string { return "> 10" }).
Result()

// result will be "", error{"pattern not matched"}

maybeResult := patternmatch.ResultMatch[int, string](11).
When(func(x int) bool { return x > 100 }, func() string { return "> 100" }).
When(func(x int) bool { return x > 50 }, func() string { return "> 50" }).
When(func(x int) bool { return x > 10 }, func() string { return "> 10" }).
MaybeResult()

// result will be Optional[string], error{"pattern not matched"}

```

Using defaults:

```go

result, err := patternmatch.ResultMatch[int, string](2).
When(func(x int) bool { return x.(int) > 100 }, func() string { return "> 100" }).
When(func(x int) bool { return x.(int) > 50 }, func() string { return "> 50" }).
When(func(x int) bool { return x.(int) > 10 }, func() string { return "> 10" }).
ResultOrDefault("< 10")
When(func(x int) bool { return x.(int) > 100 }, func() string { return "> 100" }).
When(func(x int) bool { return x.(int) > 50 }, func() string { return "> 50" }).
When(func(x int) bool { return x.(int) > 10 }, func() string { return "> 10" }).
ResultOrDefault("< 10")

// Result will be "< 10", nil

Expand All @@ -42,10 +50,10 @@ Non-conditional pattern matching:

```go
result, err := patternmatch.ResultMatch[interface{}, string](5).
WhenValue(5, func() string { return "is int 5" }).
WhenValue("10", func() string { return "is string 10" }).
WhenValue(true, func() string { return "is bool" }).
Result()
WhenValue(5, func() string { return "is int 5" }).
WhenValue("10", func() string { return "is string 10" }).
WhenValue(true, func() string { return "is bool" }).
Result()

// Result will be "is int 5", nil
```
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module gopatternmatching
module github.com/jd78/gopatternmatching

go 1.21

Expand All @@ -9,6 +9,7 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/jd78/go-optional v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jd78/go-optional v1.0.1 h1:sbss/rnM6dYO4rFohLAK80v+S8SGKwqbany9Mjdz7Xw=
github.com/jd78/go-optional v1.0.1/go.mod h1:7rD8V+s0NomoZ+X13XTmlBpNERPP/exRr7hqHpCle14=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
12 changes: 11 additions & 1 deletion pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"

"github.com/google/go-cmp/cmp"
gooptional "github.com/jd78/go-optional"
)

type match[T any] struct {
Expand Down Expand Up @@ -95,7 +96,7 @@ func (m matchResult[T, K]) WhenValue(val T, a func() K) matchResult[T, K] {
return m
}

// Get the result from the pattern or throws if not matched
// Get the result from the pattern
func (m matchResult[T, K]) Result() (K, error) {
var zeroK K
if cmp.Equal(m.output, zeroK) {
Expand All @@ -104,6 +105,15 @@ func (m matchResult[T, K]) Result() (K, error) {
return m.output, nil
}

// Get the optional result from the pattern or throws if not matched
func (m matchResult[T, K]) MaybeResult() gooptional.Optional[K] {
var zeroK K
if cmp.Equal(m.output, zeroK) {
return gooptional.Error[K](errors.New("pattern not matched"))
}
return gooptional.Some[K](m.output)
}

// Get the result from the pattern or the passed default value
func (m matchResult[T, K]) ResultOrDefault(def K) K {
var zeroK K
Expand Down
13 changes: 8 additions & 5 deletions pattern_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package patternmatch

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -83,8 +82,12 @@ func TestIsMatched(t *testing.T) {
assert.True(t, b)
}

func TestT(t *testing.T) {
Match(5).
WhenValue(5, func() { fmt.Println("is 5") }).
WhenValue(6, func() { fmt.Println("is 6") })
func TestMaybe(t *testing.T) {
maybeOpt := ResultMatch[int, string](0).
WhenValue(5, func() string { return "5" }).
WhenValue(0, func() string { return "is 0" }).
MaybeResult()

assert.True(t, maybeOpt.IsSome())
assert.Equal(t, "is 0", maybeOpt.Get())
}

0 comments on commit cf595fb

Please sign in to comment.