Skip to content

Commit db0f4d2

Browse files
authored
Tidy up (#519)
1 parent edd7060 commit db0f4d2

14 files changed

+59
-57
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,7 +1990,7 @@ ok := lo.Every([]int{0, 1, 2, 3, 4, 5}, []int{0, 6})
19901990

19911991
### EveryBy
19921992

1993-
Returns true if the predicate returns true for all of the elements in the collection or if the collection is empty.
1993+
Returns true if the predicate returns true for all elements in the collection or if the collection is empty.
19941994

19951995
```go
19961996
b := EveryBy([]int{1, 2, 3, 4}, func(x int) bool {
@@ -2913,7 +2913,9 @@ f(42, -4)
29132913

29142914
### Attempt
29152915

2916-
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.
2916+
Invokes a function N times until it returns valid output. Returns either the caught error or nil.
2917+
2918+
When the first argument is less than `1`, the function runs until a successful response is returned.
29172919

29182920
```go
29192921
iter, err := lo.Attempt(42, func(i int) error {
@@ -2953,9 +2955,9 @@ For more advanced retry strategies (delay, exponential backoff...), please take
29532955

29542956
### AttemptWithDelay
29552957

2956-
Invokes a function N times until it returns valid output, with a pause between each call. Returning either the caught error or nil.
2958+
Invokes a function N times until it returns valid output, with a pause between each call. Returns either the caught error or nil.
29572959

2958-
When first argument is less than `1`, the function runs until a successful response is returned.
2960+
When the first argument is less than `1`, the function runs until a successful response is returned.
29592961

29602962
```go
29612963
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
29762978

29772979
### AttemptWhile
29782980

2979-
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.
2981+
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.
29802982

2981-
When first argument is less than `1`, the function runs until a successful response is returned.
2983+
When the first argument is less than `1`, the function runs until a successful response is returned.
29822984

29832985
```go
29842986
count1, err1 := lo.AttemptWhile(5, func(i int) (error, bool) {
@@ -3001,9 +3003,9 @@ For more advanced retry strategies (delay, exponential backoff...), please take
30013003

30023004
### AttemptWhileWithDelay
30033005

3004-
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.
3006+
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.
30053007

3006-
When first argument is less than `1`, the function runs until a successful response is returned.
3008+
When the first argument is less than `1`, the function runs until a successful response is returned.
30073009

30083010
```go
30093011
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 {
34923494

34933495
We executed a simple benchmark with a dead-simple `lo.Map` loop:
34943496

3495-
See the full implementation [here](./benchmark_test.go).
3497+
See the full implementation [here](./map_benchmark_test.go).
34963498

34973499
```go
34983500
_ = lo.Map[int64](arr, func(x int64, i int) string {

condition_example_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func ExampleIfF() {
9898
// 3
9999
}
100100

101-
func ExampleifElse_ElseIf() {
101+
func Example_ifElse_ElseIf() {
102102
result1 := If(true, 1).
103103
ElseIf(false, 2).
104104
Else(3)
@@ -138,7 +138,7 @@ func ExampleifElse_ElseIf() {
138138
// 3
139139
}
140140

141-
func ExampleifElse_ElseIfF() {
141+
func Example_ifElse_ElseIfF() {
142142
result1 := If(true, 1).
143143
ElseIf(false, 2).
144144
Else(3)
@@ -178,7 +178,7 @@ func ExampleifElse_ElseIfF() {
178178
// 3
179179
}
180180

181-
func ExampleifElse_Else() {
181+
func Example_ifElse_Else() {
182182
result1 := If(true, 1).
183183
ElseIf(false, 2).
184184
Else(3)
@@ -218,7 +218,7 @@ func ExampleifElse_Else() {
218218
// 3
219219
}
220220

221-
func ExampleifElse_ElseF() {
221+
func Example_ifElse_ElseF() {
222222
result1 := If(true, 1).
223223
ElseIf(false, 2).
224224
Else(3)
@@ -304,7 +304,7 @@ func ExampleSwitch() {
304304
// 3
305305
}
306306

307-
func ExampleswitchCase_Case() {
307+
func Example_switchCase_Case() {
308308
result1 := Switch[int, string](1).
309309
Case(1, "1").
310310
Case(2, "2").
@@ -350,7 +350,7 @@ func ExampleswitchCase_Case() {
350350
// 3
351351
}
352352

353-
func ExampleswitchCase_CaseF() {
353+
func Example_switchCase_CaseF() {
354354
result1 := Switch[int, string](1).
355355
Case(1, "1").
356356
Case(2, "2").
@@ -396,7 +396,7 @@ func ExampleswitchCase_CaseF() {
396396
// 3
397397
}
398398

399-
func ExampleswitchCase_Default() {
399+
func Example_switchCase_Default() {
400400
result1 := Switch[int, string](1).
401401
Case(1, "1").
402402
Case(2, "2").
@@ -442,7 +442,7 @@ func ExampleswitchCase_Default() {
442442
// 3
443443
}
444444

445-
func ExampleswitchCase_DefaultF() {
445+
func Example_switchCase_DefaultF() {
446446
result1 := Switch[int, string](1).
447447
Case(1, "1").
448448
Case(2, "2").

errors_example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ func ExampleTryOr5() {
363363
fmt.Printf("%v %v %v %v %v %v\n", value1, value2, value3, value4, value5, ok3)
364364
// Output: 21 hello false {bar} 4.2 false
365365
}
366+
366367
func ExampleTryOr6() {
367368
value1, value2, value3, value4, value5, value6, ok3 := TryOr6(func() (int, string, bool, foo, float64, string, error) {
368369
panic("my error")
@@ -405,8 +406,7 @@ func ExampleTryCatchWithErrorValue() {
405406
// Output: catch: trigger an error
406407
}
407408

408-
type myError struct {
409-
}
409+
type myError struct{}
410410

411411
func (e myError) Error() string {
412412
return "my error"

errors_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestMust(t *testing.T) {
5353
is.PanicsWithValue("operation should fail: assert.AnError general error for testing", func() {
5454
Must0(cb(), "operation should fail")
5555
})
56-
56+
5757
is.PanicsWithValue("must: invalid err type 'int', should either be a bool or an error", func() {
5858
Must0(0)
5959
})
@@ -271,11 +271,11 @@ func TestTry(t *testing.T) {
271271
func TestTryX(t *testing.T) {
272272
t.Parallel()
273273
is := assert.New(t)
274-
274+
275275
is.True(Try1(func() error {
276276
return nil
277277
}))
278-
278+
279279
is.True(Try2(func() (string, error) {
280280
return "", nil
281281
}))
@@ -295,11 +295,11 @@ func TestTryX(t *testing.T) {
295295
is.True(Try6(func() (string, string, string, string, string, error) {
296296
return "", "", "", "", "", nil
297297
}))
298-
298+
299299
is.False(Try1(func() error {
300300
panic("error")
301301
}))
302-
302+
303303
is.False(Try2(func() (string, error) {
304304
panic("error")
305305
}))
@@ -319,11 +319,11 @@ func TestTryX(t *testing.T) {
319319
is.False(Try6(func() (string, string, string, string, string, error) {
320320
panic("error")
321321
}))
322-
322+
323323
is.False(Try1(func() error {
324324
return errors.New("foo")
325325
}))
326-
326+
327327
is.False(Try2(func() (string, error) {
328328
return "", errors.New("foo")
329329
}))
@@ -513,13 +513,13 @@ func TestTryWithErrorValue(t *testing.T) {
513513
})
514514
is.False(ok)
515515
is.Equal("error", err)
516-
516+
517517
err, ok = TryWithErrorValue(func() error {
518518
return errors.New("foo")
519519
})
520520
is.False(ok)
521521
is.EqualError(err.(error), "foo")
522-
522+
523523
err, ok = TryWithErrorValue(func() error {
524524
return nil
525525
})
@@ -535,7 +535,7 @@ func TestTryCatch(t *testing.T) {
535535
TryCatch(func() error {
536536
panic("error")
537537
}, func() {
538-
//error was caught
538+
// error was caught
539539
caught = true
540540
})
541541
is.True(caught)
@@ -544,7 +544,7 @@ func TestTryCatch(t *testing.T) {
544544
TryCatch(func() error {
545545
return nil
546546
}, func() {
547-
//no error to be caught
547+
// no error to be caught
548548
caught = true
549549
})
550550
is.False(caught)
@@ -558,7 +558,7 @@ func TestTryCatchWithErrorValue(t *testing.T) {
558558
TryCatchWithErrorValue(func() error {
559559
panic("error")
560560
}, func(val any) {
561-
//error was caught
561+
// error was caught
562562
caught = val == "error"
563563
})
564564
is.True(caught)
@@ -567,7 +567,7 @@ func TestTryCatchWithErrorValue(t *testing.T) {
567567
TryCatchWithErrorValue(func() error {
568568
return nil
569569
}, func(val any) {
570-
//no error to be caught
570+
// no error to be caught
571571
caught = true
572572
})
573573
is.False(caught)

find.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ func Last[T any](collection []T) (T, bool) {
441441
return collection[length-1], true
442442
}
443443

444-
// Returns the last element of a collection or zero value if empty.
444+
// LastOrEmpty returns the last element of a collection or zero value if empty.
445445
func LastOrEmpty[T any](collection []T) T {
446446
i, _ := Last(collection)
447447
return i

intersect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func Every[T comparable](collection []T, subset []T) bool {
3333
return true
3434
}
3535

36-
// EveryBy returns true if the predicate returns true for all of the elements in the collection or if the collection is empty.
36+
// EveryBy returns true if the predicate returns true for all elements in the collection or if the collection is empty.
3737
func EveryBy[T any](collection []T, predicate func(item T) bool) bool {
3838
for i := range collection {
3939
if !predicate(collection[i]) {

map_example_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ func ExampleKeys() {
1515
sort.Strings(result)
1616
fmt.Printf("%v", result)
1717
// Output: [bar baz foo]
18-
1918
}
2019

2120
func ExampleUniqKeys() {
@@ -26,7 +25,6 @@ func ExampleUniqKeys() {
2625
sort.Strings(result)
2726
fmt.Printf("%v", result)
2827
// Output: [bar foo]
29-
3028
}
3129

3230
func ExampleValues() {

math.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,20 @@ func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Comple
8787

8888
// Mean calculates the mean of a collection of numbers.
8989
func Mean[T constraints.Float | constraints.Integer](collection []T) T {
90-
var length T = T(len(collection))
90+
var length = T(len(collection))
9191
if length == 0 {
9292
return 0
9393
}
94-
var sum T = Sum(collection)
94+
var sum = Sum(collection)
9595
return sum / length
9696
}
9797

9898
// MeanBy calculates the mean of a collection of numbers using the given return value from the iteration function.
9999
func MeanBy[T any, R constraints.Float | constraints.Integer](collection []T, iteratee func(item T) R) R {
100-
var length R = R(len(collection))
100+
var length = R(len(collection))
101101
if length == 0 {
102102
return 0
103103
}
104-
var sum R = SumBy(collection, iteratee)
104+
var sum = SumBy(collection, iteratee)
105105
return sum / length
106106
}

retry.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ func (d *debounceBy[T]) reset(key T) {
104104
for i := range d.callbacks {
105105
d.callbacks[i](key, count)
106106
}
107-
108107
})
109108
}
110109

@@ -141,7 +140,8 @@ func NewDebounceBy[T comparable](duration time.Duration, f ...func(key T, count
141140
}, d.cancel
142141
}
143142

144-
// 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.
143+
// Attempt invokes a function N times until it returns valid output. Returns either the caught error or nil.
144+
// When the first argument is less than `1`, the function runs until a successful response is returned.
145145
// Play: https://go.dev/play/p/3ggJZ2ZKcMj
146146
func Attempt(maxIteration int, f func(index int) error) (int, error) {
147147
var err error
@@ -158,8 +158,8 @@ func Attempt(maxIteration int, f func(index int) error) (int, error) {
158158
}
159159

160160
// AttemptWithDelay invokes a function N times until it returns valid output,
161-
// with a pause between each call. Returning either the caught error or nil.
162-
// When first argument is less than `1`, the function runs until a successful
161+
// with a pause between each call. Returns either the caught error or nil.
162+
// When the first argument is less than `1`, the function runs until a successful
163163
// response is returned.
164164
// Play: https://go.dev/play/p/tVs6CygC7m1
165165
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
182182
}
183183

184184
// AttemptWhile invokes a function N times until it returns valid output.
185-
// Returning either the caught error or nil, and along with a bool value to identify
186-
// whether it needs invoke function continuously. It will terminate the invoke
187-
// immediately if second bool value is returned with falsy value. When first
185+
// Returns either the caught error or nil, along with a bool value to determine
186+
// whether the function should be invoked again. It will terminate the invoke
187+
// immediately if the second return value is false. When the first
188188
// argument is less than `1`, the function runs until a successful response is
189189
// returned.
190190
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) {
206206
}
207207

208208
// AttemptWhileWithDelay invokes a function N times until it returns valid output,
209-
// with a pause between each call. Returning either the caught error or nil, and along
210-
// with a bool value to identify whether it needs to invoke function continuously.
211-
// It will terminate the invoke immediately if second bool value is returned with falsy
212-
// value. When first argument is less than `1`, the function runs until a successful
209+
// with a pause between each call. Returns either the caught error or nil, along
210+
// with a bool value to determine whether the function should be invoked again.
211+
// It will terminate the invoke immediately if the second return value is false.
212+
// When the first argument is less than `1`, the function runs until a successful
213213
// response is returned.
214214
func AttemptWhileWithDelay(maxIteration int, delay time.Duration, f func(int, time.Duration) (error, bool)) (int, time.Duration, error) {
215215
var err error

retry_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestAttemptWithDelay(t *testing.T) {
8282
})
8383

8484
is.Equal(iter1, 1)
85-
is.Greater(dur1, 0*time.Millisecond)
85+
is.GreaterOrEqual(dur1, 0*time.Millisecond)
8686
is.Less(dur1, 1*time.Millisecond)
8787
is.Equal(err1, nil)
8888
is.Equal(iter2, 6)
@@ -187,7 +187,7 @@ func TestAttemptWhileWithDelay(t *testing.T) {
187187
})
188188

189189
is.Equal(iter1, 1)
190-
is.Greater(dur1, 0*time.Millisecond)
190+
is.GreaterOrEqual(dur1, 0*time.Millisecond)
191191
is.Less(dur1, 1*time.Millisecond)
192192
is.Nil(err1)
193193

0 commit comments

Comments
 (0)