Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanBaulch committed Aug 19, 2024
1 parent 741cdfd commit 8ff6a64
Show file tree
Hide file tree
Showing 20 changed files with 72 additions and 70 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Improvement:
- lo.Associate / lo.SliceToMap: faster memory allocation

Chore:
- Remove *_test.go files from releases, in order to cleanup dev dependencies
- Remove *_test.go files from releases, in order to clean up dev dependencies

## 1.36.0 (2022-11-28)

Expand Down
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ Supported helpers for tuples:
Supported helpers for time and duration:

- [Duration](#duration)
- [Duration0 -> Duration10](#duration0-duration10)
- [Duration0 -> Duration10](#duration0---duration10)

Supported helpers for channels:

Expand Down 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
4 changes: 2 additions & 2 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func DispatchingStrategyRandom[T any](msg T, index uint64, channels []<-chan T)
// DispatchingStrategyWeightedRandom distributes messages in a weighted manner.
// If the channel capacity is exceeded, another random channel will be selected and so on.
func DispatchingStrategyWeightedRandom[T any](weights []int) DispatchingStrategy[T] {
seq := []int{}
var seq []int

for i := 0; i < len(weights); i++ {
for j := 0; j < weights[i]; j++ {
Expand Down Expand Up @@ -169,7 +169,7 @@ func SliceToChannel[T any](bufferSize int, collection []T) <-chan T {

// ChannelToSlice returns a slice built from channels items. Blocks until channel closes.
func ChannelToSlice[T any](ch <-chan T) []T {
collection := []T{}
var collection []T

for item := range ch {
collection = append(collection, item)
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
10 changes: 5 additions & 5 deletions math.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func RangeFrom[T constraints.Integer | constraints.Float](start T, elementNum in
// step set to zero will return empty array.
// Play: https://go.dev/play/p/0r6VimXAi9H
func RangeWithSteps[T constraints.Integer | constraints.Float](start, end, step T) []T {
result := []T{}
var result []T
if start == end || step == 0 {
return result
}
Expand Down 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
}
2 changes: 1 addition & 1 deletion parallel/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func GroupBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(it
// of running each element of collection through iteratee.
// `iteratee` is call in parallel.
func PartitionBy[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) K) []Slice {
result := []Slice{}
var result []Slice
seen := map[K]int{}

var mu sync.Mutex
Expand Down
Loading

0 comments on commit 8ff6a64

Please sign in to comment.