Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBaulch committed Aug 20, 2024
1 parent edd7060 commit c66c6b8
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 57 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions condition_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func ExampleIfF() {
// 3
}

func ExampleifElse_ElseIf() {
func Example_ifElse_ElseIf() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
Expand Down Expand Up @@ -138,7 +138,7 @@ func ExampleifElse_ElseIf() {
// 3
}

func ExampleifElse_ElseIfF() {
func Example_ifElse_ElseIfF() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
Expand Down Expand Up @@ -178,7 +178,7 @@ func ExampleifElse_ElseIfF() {
// 3
}

func ExampleifElse_Else() {
func Example_ifElse_Else() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
Expand Down Expand Up @@ -218,7 +218,7 @@ func ExampleifElse_Else() {
// 3
}

func ExampleifElse_ElseF() {
func Example_ifElse_ElseF() {
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
Expand Down Expand Up @@ -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").
Expand Down Expand Up @@ -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").
Expand Down Expand Up @@ -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").
Expand Down Expand Up @@ -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").
Expand Down
4 changes: 2 additions & 2 deletions errors_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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"
Expand Down
26 changes: 13 additions & 13 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestMust(t *testing.T) {
is.PanicsWithValue("operation should fail: assert.AnError general error for testing", func() {
Must0(cb(), "operation should fail")
})

is.PanicsWithValue("must: invalid err type 'int', should either be a bool or an error", func() {
Must0(0)
})
Expand Down Expand Up @@ -271,11 +271,11 @@ func TestTry(t *testing.T) {
func TestTryX(t *testing.T) {
t.Parallel()
is := assert.New(t)

is.True(Try1(func() error {
return nil
}))

is.True(Try2(func() (string, error) {
return "", nil
}))
Expand All @@ -295,11 +295,11 @@ func TestTryX(t *testing.T) {
is.True(Try6(func() (string, string, string, string, string, error) {
return "", "", "", "", "", nil
}))

is.False(Try1(func() error {
panic("error")
}))

is.False(Try2(func() (string, error) {
panic("error")
}))
Expand All @@ -319,11 +319,11 @@ func TestTryX(t *testing.T) {
is.False(Try6(func() (string, string, string, string, string, error) {
panic("error")
}))

is.False(Try1(func() error {
return errors.New("foo")
}))

is.False(Try2(func() (string, error) {
return "", errors.New("foo")
}))
Expand Down Expand Up @@ -513,13 +513,13 @@ func TestTryWithErrorValue(t *testing.T) {
})
is.False(ok)
is.Equal("error", err)

err, ok = TryWithErrorValue(func() error {
return errors.New("foo")
})
is.False(ok)
is.EqualError(err.(error), "foo")

err, ok = TryWithErrorValue(func() error {
return nil
})
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion find.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion intersect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down
2 changes: 0 additions & 2 deletions map_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func ExampleKeys() {
sort.Strings(result)
fmt.Printf("%v", result)
// Output: [bar baz foo]

}

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

}

func ExampleValues() {
Expand Down
8 changes: 4 additions & 4 deletions math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 11 additions & 11 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func (d *debounceBy[T]) reset(key T) {
for i := range d.callbacks {
d.callbacks[i](key, count)
}

})
}

Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
Loading

0 comments on commit c66c6b8

Please sign in to comment.