Skip to content

Commit

Permalink
feat: name each callback/iteratee/predicate arguments in order to imp…
Browse files Browse the repository at this point in the history
…rove autocompletion

closes #242
  • Loading branch information
samber committed Oct 13, 2022
1 parent a2c5202 commit 7887963
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 84 deletions.
20 changes: 16 additions & 4 deletions README.md
Expand Up @@ -53,6 +53,18 @@ names := lo.Uniq[string]([]string{"Samuel", "John", "Samuel"})

Most of the time, the compiler will be able to infer the type so that you can call: `lo.Uniq([]string{...})`.

### Tips for lazy developers

I cannot recommend it, but in case you are too lazy for repeating `lo.` everywhere, you can import the entire library into the namespace.

```go
import (
. "github.com/samber/lo"
)
```

I take no responsibility on this junk. 😁 💩

## 🤠 Spec

GoDoc: [https://godoc.org/github.com/samber/lo](https://godoc.org/github.com/samber/lo)
Expand Down Expand Up @@ -1337,7 +1349,7 @@ Many distributions strategies are available:
- [lo.DispatchingStrategyWeightedRandom](./channel.go): Distributes messages in a weighted manner.
- [lo.DispatchingStrategyFirst](./channel.go): Distributes messages in the first non-full channel.
- [lo.DispatchingStrategyLeast](./channel.go): Distributes messages in the emptiest channel.
- [lo.DispatchingStrategyMost](./channel.go): Distributes to the fulliest channel.
- [lo.DispatchingStrategyMost](./channel.go): Distributes to the fullest channel.

Some strategies bring fallback, in order to favor non-blocking behaviors. See implementations.

Expand Down Expand Up @@ -1795,7 +1807,7 @@ uniqueValues := lo.FindUniquesBy[int, int]([]int{3, 4, 5, 6, 7}, func(i int) int

### FindDuplicates

Returns a slice with the first occurence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the array.
Returns a slice with the first occurrence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the array.

```go
duplicatedValues := lo.FindDuplicates[int]([]int{1, 2, 2, 1, 2, 3})
Expand All @@ -1804,7 +1816,7 @@ duplicatedValues := lo.FindDuplicates[int]([]int{1, 2, 2, 1, 2, 3})

### FindDuplicatesBy

Returns a slice with the first occurence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is invoked for each element in array to generate the criterion by which uniqueness is computed.
Returns a slice with the first occurrence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is invoked for each element in array to generate the criterion by which uniqueness is computed.

```go
duplicatedValues := lo.FindDuplicatesBy[int, int]([]int{3, 4, 5, 6, 7}, func(i int) int {
Expand Down Expand Up @@ -2391,7 +2403,7 @@ val := lo.Must(time.Parse("2006-01-02", "bad-value"))

### Must{0->6}

Must\* has the same behavior than Must, but returns multiple values.
Must\* has the same behavior as Must, but returns multiple values.

```go
func example0() (error)
Expand Down
8 changes: 4 additions & 4 deletions channel.go
Expand Up @@ -94,7 +94,7 @@ func DispatchingStrategyRandom[T any](msg T, index uint64, channels []<-chan T)
}
}

// DispatchingStrategyRandom distributes messages in a weighted manner.
// 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{}
Expand Down Expand Up @@ -140,7 +140,7 @@ func DispatchingStrategyLeast[T any](msg T, index uint64, channels []<-chan T) i
})
}

// DispatchingStrategyMost distributes messages in the fulliest channel.
// DispatchingStrategyMost distributes messages in the fullest channel.
// If the channel capacity is exceeded, the next channel will be selected and so on.
func DispatchingStrategyMost[T any](msg T, index uint64, channels []<-chan T) int {
seq := Range(len(channels))
Expand Down Expand Up @@ -193,7 +193,7 @@ func Generator[T any](bufferSize int, generator func(yield func(T))) <-chan T {
}

// Batch creates a slice of n elements from a channel. Returns the slice and the slice length.
// @TODO: we should probaby provide an helper that reuse the same buffer.
// @TODO: we should probably provide an helper that reuse the same buffer.
func Batch[T any](ch <-chan T, size int) (collection []T, length int, readTime time.Duration, ok bool) {
buffer := make([]T, 0, size)
index := 0
Expand All @@ -212,7 +212,7 @@ func Batch[T any](ch <-chan T, size int) (collection []T, length int, readTime t
}

// BatchWithTimeout creates a slice of n elements from a channel, with timeout. Returns the slice and the slice length.
// @TODO: we should probaby provide an helper that reuse the same buffer.
// @TODO: we should probably provide an helper that reuse the same buffer.
func BatchWithTimeout[T any](ch <-chan T, size int, timeout time.Duration) (collection []T, length int, readTime time.Duration, ok bool) {
expire := time.NewTimer(timeout)
defer expire.Stop()
Expand Down
44 changes: 22 additions & 22 deletions errors.go
Expand Up @@ -66,7 +66,7 @@ func Must[T any](val T, err any, messageArgs ...interface{}) T {
return val
}

// Must0 has the same behavior than Must, but callback returns no variable.
// Must0 has the same behavior as Must, but callback returns no variable.
// Play: https://go.dev/play/p/TMoWrRp3DyC
func Must0(err any, messageArgs ...interface{}) {
must(err, messageArgs...)
Expand All @@ -78,35 +78,35 @@ func Must1[T any](val T, err any, messageArgs ...interface{}) T {
return Must(val, err, messageArgs...)
}

// Must2 has the same behavior than Must, but callback returns 2 variables.
// Must2 has the same behavior as Must, but callback returns 2 variables.
// Play: https://go.dev/play/p/TMoWrRp3DyC
func Must2[T1 any, T2 any](val1 T1, val2 T2, err any, messageArgs ...interface{}) (T1, T2) {
must(err, messageArgs...)
return val1, val2
}

// Must3 has the same behavior than Must, but callback returns 3 variables.
// Must3 has the same behavior as Must, but callback returns 3 variables.
// Play: https://go.dev/play/p/TMoWrRp3DyC
func Must3[T1 any, T2 any, T3 any](val1 T1, val2 T2, val3 T3, err any, messageArgs ...interface{}) (T1, T2, T3) {
must(err, messageArgs...)
return val1, val2, val3
}

// Must4 has the same behavior than Must, but callback returns 4 variables.
// Must4 has the same behavior as Must, but callback returns 4 variables.
// Play: https://go.dev/play/p/TMoWrRp3DyC
func Must4[T1 any, T2 any, T3 any, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err any, messageArgs ...interface{}) (T1, T2, T3, T4) {
must(err, messageArgs...)
return val1, val2, val3, val4
}

// Must5 has the same behavior than Must, but callback returns 5 variables.
// Must5 has the same behavior as Must, but callback returns 5 variables.
// Play: https://go.dev/play/p/TMoWrRp3DyC
func Must5[T1 any, T2 any, T3 any, T4 any, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, err any, messageArgs ...interface{}) (T1, T2, T3, T4, T5) {
must(err, messageArgs...)
return val1, val2, val3, val4, val5
}

// Must6 has the same behavior than Must, but callback returns 6 variables.
// Must6 has the same behavior as Must, but callback returns 6 variables.
// Play: https://go.dev/play/p/TMoWrRp3DyC
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, messageArgs ...interface{}) (T1, T2, T3, T4, T5, T6) {
must(err, messageArgs...)
Expand All @@ -131,7 +131,7 @@ func Try(callback func() error) (ok bool) {
return
}

// Try0 has the same behavior than Try, but callback returns no variable.
// Try0 has the same behavior as Try, but callback returns no variable.
// Play: https://go.dev/play/p/mTyyWUvn9u4
func Try0(callback func()) bool {
return Try(func() error {
Expand All @@ -146,7 +146,7 @@ func Try1(callback func() error) bool {
return Try(callback)
}

// Try2 has the same behavior than Try, but callback returns 2 variables.
// Try2 has the same behavior as Try, but callback returns 2 variables.
// Play: https://go.dev/play/p/mTyyWUvn9u4
func Try2[T any](callback func() (T, error)) bool {
return Try(func() error {
Expand All @@ -155,7 +155,7 @@ func Try2[T any](callback func() (T, error)) bool {
})
}

// Try3 has the same behavior than Try, but callback returns 3 variables.
// Try3 has the same behavior as Try, but callback returns 3 variables.
// Play: https://go.dev/play/p/mTyyWUvn9u4
func Try3[T, R any](callback func() (T, R, error)) bool {
return Try(func() error {
Expand All @@ -164,7 +164,7 @@ func Try3[T, R any](callback func() (T, R, error)) bool {
})
}

// Try4 has the same behavior than Try, but callback returns 4 variables.
// Try4 has the same behavior as Try, but callback returns 4 variables.
// Play: https://go.dev/play/p/mTyyWUvn9u4
func Try4[T, R, S any](callback func() (T, R, S, error)) bool {
return Try(func() error {
Expand All @@ -173,7 +173,7 @@ func Try4[T, R, S any](callback func() (T, R, S, error)) bool {
})
}

// Try5 has the same behavior than Try, but callback returns 5 variables.
// Try5 has the same behavior as Try, but callback returns 5 variables.
// Play: https://go.dev/play/p/mTyyWUvn9u4
func Try5[T, R, S, Q any](callback func() (T, R, S, Q, error)) bool {
return Try(func() error {
Expand All @@ -182,7 +182,7 @@ func Try5[T, R, S, Q any](callback func() (T, R, S, Q, error)) bool {
})
}

// Try6 has the same behavior than Try, but callback returns 6 variables.
// Try6 has the same behavior as Try, but callback returns 6 variables.
// Play: https://go.dev/play/p/mTyyWUvn9u4
func Try6[T, R, S, Q, U any](callback func() (T, R, S, Q, U, error)) bool {
return Try(func() error {
Expand All @@ -191,13 +191,13 @@ func Try6[T, R, S, Q, U any](callback func() (T, R, S, Q, U, error)) bool {
})
}

// TryOr has the same behavior than Must, but returns a default value in case of error.
// TryOr has the same behavior as Must, but returns a default value in case of error.
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
func TryOr[A any](callback func() (A, error), fallbackA A) (A, bool) {
return TryOr1(callback, fallbackA)
}

// TryOr1 has the same behavior than Must, but returns a default value in case of error.
// TryOr1 has the same behavior as Must, but returns a default value in case of error.
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
func TryOr1[A any](callback func() (A, error), fallbackA A) (A, bool) {
ok := false
Expand All @@ -213,7 +213,7 @@ func TryOr1[A any](callback func() (A, error), fallbackA A) (A, bool) {
return fallbackA, ok
}

// TryOr2 has the same behavior than Must, but returns a default value in case of error.
// TryOr2 has the same behavior as Must, but returns a default value in case of error.
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
func TryOr2[A any, B any](callback func() (A, B, error), fallbackA A, fallbackB B) (A, B, bool) {
ok := false
Expand All @@ -230,7 +230,7 @@ func TryOr2[A any, B any](callback func() (A, B, error), fallbackA A, fallbackB
return fallbackA, fallbackB, ok
}

// TryOr3 has the same behavior than Must, but returns a default value in case of error.
// TryOr3 has the same behavior as Must, but returns a default value in case of error.
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
func TryOr3[A any, B any, C any](callback func() (A, B, C, error), fallbackA A, fallbackB B, fallbackC C) (A, B, C, bool) {
ok := false
Expand All @@ -248,7 +248,7 @@ func TryOr3[A any, B any, C any](callback func() (A, B, C, error), fallbackA A,
return fallbackA, fallbackB, fallbackC, ok
}

// TryOr4 has the same behavior than Must, but returns a default value in case of error.
// TryOr4 has the same behavior as Must, but returns a default value in case of error.
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
func TryOr4[A any, B any, C any, D any](callback func() (A, B, C, D, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D) (A, B, C, D, bool) {
ok := false
Expand All @@ -267,7 +267,7 @@ func TryOr4[A any, B any, C any, D any](callback func() (A, B, C, D, error), fal
return fallbackA, fallbackB, fallbackC, fallbackD, ok
}

// TryOr5 has the same behavior than Must, but returns a default value in case of error.
// TryOr5 has the same behavior as Must, but returns a default value in case of error.
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
func TryOr5[A any, B any, C any, D any, E any](callback func() (A, B, C, D, E, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E) (A, B, C, D, E, bool) {
ok := false
Expand All @@ -287,7 +287,7 @@ func TryOr5[A any, B any, C any, D any, E any](callback func() (A, B, C, D, E, e
return fallbackA, fallbackB, fallbackC, fallbackD, fallbackE, ok
}

// TryOr6 has the same behavior than Must, but returns a default value in case of error.
// TryOr6 has the same behavior as Must, but returns a default value in case of error.
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
func TryOr6[A any, B any, C any, D any, E any, F any](callback func() (A, B, C, D, E, F, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E, fallbackF F) (A, B, C, D, E, F, bool) {
ok := false
Expand All @@ -308,7 +308,7 @@ func TryOr6[A any, B any, C any, D any, E any, F any](callback func() (A, B, C,
return fallbackA, fallbackB, fallbackC, fallbackD, fallbackE, fallbackF, ok
}

// TryWithErrorValue has the same behavior than Try, but also returns value passed to panic.
// TryWithErrorValue has the same behavior as Try, but also returns value passed to panic.
// Play: https://go.dev/play/p/Kc7afQIT2Fs
func TryWithErrorValue(callback func() error) (errorValue any, ok bool) {
ok = true
Expand All @@ -329,15 +329,15 @@ func TryWithErrorValue(callback func() error) (errorValue any, ok bool) {
return
}

// TryCatch has the same behavior than Try, but calls the catch function in case of error.
// TryCatch has the same behavior as Try, but calls the catch function in case of error.
// Play: https://go.dev/play/p/PnOON-EqBiU
func TryCatch(callback func() error, catch func()) {
if !Try(callback) {
catch()
}
}

// TryCatchWithErrorValue has the same behavior than TryWithErrorValue, but calls the catch function in case of error.
// TryCatchWithErrorValue has the same behavior as TryWithErrorValue, but calls the catch function in case of error.
// Play: https://go.dev/play/p/8Pc9gwX_GZO
func TryCatchWithErrorValue(callback func() error, catch func(any)) {
if err, ok := TryWithErrorValue(callback); !ok {
Expand Down
22 changes: 11 additions & 11 deletions find.go
Expand Up @@ -36,7 +36,7 @@ func LastIndexOf[T comparable](collection []T, element T) int {
}

// Find search an element in a slice based on a predicate. It returns element and true if element was found.
func Find[T any](collection []T, predicate func(T) bool) (T, bool) {
func Find[T any](collection []T, predicate func(item T) bool) (T, bool) {
for _, item := range collection {
if predicate(item) {
return item, true
Expand All @@ -49,7 +49,7 @@ func Find[T any](collection []T, predicate func(T) bool) (T, bool) {

// FindIndexOf searches an element in a slice based on a predicate and returns the index and true.
// It returns -1 and false if the element is not found.
func FindIndexOf[T any](collection []T, predicate func(T) bool) (T, int, bool) {
func FindIndexOf[T any](collection []T, predicate func(item T) bool) (T, int, bool) {
for i, item := range collection {
if predicate(item) {
return item, i, true
Expand All @@ -62,7 +62,7 @@ func FindIndexOf[T any](collection []T, predicate func(T) bool) (T, int, bool) {

// FindLastIndexOf searches last element in a slice based on a predicate and returns the index and true.
// It returns -1 and false if the element is not found.
func FindLastIndexOf[T any](collection []T, predicate func(T) bool) (T, int, bool) {
func FindLastIndexOf[T any](collection []T, predicate func(item T) bool) (T, int, bool) {
length := len(collection)

for i := length - 1; i >= 0; i-- {
Expand All @@ -76,7 +76,7 @@ func FindLastIndexOf[T any](collection []T, predicate func(T) bool) (T, int, boo
}

// FindOrElse search an element in a slice based on a predicate. It returns the element if found or a given fallback value otherwise.
func FindOrElse[T any](collection []T, fallback T, predicate func(T) bool) T {
func FindOrElse[T any](collection []T, fallback T, predicate func(item T) bool) T {
for _, item := range collection {
if predicate(item) {
return item
Expand All @@ -98,7 +98,7 @@ func FindKey[K comparable, V comparable](object map[K]V, value V) (K, bool) {
}

// FindKeyBy returns the key of the first element predicate returns truthy for.
func FindKeyBy[K comparable, V any](object map[K]V, predicate func(K, V) bool) (K, bool) {
func FindKeyBy[K comparable, V any](object map[K]V, predicate func(key K, value V) bool) (K, bool) {
for k, v := range object {
if predicate(k, v) {
return k, true
Expand Down Expand Up @@ -136,7 +136,7 @@ func FindUniques[T comparable](collection []T) []T {
// FindUniquesBy returns a slice with all the unique elements of the collection.
// The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is
// invoked for each element in array to generate the criterion by which uniqueness is computed.
func FindUniquesBy[T any, U comparable](collection []T, iteratee func(T) U) []T {
func FindUniquesBy[T any, U comparable](collection []T, iteratee func(item T) U) []T {
isDupl := make(map[U]bool, len(collection))

for _, item := range collection {
Expand All @@ -163,7 +163,7 @@ func FindUniquesBy[T any, U comparable](collection []T, iteratee func(T) U) []T
return result
}

// FindDuplicates returns a slice with the first occurence of each duplicated elements of the collection.
// FindDuplicates returns a slice with the first occurrence of each duplicated elements of the collection.
// The order of result values is determined by the order they occur in the collection.
func FindDuplicates[T comparable](collection []T) []T {
isDupl := make(map[T]bool, len(collection))
Expand All @@ -189,10 +189,10 @@ func FindDuplicates[T comparable](collection []T) []T {
return result
}

// FindDuplicatesBy returns a slice with the first occurence of each duplicated elements of the collection.
// FindDuplicatesBy returns a slice with the first occurrence of each duplicated elements of the collection.
// The order of result values is determined by the order they occur in the array. It accepts `iteratee` which is
// invoked for each element in array to generate the criterion by which uniqueness is computed.
func FindDuplicatesBy[T any, U comparable](collection []T, iteratee func(T) U) []T {
func FindDuplicatesBy[T any, U comparable](collection []T, iteratee func(item T) U) []T {
isDupl := make(map[U]bool, len(collection))

for _, item := range collection {
Expand Down Expand Up @@ -245,7 +245,7 @@ func Min[T constraints.Ordered](collection []T) T {
// MinBy search the minimum value of a collection using the given comparison function.
// If several values of the collection are equal to the smallest value, returns the first such value.
// Returns zero value when collection is empty.
func MinBy[T any](collection []T, comparison func(T, T) bool) T {
func MinBy[T any](collection []T, comparison func(a T, b T) bool) T {
var min T

if len(collection) == 0 {
Expand Down Expand Up @@ -290,7 +290,7 @@ func Max[T constraints.Ordered](collection []T) T {
// MaxBy search the maximum value of a collection using the given comparison function.
// If several values of the collection are equal to the greatest value, returns the first such value.
// Returns zero value when collection is empty.
func MaxBy[T any](collection []T, comparison func(T, T) bool) T {
func MaxBy[T any](collection []T, comparison func(a T, b T) bool) T {
var max T

if len(collection) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion func.go
@@ -1,7 +1,7 @@
package lo

// Partial returns new function that, when called, has its first argument set to the provided value.
func Partial[T1, T2, R any](f func(T1, T2) R, arg1 T1) func(T2) R {
func Partial[T1, T2, R any](f func(a T1, b T2) R, arg1 T1) func(T2) R {
return func(t2 T2) R {
return f(arg1, t2)
}
Expand Down

0 comments on commit 7887963

Please sign in to comment.