Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/samber/lo into feat-must-any
Browse files Browse the repository at this point in the history
  • Loading branch information
wirekang committed Apr 22, 2022
2 parents 972de5c + 1bec3a3 commit a894d7c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 11 deletions.
26 changes: 22 additions & 4 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 @@ -747,13 +748,13 @@ Clamps number within the inclusive lower and upper bounds.

```go
r1 := lo.Clamp(0, -10, 10)
// 10
// 0

r2 := lo.Clamp(-42, -10, 10)
// -42
// -10

r3 := lo.Clamp(42, -10, 10)
// 42
// 10
```

### T2 -> T9
Expand Down Expand Up @@ -840,7 +841,7 @@ Returns the difference between two collections.
left, right := lo.Difference[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2, 6})
// []int{1, 3, 4, 5}, []int{6}

left, right := Difference[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 1, 2, 3, 4, 5})
left, right := lo.Difference[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 1, 2, 3, 4, 5})
// []int{}, []int{}
```

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
10 changes: 5 additions & 5 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ func Must1[T any](val T, err any) T {
return Must(val, err)
}

// Must2 has the same behavior than Must, but callback returns 2 variable2.
// Must2 has the same behavior than Must, but callback returns 2 variables.
func Must2[T1 any, T2 any](val1 T1, val2 T2, err any) (T1, T2) {
must(err)
return val1, val2
}

// Must3 has the same behavior than Must, but callback returns 2 variable2.
// Must3 has the same behavior than Must, but callback returns 3 variables.
func Must3[T1 any, T2 any, T3 any](val1 T1, val2 T2, val3 T3, err any) (T1, T2, T3) {
must(err)
return val1, val2, val3
}

// Must4 has the same behavior than Must, but callback returns 2 variable2.
// Must4 has the same behavior than Must, but callback returns 4 variables.
func Must4[T1 any, T2 any, T3 any, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err any) (T1, T2, T3, T4) {
must(err)
return val1, val2, val3, val4
}

// Must5 has the same behavior than Must, but callback returns 2 variable2.
// Must5 has the same behavior than Must, but callback returns 5 variables.
func Must5[T1 any, T2 any, T3 any, T4 any, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, err any) (T1, T2, T3, T4, T5) {
must(err)
return val1, val2, val3, val4, val5
}

// Must6 has the same behavior than Must, but callback returns 2 variable2.
// Must6 has the same behavior than Must, but callback returns 6 variables.
func Must6[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, val6 T6, err any) (T1, T2, T3, T4, T5, T6) {
must(err)
return val1, val2, val3, val4, val5, val6
Expand Down
3 changes: 2 additions & 1 deletion 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
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 a894d7c

Please sign in to comment.