This commit is contained in:
Nathan Baulch
2024-08-22 09:17:02 +10:00
committed by GitHub
parent edd7060c67
commit db0f4d2171
14 changed files with 59 additions and 57 deletions

View File

@@ -1990,7 +1990,7 @@ ok := lo.Every([]int{0, 1, 2, 3, 4, 5}, []int{0, 6})
### EveryBy
Returns true if the predicate returns true for all of the elements in the collection or if the collection is empty.
Returns true if the predicate returns true for all elements in the collection or if the collection is empty.
```go
b := EveryBy([]int{1, 2, 3, 4}, func(x int) bool {
@@ -2913,7 +2913,9 @@ f(42, -4)
### 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 successful response is returned.
Invokes a function N times until it returns valid output. Returns either the caught error or nil.
When the first argument is less than `1`, the function runs until a successful response is returned.
```go
iter, err := lo.Attempt(42, func(i int) error {
@@ -2953,9 +2955,9 @@ For more advanced retry strategies (delay, exponential backoff...), please take
### AttemptWithDelay
Invokes a function N times until it returns valid output, with a pause between each call. Returning either the caught error or nil.
Invokes a function N times until it returns valid output, with a pause between each call. Returns either the caught error or nil.
When first argument is less than `1`, the function runs until a successful response is returned.
When the first argument is less than `1`, the function runs until a successful response is returned.
```go
iter, duration, err := lo.AttemptWithDelay(5, 2*time.Second, func(i int, duration time.Duration) error {
@@ -2976,9 +2978,9 @@ For more advanced retry strategies (delay, exponential backoff...), please take
### AttemptWhile
Invokes a function N times until it returns valid output. Returning either the caught error or nil, and along with a bool value to identifying whether it needs invoke function continuously. It will terminate the invoke immediately if second bool value is returned with falsy value.
Invokes a function N times until it returns valid output. Returns either the caught error or nil, along with a bool value to determine whether the function should be invoked again. It will terminate the invoke immediately if the second return value is false.
When first argument is less than `1`, the function runs until a successful response is returned.
When the first argument is less than `1`, the function runs until a successful response is returned.
```go
count1, err1 := lo.AttemptWhile(5, func(i int) (error, bool) {
@@ -3001,9 +3003,9 @@ For more advanced retry strategies (delay, exponential backoff...), please take
### AttemptWhileWithDelay
Invokes a function N times until it returns valid output, with a pause between each call. Returning either the caught error or nil, and along with a bool value to identifying whether it needs to invoke function continuously. It will terminate the invoke immediately if second bool value is returned with falsy value.
Invokes a function N times until it returns valid output, with a pause between each call. Returns either the caught error or nil, along with a bool value to determine whether the function should be invoked again. It will terminate the invoke immediately if the second return value is false.
When first argument is less than `1`, the function runs until a successful response is returned.
When the first argument is less than `1`, the function runs until a successful response is returned.
```go
count1, time1, err1 := lo.AttemptWhileWithDelay(5, time.Millisecond, func(i int, d time.Duration) (error, bool) {
@@ -3492,7 +3494,7 @@ if rateLimitErr, ok := lo.ErrorsAs[*RateLimitError](err); ok {
We executed a simple benchmark with a dead-simple `lo.Map` loop:
See the full implementation [here](./benchmark_test.go).
See the full implementation [here](./map_benchmark_test.go).
```go
_ = lo.Map[int64](arr, func(x int64, i int) string {

View File

@@ -98,7 +98,7 @@ func ExampleIfF() {
// 3
}
func ExampleifElse_ElseIf() {
func Example_ifElse_ElseIf() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
@@ -138,7 +138,7 @@ func ExampleifElse_ElseIf() {
// 3
}
func ExampleifElse_ElseIfF() {
func Example_ifElse_ElseIfF() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
@@ -178,7 +178,7 @@ func ExampleifElse_ElseIfF() {
// 3
}
func ExampleifElse_Else() {
func Example_ifElse_Else() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
@@ -218,7 +218,7 @@ func ExampleifElse_Else() {
// 3
}
func ExampleifElse_ElseF() {
func Example_ifElse_ElseF() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
@@ -304,7 +304,7 @@ func ExampleSwitch() {
// 3
}
func ExampleswitchCase_Case() {
func Example_switchCase_Case() {
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
@@ -350,7 +350,7 @@ func ExampleswitchCase_Case() {
// 3
}
func ExampleswitchCase_CaseF() {
func Example_switchCase_CaseF() {
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
@@ -396,7 +396,7 @@ func ExampleswitchCase_CaseF() {
// 3
}
func ExampleswitchCase_Default() {
func Example_switchCase_Default() {
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
@@ -442,7 +442,7 @@ func ExampleswitchCase_Default() {
// 3
}
func ExampleswitchCase_DefaultF() {
func Example_switchCase_DefaultF() {
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").

View File

@@ -363,6 +363,7 @@ func ExampleTryOr5() {
fmt.Printf("%v %v %v %v %v %v\n", value1, value2, value3, value4, value5, ok3)
// Output: 21 hello false {bar} 4.2 false
}
func ExampleTryOr6() {
value1, value2, value3, value4, value5, value6, ok3 := TryOr6(func() (int, string, bool, foo, float64, string, error) {
panic("my error")
@@ -405,8 +406,7 @@ func ExampleTryCatchWithErrorValue() {
// Output: catch: trigger an error
}
type myError struct {
}
type myError struct{}
func (e myError) Error() string {
return "my error"

View File

@@ -535,7 +535,7 @@ func TestTryCatch(t *testing.T) {
TryCatch(func() error {
panic("error")
}, func() {
//error was caught
// error was caught
caught = true
})
is.True(caught)
@@ -544,7 +544,7 @@ func TestTryCatch(t *testing.T) {
TryCatch(func() error {
return nil
}, func() {
//no error to be caught
// no error to be caught
caught = true
})
is.False(caught)
@@ -558,7 +558,7 @@ func TestTryCatchWithErrorValue(t *testing.T) {
TryCatchWithErrorValue(func() error {
panic("error")
}, func(val any) {
//error was caught
// error was caught
caught = val == "error"
})
is.True(caught)
@@ -567,7 +567,7 @@ func TestTryCatchWithErrorValue(t *testing.T) {
TryCatchWithErrorValue(func() error {
return nil
}, func(val any) {
//no error to be caught
// no error to be caught
caught = true
})
is.False(caught)

View File

@@ -441,7 +441,7 @@ func Last[T any](collection []T) (T, bool) {
return collection[length-1], true
}
// Returns the last element of a collection or zero value if empty.
// LastOrEmpty returns the last element of a collection or zero value if empty.
func LastOrEmpty[T any](collection []T) T {
i, _ := Last(collection)
return i

View File

@@ -33,7 +33,7 @@ func Every[T comparable](collection []T, subset []T) bool {
return true
}
// EveryBy returns true if the predicate returns true for all of the elements in the collection or if the collection is empty.
// EveryBy returns true if the predicate returns true for all elements in the collection or if the collection is empty.
func EveryBy[T any](collection []T, predicate func(item T) bool) bool {
for i := range collection {
if !predicate(collection[i]) {

View File

@@ -15,7 +15,6 @@ func ExampleKeys() {
sort.Strings(result)
fmt.Printf("%v", result)
// Output: [bar baz foo]
}
func ExampleUniqKeys() {
@@ -26,7 +25,6 @@ func ExampleUniqKeys() {
sort.Strings(result)
fmt.Printf("%v", result)
// Output: [bar foo]
}
func ExampleValues() {

View File

@@ -87,20 +87,20 @@ func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Comple
// Mean calculates the mean of a collection of numbers.
func Mean[T constraints.Float | constraints.Integer](collection []T) T {
var length T = T(len(collection))
var length = T(len(collection))
if length == 0 {
return 0
}
var sum T = Sum(collection)
var sum = Sum(collection)
return sum / length
}
// MeanBy calculates the mean of a collection of numbers using the given return value from the iteration function.
func MeanBy[T any, R constraints.Float | constraints.Integer](collection []T, iteratee func(item T) R) R {
var length R = R(len(collection))
var length = R(len(collection))
if length == 0 {
return 0
}
var sum R = SumBy(collection, iteratee)
var sum = SumBy(collection, iteratee)
return sum / length
}

View File

@@ -104,7 +104,6 @@ func (d *debounceBy[T]) reset(key T) {
for i := range d.callbacks {
d.callbacks[i](key, count)
}
})
}
@@ -141,7 +140,8 @@ func NewDebounceBy[T comparable](duration time.Duration, f ...func(key T, count
}, d.cancel
}
// 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 successful response is returned.
// Attempt invokes a function N times until it returns valid output. Returns either the caught error or nil.
// When the first argument is less than `1`, the function runs until a successful response is returned.
// Play: https://go.dev/play/p/3ggJZ2ZKcMj
func Attempt(maxIteration int, f func(index int) error) (int, error) {
var err error
@@ -158,8 +158,8 @@ func Attempt(maxIteration int, f func(index int) error) (int, error) {
}
// AttemptWithDelay invokes a function N times until it returns valid output,
// with a pause between each call. Returning either the caught error or nil.
// When first argument is less than `1`, the function runs until a successful
// with a pause between each call. Returns either the caught error or nil.
// When the first argument is less than `1`, the function runs until a successful
// response is returned.
// Play: https://go.dev/play/p/tVs6CygC7m1
func AttemptWithDelay(maxIteration int, delay time.Duration, f func(index int, duration time.Duration) error) (int, time.Duration, error) {
@@ -182,9 +182,9 @@ func AttemptWithDelay(maxIteration int, delay time.Duration, f func(index int, d
}
// AttemptWhile invokes a function N times until it returns valid output.
// Returning either the caught error or nil, and along with a bool value to identify
// whether it needs invoke function continuously. It will terminate the invoke
// immediately if second bool value is returned with falsy value. When first
// Returns either the caught error or nil, along with a bool value to determine
// whether the function should be invoked again. It will terminate the invoke
// immediately if the second return value is false. When the first
// argument is less than `1`, the function runs until a successful response is
// returned.
func AttemptWhile(maxIteration int, f func(int) (error, bool)) (int, error) {
@@ -206,10 +206,10 @@ func AttemptWhile(maxIteration int, f func(int) (error, bool)) (int, error) {
}
// AttemptWhileWithDelay invokes a function N times until it returns valid output,
// with a pause between each call. Returning either the caught error or nil, and along
// with a bool value to identify whether it needs to invoke function continuously.
// It will terminate the invoke immediately if second bool value is returned with falsy
// value. When first argument is less than `1`, the function runs until a successful
// with a pause between each call. Returns either the caught error or nil, along
// with a bool value to determine whether the function should be invoked again.
// It will terminate the invoke immediately if the second return value is false.
// When the first argument is less than `1`, the function runs until a successful
// response is returned.
func AttemptWhileWithDelay(maxIteration int, delay time.Duration, f func(int, time.Duration) (error, bool)) (int, time.Duration, error) {
var err error

View File

@@ -82,7 +82,7 @@ func TestAttemptWithDelay(t *testing.T) {
})
is.Equal(iter1, 1)
is.Greater(dur1, 0*time.Millisecond)
is.GreaterOrEqual(dur1, 0*time.Millisecond)
is.Less(dur1, 1*time.Millisecond)
is.Equal(err1, nil)
is.Equal(iter2, 6)
@@ -187,7 +187,7 @@ func TestAttemptWhileWithDelay(t *testing.T) {
})
is.Equal(iter1, 1)
is.Greater(dur1, 0*time.Millisecond)
is.GreaterOrEqual(dur1, 0*time.Millisecond)
is.Less(dur1, 1*time.Millisecond)
is.Nil(err1)

View File

@@ -88,6 +88,7 @@ func ExampleForEach() {
// 3
// 4
}
func ExampleForEachWhile() {
list := []int64{1, 2, -math.MaxInt, 4}
@@ -103,6 +104,7 @@ func ExampleForEachWhile() {
// 1
// 2
}
func ExampleTimes() {
result := Times(3, func(i int) string {
return strconv.FormatInt(int64(i), 10)

View File

@@ -1006,7 +1006,7 @@ func TestSplice(t *testing.T) {
is.Equal([]string{"a", "b", "c", "d", "e", "f", "g"}, sample)
is.Equal(results, []string{"1", "2", "a", "b", "c", "d", "e", "f", "g"})
// backard
// backward
results = Splice(sample, -2, "1", "2")
is.Equal([]string{"a", "b", "c", "d", "e", "f", "g"}, sample)
is.Equal(results, []string{"a", "b", "c", "d", "e", "1", "2", "f", "g"})

View File

@@ -85,7 +85,7 @@ func ChunkString[T ~string](str T, size int) []T {
return []T{str}
}
var chunks []T = make([]T, 0, ((len(str)-1)/size)+1)
var chunks = make([]T, 0, ((len(str)-1)/size)+1)
currentLen := 0
currentStart := 0
for i := range str {
@@ -181,7 +181,7 @@ func Ellipsis(str string, length int) string {
return str
}
// Elipse trims truncates a string to a specified length and appends an ellipsis if truncated.
// Elipse trims and truncates a string to a specified length and appends an ellipsis if truncated.
//
// Deprecated: Use Ellipsis instead.
func Elipse(str string, length int) string {

View File

@@ -69,7 +69,7 @@ func FromSlicePtr[T any](collection []*T) []T {
})
}
// FromSlicePtr returns a slice with the pointer values or the fallback value.
// FromSlicePtrOr returns a slice with the pointer values or the fallback value.
func FromSlicePtrOr[T any](collection []*T, fallback T) []T {
return Map(collection, func(x *T, _ int) T {
if x == nil {