Skip to content

Commit

Permalink
doc(coalesce): adding doc for this new helper
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Apr 21, 2022
1 parent 9163c91 commit dff318b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 73 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ Other functional programming helpers:
- ToPtr
- ToSlicePtr
- Empty
- Coalesce

Concurrency helpers:

Expand Down Expand Up @@ -1161,6 +1162,23 @@ lo.Empty[bool]()
// false
```

### Coalesce

Returns the first non-empty arguments. Arguments must be comparable.

```go
result, ok := Coalesce(0, 1, 2, 3)
// 1 true

result, ok := Coalesce("")
// "" false

var nilStr *string
str := "foobar"
result, ok := Coalesce[*string](nil, nilStr, &str)
// &"foobar" true
```

### Attempt

Invokes a function N times until it returns valid output. Returning either the caught error or nil. When first argument is less than `1`, the function runs until a sucessfull response is returned.
Expand Down
16 changes: 2 additions & 14 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package lo

import (
"fmt"
"golang.org/x/exp/constraints"
"math"
"math/rand"

"golang.org/x/exp/constraints"
)

// import "golang.org/x/exp/constraints"
Expand Down Expand Up @@ -237,16 +238,3 @@ func Samples[T any](collection []T, count int) []T {

return results
}

// Coalesce returns the first non-default arguments. Arguments must be comparable.
func Coalesce[T comparable](v ...T) (result T, ok bool) {
for _, e := range v {
if e != result {
result = e
ok = true
return
}
}

return
}
58 changes: 0 additions & 58 deletions find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,61 +214,3 @@ func TestSamples(t *testing.T) {
is.Equal(result1, []string{"a", "b", "c"})
is.Equal(result2, []string{})
}

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

newStr := func(v string) *string { return &v }
var nilStr *string
str1 := newStr("str1")
str2 := newStr("str2")

type structType struct {
field1 int
field2 float64
}
var zeroStruct structType
struct1 := structType{1, 1.0}
struct2 := structType{2, 2.0}

result1, ok1 := Coalesce[int]()
result2, ok2 := Coalesce(3)
result3, ok3 := Coalesce[*string](nil, nilStr)
result4, ok4 := Coalesce(nilStr, str1)
result5, ok5 := Coalesce(nilStr, str1, str2)
result6, ok6 := Coalesce(str1, str2, nilStr)
result7, ok7 := Coalesce(0, 1, 2, 3)
result8, ok8 := Coalesce(zeroStruct)
result9, ok9 := Coalesce(zeroStruct, struct1)
result10, ok10 := Coalesce(zeroStruct, struct1, struct2)

is.Equal(0, result1)
is.False(ok1)

is.Equal(3, result2)
is.True(ok2)

is.Nil(result3)
is.False(ok3)

is.Equal(str1, result4)
is.True(ok4)

is.Equal(str1, result5)
is.True(ok5)

is.Equal(str1, result6)
is.True(ok6)

is.Equal(result7, 1)
is.True(ok7)

is.Equal(result8, zeroStruct)
is.False(ok8)

is.Equal(result9, struct1)
is.True(ok9)

is.Equal(result10, struct1)
is.True(ok10)
}
15 changes: 14 additions & 1 deletion pointers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func ToPtr[T any](x T) *T {

// ToSlicePtr returns a slice of pointer copy of value.
func ToSlicePtr[T any](collection []T) []*T {
return Map(collection, func (x T, _ int) *T {
return Map(collection, func(x T, _ int) *T {
return &x
})
}
Expand All @@ -17,3 +17,16 @@ func Empty[T any]() T {
var t T
return t
}

// Coalesce returns the first non-empty arguments. Arguments must be comparable.
func Coalesce[T comparable](v ...T) (result T, ok bool) {
for _, e := range v {
if e != result {
result = e
ok = true
return
}
}

return
}
58 changes: 58 additions & 0 deletions pointers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,61 @@ func TestToSlicePtr(t *testing.T) {

is.Equal(result1, []*string{&str1, &str2})
}

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

newStr := func(v string) *string { return &v }
var nilStr *string
str1 := newStr("str1")
str2 := newStr("str2")

type structType struct {
field1 int
field2 float64
}
var zeroStruct structType
struct1 := structType{1, 1.0}
struct2 := structType{2, 2.0}

result1, ok1 := Coalesce[int]()
result2, ok2 := Coalesce(3)
result3, ok3 := Coalesce[*string](nil, nilStr)
result4, ok4 := Coalesce(nilStr, str1)
result5, ok5 := Coalesce(nilStr, str1, str2)
result6, ok6 := Coalesce(str1, str2, nilStr)
result7, ok7 := Coalesce(0, 1, 2, 3)
result8, ok8 := Coalesce(zeroStruct)
result9, ok9 := Coalesce(zeroStruct, struct1)
result10, ok10 := Coalesce(zeroStruct, struct1, struct2)

is.Equal(0, result1)
is.False(ok1)

is.Equal(3, result2)
is.True(ok2)

is.Nil(result3)
is.False(ok3)

is.Equal(str1, result4)
is.True(ok4)

is.Equal(str1, result5)
is.True(ok5)

is.Equal(str1, result6)
is.True(ok6)

is.Equal(result7, 1)
is.True(ok7)

is.Equal(result8, zeroStruct)
is.False(ok8)

is.Equal(result9, struct1)
is.True(ok9)

is.Equal(result10, struct1)
is.True(ok10)
}

0 comments on commit dff318b

Please sign in to comment.