feat: name each callback/iteratee/predicate arguments in order to improve autocompletion

closes #242
This commit is contained in:
Samuel Berthe
2022-10-13 22:25:33 +02:00
parent a2c520241f
commit 7887963db8
12 changed files with 96 additions and 84 deletions

View File

@@ -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)
@@ -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.
@@ -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})
@@ -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 {
@@ -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)

View File

@@ -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{}
@@ -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))
@@ -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
@@ -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()

View File

@@ -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...)
@@ -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...)
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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 {
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -329,7 +329,7 @@ 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) {
@@ -337,7 +337,7 @@ func TryCatch(callback func() error, catch func()) {
}
}
// 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 {

22
find.go
View File

@@ -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
@@ -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
@@ -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-- {
@@ -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
@@ -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
@@ -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 {
@@ -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))
@@ -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 {
@@ -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 {
@@ -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 {

View File

@@ -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)
}

View File

@@ -12,7 +12,7 @@ func Contains[T comparable](collection []T, element T) bool {
}
// ContainsBy returns true if predicate function return true.
func ContainsBy[T any](collection []T, predicate func(T) bool) bool {
func ContainsBy[T any](collection []T, predicate func(item T) bool) bool {
for _, item := range collection {
if predicate(item) {
return true
@@ -34,7 +34,7 @@ func Every[T comparable](collection []T, subset []T) bool {
}
// EveryBy returns true if the predicate returns true for all of the elements in the collection or if the collection is empty.
func EveryBy[V any](collection []V, predicate func(V) bool) bool {
func EveryBy[T any](collection []T, predicate func(item T) bool) bool {
for _, v := range collection {
if !predicate(v) {
return false
@@ -58,7 +58,7 @@ func Some[T comparable](collection []T, subset []T) bool {
// SomeBy returns true if the predicate returns true for any of the elements in the collection.
// If the collection is empty SomeBy returns false.
func SomeBy[V any](collection []V, predicate func(V) bool) bool {
func SomeBy[T any](collection []T, predicate func(item T) bool) bool {
for _, v := range collection {
if predicate(v) {
return true
@@ -69,7 +69,7 @@ func SomeBy[V any](collection []V, predicate func(V) bool) bool {
}
// None returns true if no element of a subset are contained into a collection or if the subset is empty.
func None[V comparable](collection []V, subset []V) bool {
func None[T comparable](collection []T, subset []T) bool {
for _, elem := range subset {
if Contains(collection, elem) {
return false
@@ -80,7 +80,7 @@ func None[V comparable](collection []V, subset []V) bool {
}
// NoneBy returns true if the predicate returns true for none of the elements in the collection or if the collection is empty.
func NoneBy[V any](collection []V, predicate func(V) bool) bool {
func NoneBy[T any](collection []T, predicate func(item T) bool) bool {
for _, v := range collection {
if predicate(v) {
return false

12
map.go
View File

@@ -26,7 +26,7 @@ func Values[K comparable, V any](in map[K]V) []V {
// PickBy returns same map type filtered by given predicate.
// Play: https://go.dev/play/p/kdg8GR_QMmf
func PickBy[K comparable, V any](in map[K]V, predicate func(K, V) bool) map[K]V {
func PickBy[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) map[K]V {
r := map[K]V{}
for k, v := range in {
if predicate(k, v) {
@@ -62,7 +62,7 @@ func PickByValues[K comparable, V comparable](in map[K]V, values []V) map[K]V {
// OmitBy returns same map type filtered by given predicate.
// Play: https://go.dev/play/p/EtBsR43bdsd
func OmitBy[K comparable, V any](in map[K]V, predicate func(K, V) bool) map[K]V {
func OmitBy[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) map[K]V {
r := map[K]V{}
for k, v := range in {
if !predicate(k, v) {
@@ -167,7 +167,7 @@ func Assign[K comparable, V any](maps ...map[K]V) map[K]V {
// MapKeys manipulates a map keys and transforms it to a map of another type.
// Play: https://go.dev/play/p/9_4WPIqOetJ
func MapKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(V, K) R) map[R]V {
func MapKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(value V, key K) R) map[R]V {
result := map[R]V{}
for k, v := range in {
@@ -179,7 +179,7 @@ func MapKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(V, K)
// MapValues manipulates a map values and transforms it to a map of another type.
// Play: https://go.dev/play/p/T_8xAfvcf0W
func MapValues[K comparable, V any, R any](in map[K]V, iteratee func(V, K) R) map[K]R {
func MapValues[K comparable, V any, R any](in map[K]V, iteratee func(value V, key K) R) map[K]R {
result := map[K]R{}
for k, v := range in {
@@ -191,7 +191,7 @@ func MapValues[K comparable, V any, R any](in map[K]V, iteratee func(V, K) R) ma
// MapEntries manipulates a map entries and transforms it to a map of another type.
// Play: https://go.dev/play/p/VuvNQzxKimT
func MapEntries[K1 comparable, V1 any, K2 comparable, V2 any](in map[K1]V1, iteratee func(K1, V1) (K2, V2)) map[K2]V2 {
func MapEntries[K1 comparable, V1 any, K2 comparable, V2 any](in map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2 {
result := make(map[K2]V2, len(in))
for k1, v1 := range in {
@@ -204,7 +204,7 @@ func MapEntries[K1 comparable, V1 any, K2 comparable, V2 any](in map[K1]V1, iter
// MapToSlice transforms a map into a slice based on specific iteratee
// Play: https://go.dev/play/p/ZuiCZpDt6LD
func MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(K, V) R) []R {
func MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) R) []R {
result := make([]R, 0, len(in))
for k, v := range in {

View File

@@ -75,7 +75,7 @@ func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collec
// SumBy summarizes the values in a collection using the given return value from the iteration function. If collection is empty 0 is returned.
// Play: https://go.dev/play/p/Dz_a_7jN_ca
func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(T) R) R {
func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R {
var sum R = 0
for _, item := range collection {
sum = sum + iteratee(item)

View File

@@ -4,7 +4,7 @@ import "sync"
// Map manipulates a slice and transforms it to a slice of another type.
// `iteratee` is call in parallel. Result keep the same order.
func Map[T any, R any](collection []T, iteratee func(T, int) R) []R {
func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R {
result := make([]R, len(collection))
var wg sync.WaitGroup
@@ -27,7 +27,7 @@ func Map[T any, R any](collection []T, iteratee func(T, int) R) []R {
// ForEach iterates over elements of collection and invokes iteratee for each element.
// `iteratee` is call in parallel.
func ForEach[T any](collection []T, iteratee func(T, int)) {
func ForEach[T any](collection []T, iteratee func(item T, index int)) {
var wg sync.WaitGroup
wg.Add(len(collection))
@@ -44,7 +44,7 @@ func ForEach[T any](collection []T, iteratee func(T, int)) {
// Times invokes the iteratee n times, returning an array of the results of each invocation.
// The iteratee is invoked with index as argument.
// `iteratee` is call in parallel.
func Times[T any](count int, iteratee func(int) T) []T {
func Times[T any](count int, iteratee func(index int) T) []T {
result := make([]T, count)
var wg sync.WaitGroup
@@ -67,7 +67,7 @@ func Times[T any](count int, iteratee func(int) T) []T {
// GroupBy returns an object composed of keys generated from the results of running each element of collection through iteratee.
// `iteratee` is call in parallel.
func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T {
func GroupBy[T any, U comparable](collection []T, iteratee func(item T) U) map[U][]T {
result := map[U][]T{}
var mu sync.Mutex
@@ -96,7 +96,7 @@ func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T
// determined by the order they occur in collection. The grouping is generated from the results
// of running each element of collection through iteratee.
// `iteratee` is call in parallel.
func PartitionBy[T any, K comparable](collection []T, iteratee func(x T) K) [][]T {
func PartitionBy[T any, K comparable](collection []T, iteratee func(item T) K) [][]T {
result := [][]T{}
seen := map[K]int{}

View File

@@ -63,7 +63,7 @@ func NewDebounce(duration time.Duration, f ...func()) (func(), func()) {
// 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.
// Play: https://go.dev/play/p/3ggJZ2ZKcMj
func Attempt(maxIteration int, f func(int) error) (int, error) {
func Attempt(maxIteration int, f func(index int) error) (int, error) {
var err error
for i := 0; maxIteration <= 0 || i < maxIteration; i++ {
@@ -82,7 +82,7 @@ func Attempt(maxIteration int, f func(int) error) (int, error) {
// When 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(int, time.Duration) error) (int, time.Duration, error) {
func AttemptWithDelay(maxIteration int, delay time.Duration, f func(index int, duration time.Duration) error) (int, time.Duration, error) {
var err error
start := time.Now()

View File

@@ -8,7 +8,7 @@ import (
// Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.
// Play: https://go.dev/play/p/Apjg3WeSi7K
func Filter[V any](collection []V, predicate func(V, int) bool) []V {
func Filter[V any](collection []V, predicate func(item V, index int) bool) []V {
result := []V{}
for i, item := range collection {
@@ -22,7 +22,7 @@ func Filter[V any](collection []V, predicate func(V, int) bool) []V {
// Map manipulates a slice and transforms it to a slice of another type.
// Play: https://go.dev/play/p/OkPcYAhBo0D
func Map[T any, R any](collection []T, iteratee func(T, int) R) []R {
func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R {
result := make([]R, len(collection))
for i, item := range collection {
@@ -38,7 +38,7 @@ func Map[T any, R any](collection []T, iteratee func(T, int) R) []R {
// - whether the result element should be included or not.
//
// Play: https://go.dev/play/p/-AuYXfy7opz
func FilterMap[T any, R any](collection []T, callback func(T, int) (R, bool)) []R {
func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R, bool)) []R {
result := []R{}
for i, item := range collection {
@@ -52,7 +52,7 @@ func FilterMap[T any, R any](collection []T, callback func(T, int) (R, bool)) []
// FlatMap manipulates a slice and transforms and flattens it to a slice of another type.
// Play: https://go.dev/play/p/YSoYmQTA8-U
func FlatMap[T any, R any](collection []T, iteratee func(T, int) []R) []R {
func FlatMap[T any, R any](collection []T, iteratee func(item T, index int) []R) []R {
result := []R{}
for i, item := range collection {
@@ -65,7 +65,7 @@ func FlatMap[T any, R any](collection []T, iteratee func(T, int) []R) []R {
// Reduce reduces collection to a value which is the accumulated result of running each element in collection
// through accumulator, where each successive invocation is supplied the return value of the previous.
// Play: https://go.dev/play/p/R4UHXZNaaUG
func Reduce[T any, R any](collection []T, accumulator func(R, T, int) R, initial R) R {
func Reduce[T any, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R {
for i, item := range collection {
initial = accumulator(initial, item, i)
}
@@ -75,7 +75,7 @@ func Reduce[T any, R any](collection []T, accumulator func(R, T, int) R, initial
// ReduceRight helper is like Reduce except that it iterates over elements of collection from right to left.
// Play: https://go.dev/play/p/Fq3W70l7wXF
func ReduceRight[T any, R any](collection []T, accumulator func(R, T, int) R, initial R) R {
func ReduceRight[T any, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R {
for i := len(collection) - 1; i >= 0; i-- {
initial = accumulator(initial, collection[i], i)
}
@@ -85,7 +85,7 @@ func ReduceRight[T any, R any](collection []T, accumulator func(R, T, int) R, in
// ForEach iterates over elements of collection and invokes iteratee for each element.
// Play: https://go.dev/play/p/oofyiUPRf8t
func ForEach[T any](collection []T, iteratee func(T, int)) {
func ForEach[T any](collection []T, iteratee func(item T, index int)) {
for i, item := range collection {
iteratee(item, i)
}
@@ -94,7 +94,7 @@ func ForEach[T any](collection []T, iteratee func(T, int)) {
// Times invokes the iteratee n times, returning an array of the results of each invocation.
// The iteratee is invoked with index as argument.
// Play: https://go.dev/play/p/vgQj3Glr6lT
func Times[T any](count int, iteratee func(int) T) []T {
func Times[T any](count int, iteratee func(index int) T) []T {
result := make([]T, count)
for i := 0; i < count; i++ {
@@ -127,7 +127,7 @@ func Uniq[T comparable](collection []T) []T {
// 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.
// Play: https://go.dev/play/p/g42Z3QSb53u
func UniqBy[T any, U comparable](collection []T, iteratee func(T) U) []T {
func UniqBy[T any, U comparable](collection []T, iteratee func(item T) U) []T {
result := make([]T, 0, len(collection))
seen := make(map[U]struct{}, len(collection))
@@ -147,7 +147,7 @@ func UniqBy[T any, U comparable](collection []T, iteratee func(T) U) []T {
// GroupBy returns an object composed of keys generated from the results of running each element of collection through iteratee.
// Play: https://go.dev/play/p/XnQBd_v6brd
func GroupBy[T any, U comparable](collection []T, iteratee func(T) U) map[U][]T {
func GroupBy[T any, U comparable](collection []T, iteratee func(item T) U) map[U][]T {
result := map[U][]T{}
for _, item := range collection {
@@ -189,7 +189,7 @@ func Chunk[T any](collection []T, size int) [][]T {
// determined by the order they occur in collection. The grouping is generated from the results
// of running each element of collection through iteratee.
// Play: https://go.dev/play/p/NfQ_nGjkgXW
func PartitionBy[T any, K comparable](collection []T, iteratee func(x T) K) [][]T {
func PartitionBy[T any, K comparable](collection []T, iteratee func(item T) K) [][]T {
result := [][]T{}
seen := map[K]int{}
@@ -317,7 +317,7 @@ func Repeat[T Clonable[T]](count int, initial T) []T {
// RepeatBy builds a slice with values returned by N calls of callback.
// Play: https://go.dev/play/p/ozZLCtX_hNU
func RepeatBy[T any](count int, predicate func(int) T) []T {
func RepeatBy[T any](count int, predicate func(index int) T) []T {
result := make([]T, 0, count)
for i := 0; i < count; i++ {
@@ -329,7 +329,7 @@ func RepeatBy[T any](count int, predicate func(int) T) []T {
// KeyBy transforms a slice or an array of structs to a map based on a pivot callback.
// Play: https://go.dev/play/p/mdaClUAT-zZ
func KeyBy[K comparable, V any](collection []V, iteratee func(V) K) map[K]V {
func KeyBy[K comparable, V any](collection []V, iteratee func(item V) K) map[K]V {
result := make(map[K]V, len(collection))
for _, v := range collection {
@@ -344,7 +344,7 @@ func KeyBy[K comparable, V any](collection []V, iteratee func(V) K) map[K]V {
// If any of two pairs would have the same key the last one gets added to the map.
// The order of keys in returned map is not specified and is not guaranteed to be the same from the original array.
// Play: https://go.dev/play/p/WHa2CfMO3Lr
func Associate[T any, K comparable, V any](collection []T, transform func(T) (K, V)) map[K]V {
func Associate[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V {
result := make(map[K]V)
for _, t := range collection {
@@ -360,7 +360,7 @@ func Associate[T any, K comparable, V any](collection []T, transform func(T) (K,
// The order of keys in returned map is not specified and is not guaranteed to be the same from the original array.
// Alias of Associate().
// Play: https://go.dev/play/p/WHa2CfMO3Lr
func SliceToMap[T any, K comparable, V any](collection []T, transform func(T) (K, V)) map[K]V {
func SliceToMap[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V {
return Associate(collection, transform)
}
@@ -389,7 +389,7 @@ func DropRight[T any](collection []T, n int) []T {
// DropWhile drops elements from the beginning of a slice or array while the predicate returns true.
// Play: https://go.dev/play/p/7gBPYw2IK16
func DropWhile[T any](collection []T, predicate func(T) bool) []T {
func DropWhile[T any](collection []T, predicate func(item T) bool) []T {
i := 0
for ; i < len(collection); i++ {
if !predicate(collection[i]) {
@@ -403,7 +403,7 @@ func DropWhile[T any](collection []T, predicate func(T) bool) []T {
// DropRightWhile drops elements from the end of a slice or array while the predicate returns true.
// Play: https://go.dev/play/p/3-n71oEC0Hz
func DropRightWhile[T any](collection []T, predicate func(T) bool) []T {
func DropRightWhile[T any](collection []T, predicate func(item T) bool) []T {
i := len(collection) - 1
for ; i >= 0; i-- {
if !predicate(collection[i]) {
@@ -417,7 +417,7 @@ func DropRightWhile[T any](collection []T, predicate func(T) bool) []T {
// Reject is the opposite of Filter, this method returns the elements of collection that predicate does not return truthy for.
// Play: https://go.dev/play/p/YkLMODy1WEL
func Reject[V any](collection []V, predicate func(V, int) bool) []V {
func Reject[V any](collection []V, predicate func(item V, index int) bool) []V {
result := []V{}
for i, item := range collection {
@@ -443,7 +443,7 @@ func Count[T comparable](collection []T, value T) (count int) {
// CountBy counts the number of elements in the collection for which predicate is true.
// Play: https://go.dev/play/p/ByQbNYQQi4X
func CountBy[T any](collection []T, predicate func(T) bool) (count int) {
func CountBy[T any](collection []T, predicate func(item T) bool) (count int) {
for _, item := range collection {
if predicate(item) {
count++
@@ -465,10 +465,10 @@ func CountValues[T comparable](collection []T) map[T]int {
return result
}
// CountValues counts the number of each element return from mapper function.
// CountValuesBy counts the number of each element return from mapper function.
// Is equivalent to chaining lo.Map and lo.CountValues.
// Play: https://go.dev/play/p/2U0dG1SnOmS
func CountValuesBy[T any, U comparable](collection []T, mapper func(T) U) map[U]int {
func CountValuesBy[T any, U comparable](collection []T, mapper func(item T) U) map[U]int {
result := make(map[U]int)
for _, item := range collection {
@@ -573,7 +573,7 @@ func IsSorted[T constraints.Ordered](collection []T) bool {
// IsSortedByKey checks if a slice is sorted by iteratee.
// Play: https://go.dev/play/p/wiG6XyBBu49
func IsSortedByKey[T any, K constraints.Ordered](collection []T, iteratee func(T) K) bool {
func IsSortedByKey[T any, K constraints.Ordered](collection []T, iteratee func(item T) K) bool {
size := len(collection)
for i := 0; i < size-1; i++ {

View File

@@ -42,7 +42,7 @@ func T8[A any, B any, C any, D any, E any, F any, G any, H any](a A, b B, c C, d
return Tuple8[A, B, C, D, E, F, G, H]{A: a, B: b, C: c, D: d, E: e, F: f, G: g, H: h}
}
// T8 creates a tuple from a list of values.
// T9 creates a tuple from a list of values.
// Play: https://go.dev/play/p/IllL3ZO4BQm
func T9[A any, B any, C any, D any, E any, F any, G any, H any, I any](a A, b B, c C, d D, e E, f F, g G, h H, i I) Tuple9[A, B, C, D, E, F, G, H, I] {
return Tuple9[A, B, C, D, E, F, G, H, I]{A: a, B: b, C: c, D: d, E: e, F: f, G: g, H: h, I: i}