From f80918dcef9839f55054d96fe3fb2abc05ed11a7 Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Fri, 28 Jun 2024 01:35:26 +0200 Subject: [PATCH] feat: speed up loops by reducing allocations --- channel.go | 12 ++-- find.go | 80 ++++++++++++------------- intersect.go | 84 +++++++++++++------------- map.go | 76 ++++++++++++------------ math.go | 8 +-- retry.go | 8 +-- slice.go | 106 ++++++++++++++++----------------- string.go | 12 ++-- tuples.go | 136 +++++++++++++++++++++---------------------- type_manipulation.go | 16 ++--- 10 files changed, 269 insertions(+), 269 deletions(-) diff --git a/channel.go b/channel.go index 5079ded7..2ffdb381 100644 --- a/channel.go +++ b/channel.go @@ -160,8 +160,8 @@ func SliceToChannel[T any](bufferSize int, collection []T) <-chan T { ch := make(chan T, bufferSize) go func() { - for _, item := range collection { - ch <- item + for i := range collection { + ch <- collection[i] } close(ch) @@ -265,13 +265,13 @@ func FanIn[T any](channelBufferCap int, upstreams ...<-chan T) <-chan T { // Start an output goroutine for each input channel in upstreams. wg.Add(len(upstreams)) - for _, c := range upstreams { - go func(c <-chan T) { - for n := range c { + for i := range upstreams { + go func(index int) { + for n := range upstreams[index] { out <- n } wg.Done() - }(c) + }(i) } // Start a goroutine to close out once all the output goroutines are done. diff --git a/find.go b/find.go index 3cbefad2..93329a0b 100644 --- a/find.go +++ b/find.go @@ -13,8 +13,8 @@ import ( // IndexOf returns the index at which the first occurrence of a value is found in an array or return -1 // if the value cannot be found. func IndexOf[T comparable](collection []T, element T) int { - for i, item := range collection { - if item == element { + for i := range collection { + if collection[i] == element { return i } } @@ -38,9 +38,9 @@ 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(item T) bool) (T, bool) { - for _, item := range collection { - if predicate(item) { - return item, true + for i := range collection { + if predicate(collection[i]) { + return collection[i], true } } @@ -51,9 +51,9 @@ func Find[T any](collection []T, predicate func(item 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(item T) bool) (T, int, bool) { - for i, item := range collection { - if predicate(item) { - return item, i, true + for i := range collection { + if predicate(collection[i]) { + return collection[i], i, true } } @@ -78,9 +78,9 @@ func FindLastIndexOf[T any](collection []T, predicate func(item T) bool) (T, int // 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(item T) bool) T { - for _, item := range collection { - if predicate(item) { - return item + for i := range collection { + if predicate(collection[i]) { + return collection[i] } } @@ -89,8 +89,8 @@ func FindOrElse[T any](collection []T, fallback T, predicate func(item T) bool) // FindKey returns the key of the first value matching. func FindKey[K comparable, V comparable](object map[K]V, value V) (K, bool) { - for k, v := range object { - if v == value { + for k := range object { + if object[k] == value { return k, true } } @@ -100,8 +100,8 @@ 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(key K, value V) bool) (K, bool) { - for k, v := range object { - if predicate(k, v) { + for k := range object { + if predicate(k, object[k]) { return k, true } } @@ -114,20 +114,20 @@ func FindKeyBy[K comparable, V any](object map[K]V, predicate func(key K, value func FindUniques[T comparable](collection []T) []T { isDupl := make(map[T]bool, len(collection)) - for _, item := range collection { - duplicated, ok := isDupl[item] + for i := range collection { + duplicated, ok := isDupl[collection[i]] if !ok { - isDupl[item] = false + isDupl[collection[i]] = false } else if !duplicated { - isDupl[item] = true + isDupl[collection[i]] = true } } result := make([]T, 0, len(collection)-len(isDupl)) - for _, item := range collection { - if duplicated := isDupl[item]; !duplicated { - result = append(result, item) + for i := range collection { + if duplicated := isDupl[collection[i]]; !duplicated { + result = append(result, collection[i]) } } @@ -140,8 +140,8 @@ func FindUniques[T comparable](collection []T) []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 { - key := iteratee(item) + for i := range collection { + key := iteratee(collection[i]) duplicated, ok := isDupl[key] if !ok { @@ -153,11 +153,11 @@ func FindUniquesBy[T any, U comparable](collection []T, iteratee func(item T) U) result := make([]T, 0, len(collection)-len(isDupl)) - for _, item := range collection { - key := iteratee(item) + for i := range collection { + key := iteratee(collection[i]) if duplicated := isDupl[key]; !duplicated { - result = append(result, item) + result = append(result, collection[i]) } } @@ -169,21 +169,21 @@ func FindUniquesBy[T any, U comparable](collection []T, iteratee func(item T) U) func FindDuplicates[T comparable](collection []T) []T { isDupl := make(map[T]bool, len(collection)) - for _, item := range collection { - duplicated, ok := isDupl[item] + for i := range collection { + duplicated, ok := isDupl[collection[i]] if !ok { - isDupl[item] = false + isDupl[collection[i]] = false } else if !duplicated { - isDupl[item] = true + isDupl[collection[i]] = true } } result := make([]T, 0, len(collection)-len(isDupl)) - for _, item := range collection { - if duplicated := isDupl[item]; duplicated { - result = append(result, item) - isDupl[item] = false + for i := range collection { + if duplicated := isDupl[collection[i]]; duplicated { + result = append(result, collection[i]) + isDupl[collection[i]] = false } } @@ -196,8 +196,8 @@ func FindDuplicates[T comparable](collection []T) []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 { - key := iteratee(item) + for i := range collection { + key := iteratee(collection[i]) duplicated, ok := isDupl[key] if !ok { @@ -209,11 +209,11 @@ func FindDuplicatesBy[T any, U comparable](collection []T, iteratee func(item T) result := make([]T, 0, len(collection)-len(isDupl)) - for _, item := range collection { - key := iteratee(item) + for i := range collection { + key := iteratee(collection[i]) if duplicated := isDupl[key]; duplicated { - result = append(result, item) + result = append(result, collection[i]) isDupl[key] = false } } diff --git a/intersect.go b/intersect.go index cf6cab3d..4e239d85 100644 --- a/intersect.go +++ b/intersect.go @@ -2,8 +2,8 @@ package lo // Contains returns true if an element is present in a collection. func Contains[T comparable](collection []T, element T) bool { - for _, item := range collection { - if item == element { + for i := range collection { + if collection[i] == element { return true } } @@ -13,8 +13,8 @@ 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(item T) bool) bool { - for _, item := range collection { - if predicate(item) { + for i := range collection { + if predicate(collection[i]) { return true } } @@ -24,8 +24,8 @@ func ContainsBy[T any](collection []T, predicate func(item T) bool) bool { // Every returns true if all elements of a subset are contained into a collection or if the subset is empty. func Every[T comparable](collection []T, subset []T) bool { - for _, elem := range subset { - if !Contains(collection, elem) { + for i := range subset { + if !Contains(collection, subset[i]) { return false } } @@ -35,8 +35,8 @@ 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[T any](collection []T, predicate func(item T) bool) bool { - for _, v := range collection { - if !predicate(v) { + for i := range collection { + if !predicate(collection[i]) { return false } } @@ -47,8 +47,8 @@ func EveryBy[T any](collection []T, predicate func(item T) bool) bool { // Some returns true if at least 1 element of a subset is contained into a collection. // If the subset is empty Some returns false. func Some[T comparable](collection []T, subset []T) bool { - for _, elem := range subset { - if Contains(collection, elem) { + for i := range subset { + if Contains(collection, subset[i]) { return true } } @@ -59,8 +59,8 @@ 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[T any](collection []T, predicate func(item T) bool) bool { - for _, v := range collection { - if predicate(v) { + for i := range collection { + if predicate(collection[i]) { return true } } @@ -70,8 +70,8 @@ func SomeBy[T any](collection []T, predicate func(item T) bool) bool { // None returns true if no element of a subset are contained into a collection or if the subset is empty. func None[T comparable](collection []T, subset []T) bool { - for _, elem := range subset { - if Contains(collection, elem) { + for i := range subset { + if Contains(collection, subset[i]) { return false } } @@ -81,8 +81,8 @@ func None[T comparable](collection []T, subset []T) 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[T any](collection []T, predicate func(item T) bool) bool { - for _, v := range collection { - if predicate(v) { + for i := range collection { + if predicate(collection[i]) { return false } } @@ -95,13 +95,13 @@ func Intersect[T comparable](list1 []T, list2 []T) []T { result := []T{} seen := map[T]struct{}{} - for _, elem := range list1 { - seen[elem] = struct{}{} + for i := range list1 { + seen[list1[i]] = struct{}{} } - for _, elem := range list2 { - if _, ok := seen[elem]; ok { - result = append(result, elem) + for i := range list2 { + if _, ok := seen[list2[i]]; ok { + result = append(result, list2[i]) } } @@ -118,23 +118,23 @@ func Difference[T comparable](list1 []T, list2 []T) ([]T, []T) { seenLeft := map[T]struct{}{} seenRight := map[T]struct{}{} - for _, elem := range list1 { - seenLeft[elem] = struct{}{} + for i := range list1 { + seenLeft[list1[i]] = struct{}{} } - for _, elem := range list2 { - seenRight[elem] = struct{}{} + for i := range list2 { + seenRight[list2[i]] = struct{}{} } - for _, elem := range list1 { - if _, ok := seenRight[elem]; !ok { - left = append(left, elem) + for i := range list1 { + if _, ok := seenRight[list1[i]]; !ok { + left = append(left, list1[i]) } } - for _, elem := range list2 { - if _, ok := seenLeft[elem]; !ok { - right = append(right, elem) + for i := range list2 { + if _, ok := seenLeft[list2[i]]; !ok { + right = append(right, list2[i]) } } @@ -147,11 +147,11 @@ func Union[T comparable](lists ...[]T) []T { result := []T{} seen := map[T]struct{}{} - for _, list := range lists { - for _, e := range list { - if _, ok := seen[e]; !ok { - seen[e] = struct{}{} - result = append(result, e) + for i := range lists { + for j := range lists[i] { + if _, ok := seen[lists[i][j]]; !ok { + seen[lists[i][j]] = struct{}{} + result = append(result, lists[i][j]) } } } @@ -162,9 +162,9 @@ func Union[T comparable](lists ...[]T) []T { // Without returns slice excluding all given values. func Without[T comparable](collection []T, exclude ...T) []T { result := make([]T, 0, len(collection)) - for _, e := range collection { - if !Contains(exclude, e) { - result = append(result, e) + for i := range collection { + if !Contains(exclude, collection[i]) { + result = append(result, collection[i]) } } return result @@ -175,9 +175,9 @@ func WithoutEmpty[T comparable](collection []T) []T { var empty T result := make([]T, 0, len(collection)) - for _, e := range collection { - if e != empty { - result = append(result, e) + for i := range collection { + if collection[i] != empty { + result = append(result, collection[i]) } } diff --git a/map.go b/map.go index f1ac54b9..b121daa3 100644 --- a/map.go +++ b/map.go @@ -17,8 +17,8 @@ func Keys[K comparable, V any](in map[K]V) []K { func Values[K comparable, V any](in map[K]V) []V { result := make([]V, 0, len(in)) - for _, v := range in { - result = append(result, v) + for k := range in { + result = append(result, in[k]) } return result @@ -37,9 +37,9 @@ func ValueOr[K comparable, V any](in map[K]V, key K, fallback V) V { // Play: https://go.dev/play/p/kdg8GR_QMmf 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) { - r[k] = v + for k := range in { + if predicate(k, in[k]) { + r[k] = in[k] } } return r @@ -49,9 +49,9 @@ func PickBy[K comparable, V any](in map[K]V, predicate func(key K, value V) bool // Play: https://go.dev/play/p/R1imbuci9qU func PickByKeys[K comparable, V any](in map[K]V, keys []K) map[K]V { r := map[K]V{} - for _, k := range keys { - if v, ok := in[k]; ok { - r[k] = v + for i := range keys { + if v, ok := in[keys[i]]; ok { + r[keys[i]] = v } } return r @@ -61,9 +61,9 @@ func PickByKeys[K comparable, V any](in map[K]V, keys []K) map[K]V { // Play: https://go.dev/play/p/1zdzSvbfsJc func PickByValues[K comparable, V comparable](in map[K]V, values []V) map[K]V { r := map[K]V{} - for k, v := range in { - if Contains(values, v) { - r[k] = v + for k := range in { + if Contains(values, in[k]) { + r[k] = in[k] } } return r @@ -73,9 +73,9 @@ func PickByValues[K comparable, V comparable](in map[K]V, values []V) map[K]V { // Play: https://go.dev/play/p/EtBsR43bdsd 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) { - r[k] = v + for k := range in { + if !predicate(k, in[k]) { + r[k] = in[k] } } return r @@ -85,11 +85,11 @@ func OmitBy[K comparable, V any](in map[K]V, predicate func(key K, value V) bool // Play: https://go.dev/play/p/t1QjCrs-ysk func OmitByKeys[K comparable, V any](in map[K]V, keys []K) map[K]V { r := map[K]V{} - for k, v := range in { - r[k] = v + for k := range in { + r[k] = in[k] } - for _, k := range keys { - delete(r, k) + for i := range keys { + delete(r, keys[i]) } return r } @@ -98,9 +98,9 @@ func OmitByKeys[K comparable, V any](in map[K]V, keys []K) map[K]V { // Play: https://go.dev/play/p/9UYZi-hrs8j func OmitByValues[K comparable, V comparable](in map[K]V, values []V) map[K]V { r := map[K]V{} - for k, v := range in { - if !Contains(values, v) { - r[k] = v + for k := range in { + if !Contains(values, in[k]) { + r[k] = in[k] } } return r @@ -111,10 +111,10 @@ func OmitByValues[K comparable, V comparable](in map[K]V, values []V) map[K]V { func Entries[K comparable, V any](in map[K]V) []Entry[K, V] { entries := make([]Entry[K, V], 0, len(in)) - for k, v := range in { + for k := range in { entries = append(entries, Entry[K, V]{ Key: k, - Value: v, + Value: in[k], }) } @@ -133,8 +133,8 @@ func ToPairs[K comparable, V any](in map[K]V) []Entry[K, V] { func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V { out := make(map[K]V, len(entries)) - for _, v := range entries { - out[v.Key] = v.Value + for i := range entries { + out[entries[i].Key] = entries[i].Value } return out @@ -154,8 +154,8 @@ func FromPairs[K comparable, V any](entries []Entry[K, V]) map[K]V { func Invert[K comparable, V comparable](in map[K]V) map[V]K { out := make(map[V]K, len(in)) - for k, v := range in { - out[v] = k + for k := range in { + out[in[k]] = k } return out @@ -166,9 +166,9 @@ func Invert[K comparable, V comparable](in map[K]V) map[V]K { func Assign[K comparable, V any](maps ...map[K]V) map[K]V { out := map[K]V{} - for _, m := range maps { - for k, v := range m { - out[k] = v + for i := range maps { + for k := range maps[i] { + out[k] = maps[i][k] } } @@ -180,8 +180,8 @@ func Assign[K comparable, V any](maps ...map[K]V) map[K]V { func MapKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(value V, key K) R) map[R]V { result := make(map[R]V, len(in)) - for k, v := range in { - result[iteratee(v, k)] = v + for k := range in { + result[iteratee(in[k], k)] = in[k] } return result @@ -192,8 +192,8 @@ func MapKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(value func MapValues[K comparable, V any, R any](in map[K]V, iteratee func(value V, key K) R) map[K]R { result := make(map[K]R, len(in)) - for k, v := range in { - result[k] = iteratee(v, k) + for k := range in { + result[k] = iteratee(in[k], k) } return result @@ -204,8 +204,8 @@ func MapValues[K comparable, V any, R any](in map[K]V, iteratee func(value V, ke 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 { - k2, v2 := iteratee(k1, v1) + for k1 := range in { + k2, v2 := iteratee(k1, in[k1]) result[k2] = v2 } @@ -217,8 +217,8 @@ func MapEntries[K1 comparable, V1 any, K2 comparable, V2 any](in map[K1]V1, iter 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 { - result = append(result, iteratee(k, v)) + for k := range in { + result = append(result, iteratee(k, in[k])) } return result diff --git a/math.go b/math.go index f5793d49..3ed839f6 100644 --- a/math.go +++ b/math.go @@ -67,8 +67,8 @@ func Clamp[T constraints.Ordered](value T, min T, max T) T { // Play: https://go.dev/play/p/upfeJVqs4Bt func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T { var sum T = 0 - for _, val := range collection { - sum += val + for i := range collection { + sum += collection[i] } return sum } @@ -77,8 +77,8 @@ func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collec // 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(item T) R) R { var sum R = 0 - for _, item := range collection { - sum = sum + iteratee(item) + for i := range collection { + sum = sum + iteratee(collection[i]) } return sum } diff --git a/retry.go b/retry.go index 11f456d2..f026aa33 100644 --- a/retry.go +++ b/retry.go @@ -26,8 +26,8 @@ func (d *debounce) reset() { } d.timer = time.AfterFunc(d.after, func() { - for _, f := range d.callbacks { - f() + for i := range d.callbacks { + d.callbacks[i]() } }) } @@ -101,8 +101,8 @@ func (d *debounceBy[T]) reset(key T) { item.count = 0 item.mu.Unlock() - for _, f := range d.callbacks { - f(key, count) + for i := range d.callbacks { + d.callbacks[i](key, count) } }) diff --git a/slice.go b/slice.go index ce5189e0..7ef820ad 100644 --- a/slice.go +++ b/slice.go @@ -11,9 +11,9 @@ import ( func Filter[V any](collection []V, predicate func(item V, index int) bool) []V { result := make([]V, 0, len(collection)) - for i, item := range collection { - if predicate(item, i) { - result = append(result, item) + for i := range collection { + if predicate(collection[i], i) { + result = append(result, collection[i]) } } @@ -25,8 +25,8 @@ func Filter[V any](collection []V, predicate func(item V, index int) bool) []V { 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 { - result[i] = iteratee(item, i) + for i := range collection { + result[i] = iteratee(collection[i], i) } return result @@ -41,8 +41,8 @@ func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []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 { - if r, ok := callback(item, i); ok { + for i := range collection { + if r, ok := callback(collection[i], i); ok { result = append(result, r) } } @@ -57,8 +57,8 @@ func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R func FlatMap[T any, R any](collection []T, iteratee func(item T, index int) []R) []R { result := make([]R, 0, len(collection)) - for i, item := range collection { - result = append(result, iteratee(item, i)...) + for i := range collection { + result = append(result, iteratee(collection[i], i)...) } return result @@ -68,8 +68,8 @@ func FlatMap[T any, R any](collection []T, iteratee func(item T, index int) []R) // 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(agg R, item T, index int) R, initial R) R { - for i, item := range collection { - initial = accumulator(initial, item, i) + for i := range collection { + initial = accumulator(initial, collection[i], i) } return initial @@ -88,8 +88,8 @@ func ReduceRight[T any, R any](collection []T, accumulator func(agg R, item T, i // 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(item T, index int)) { - for i, item := range collection { - iteratee(item, i) + for i := range collection { + iteratee(collection[i], i) } } @@ -113,13 +113,13 @@ func Uniq[T comparable](collection []T) []T { result := make([]T, 0, len(collection)) seen := make(map[T]struct{}, len(collection)) - for _, item := range collection { - if _, ok := seen[item]; ok { + for i := range collection { + if _, ok := seen[collection[i]]; ok { continue } - seen[item] = struct{}{} - result = append(result, item) + seen[collection[i]] = struct{}{} + result = append(result, collection[i]) } return result @@ -133,15 +133,15 @@ 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)) - for _, item := range collection { - key := iteratee(item) + for i := range collection { + key := iteratee(collection[i]) if _, ok := seen[key]; ok { continue } seen[key] = struct{}{} - result = append(result, item) + result = append(result, collection[i]) } return result @@ -152,10 +152,10 @@ func UniqBy[T any, U comparable](collection []T, iteratee func(item T) 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 { - key := iteratee(item) + for i := range collection { + key := iteratee(collection[i]) - result[key] = append(result[key], item) + result[key] = append(result[key], collection[i]) } return result @@ -195,8 +195,8 @@ func PartitionBy[T any, K comparable](collection []T, iteratee func(item T) K) [ result := [][]T{} seen := map[K]int{} - for _, item := range collection { - key := iteratee(item) + for i := range collection { + key := iteratee(collection[i]) resultIndex, ok := seen[key] if !ok { @@ -205,7 +205,7 @@ func PartitionBy[T any, K comparable](collection []T, iteratee func(item T) K) [ result = append(result, []T{}) } - result[resultIndex] = append(result[resultIndex], item) + result[resultIndex] = append(result[resultIndex], collection[i]) } return result @@ -240,8 +240,8 @@ func Interleave[T any](collections ...[]T) []T { maxSize := 0 totalSize := 0 - for _, c := range collections { - size := len(c) + for i := range collections { + size := len(collections[i]) totalSize += size if size > maxSize { maxSize = size @@ -334,9 +334,9 @@ func RepeatBy[T any](count int, predicate func(index int) T) []T { 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 { - k := iteratee(v) - result[k] = v + for i := range collection { + k := iteratee(collection[i]) + result[k] = collection[i] } return result @@ -349,8 +349,8 @@ func KeyBy[K comparable, V any](collection []V, iteratee func(item V) K) 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, len(collection)) - for _, t := range collection { - k, v := transform(t) + for i := range collection { + k, v := transform(collection[i]) result[k] = v } @@ -422,9 +422,9 @@ func DropRightWhile[T any](collection []T, predicate func(item T) bool) []T { func Reject[V any](collection []V, predicate func(item V, index int) bool) []V { result := []V{} - for i, item := range collection { - if !predicate(item, i) { - result = append(result, item) + for i := range collection { + if !predicate(collection[i], i) { + result = append(result, collection[i]) } } @@ -438,8 +438,8 @@ func Reject[V any](collection []V, predicate func(item V, index int) bool) []V { func RejectMap[T any, R any](collection []T, callback func(item T, index int) (R, bool)) []R { result := []R{} - for i, item := range collection { - if r, ok := callback(item, i); !ok { + for i := range collection { + if r, ok := callback(collection[i], i); !ok { result = append(result, r) } } @@ -453,11 +453,11 @@ func FilterReject[V any](collection []V, predicate func(V, int) bool) (kept []V, kept = make([]V, 0, len(collection)) rejected = make([]V, 0, len(collection)) - for i, item := range collection { - if predicate(item, i) { - kept = append(kept, item) + for i := range collection { + if predicate(collection[i], i) { + kept = append(kept, collection[i]) } else { - rejected = append(rejected, item) + rejected = append(rejected, collection[i]) } } @@ -467,8 +467,8 @@ func FilterReject[V any](collection []V, predicate func(V, int) bool) (kept []V, // Count counts the number of elements in the collection that compare equal to value. // Play: https://go.dev/play/p/Y3FlK54yveC func Count[T comparable](collection []T, value T) (count int) { - for _, item := range collection { - if item == value { + for i := range collection { + if collection[i] == value { count++ } } @@ -479,8 +479,8 @@ 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(item T) bool) (count int) { - for _, item := range collection { - if predicate(item) { + for i := range collection { + if predicate(collection[i]) { count++ } } @@ -493,8 +493,8 @@ func CountBy[T any](collection []T, predicate func(item T) bool) (count int) { func CountValues[T comparable](collection []T) map[T]int { result := make(map[T]int) - for _, item := range collection { - result[item]++ + for i := range collection { + result[collection[i]]++ } return result @@ -506,8 +506,8 @@ func CountValues[T comparable](collection []T) map[T]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 { - result[mapper(item)]++ + for i := range collection { + result[mapper(collection[i])]++ } return result @@ -591,9 +591,9 @@ func Compact[T comparable](collection []T) []T { result := make([]T, 0, len(collection)) - for _, item := range collection { - if item != zero { - result = append(result, item) + for i := range collection { + if collection[i] != zero { + result = append(result, collection[i]) } } diff --git a/string.go b/string.go index dc6283fc..2606ec86 100644 --- a/string.go +++ b/string.go @@ -110,8 +110,8 @@ func RuneLength(str string) int { // PascalCase converts string to pascal case. func PascalCase(str string) string { items := Words(str) - for i, item := range items { - items[i] = Capitalize(item) + for i := range items { + items[i] = Capitalize(items[i]) } return strings.Join(items, "") } @@ -132,8 +132,8 @@ func CamelCase(str string) string { // KebabCase converts string to kebab case. func KebabCase(str string) string { items := Words(str) - for i, item := range items { - items[i] = strings.ToLower(item) + for i := range items { + items[i] = strings.ToLower(items[i]) } return strings.Join(items, "-") } @@ -141,8 +141,8 @@ func KebabCase(str string) string { // SnakeCase converts string to snake case. func SnakeCase(str string) string { items := Words(str) - for i, item := range items { - items[i] = strings.ToLower(item) + for i := range items { + items[i] = strings.ToLower(items[i]) } return strings.Join(items, "_") } diff --git a/tuples.go b/tuples.go index c205c6a1..18a03009 100644 --- a/tuples.go +++ b/tuples.go @@ -508,9 +508,9 @@ func Unzip2[A, B any](tuples []Tuple2[A, B]) ([]A, []B) { r1 := make([]A, 0, size) r2 := make([]B, 0, size) - for _, tuple := range tuples { - r1 = append(r1, tuple.A) - r2 = append(r2, tuple.B) + for i := range tuples { + r1 = append(r1, tuples[i].A) + r2 = append(r2, tuples[i].B) } return r1, r2 @@ -525,10 +525,10 @@ func Unzip3[A, B, C any](tuples []Tuple3[A, B, C]) ([]A, []B, []C) { r2 := make([]B, 0, size) r3 := make([]C, 0, size) - for _, tuple := range tuples { - r1 = append(r1, tuple.A) - r2 = append(r2, tuple.B) - r3 = append(r3, tuple.C) + for i := range tuples { + r1 = append(r1, tuples[i].A) + r2 = append(r2, tuples[i].B) + r3 = append(r3, tuples[i].C) } return r1, r2, r3 @@ -544,11 +544,11 @@ func Unzip4[A, B, C, D any](tuples []Tuple4[A, B, C, D]) ([]A, []B, []C, []D) { r3 := make([]C, 0, size) r4 := make([]D, 0, size) - for _, tuple := range tuples { - r1 = append(r1, tuple.A) - r2 = append(r2, tuple.B) - r3 = append(r3, tuple.C) - r4 = append(r4, tuple.D) + for i := range tuples { + r1 = append(r1, tuples[i].A) + r2 = append(r2, tuples[i].B) + r3 = append(r3, tuples[i].C) + r4 = append(r4, tuples[i].D) } return r1, r2, r3, r4 @@ -565,12 +565,12 @@ func Unzip5[A, B, C, D, E any](tuples []Tuple5[A, B, C, D, E]) ([]A, []B, []C, [ r4 := make([]D, 0, size) r5 := make([]E, 0, size) - for _, tuple := range tuples { - r1 = append(r1, tuple.A) - r2 = append(r2, tuple.B) - r3 = append(r3, tuple.C) - r4 = append(r4, tuple.D) - r5 = append(r5, tuple.E) + for i := range tuples { + r1 = append(r1, tuples[i].A) + r2 = append(r2, tuples[i].B) + r3 = append(r3, tuples[i].C) + r4 = append(r4, tuples[i].D) + r5 = append(r5, tuples[i].E) } return r1, r2, r3, r4, r5 @@ -588,13 +588,13 @@ func Unzip6[A, B, C, D, E, F any](tuples []Tuple6[A, B, C, D, E, F]) ([]A, []B, r5 := make([]E, 0, size) r6 := make([]F, 0, size) - for _, tuple := range tuples { - r1 = append(r1, tuple.A) - r2 = append(r2, tuple.B) - r3 = append(r3, tuple.C) - r4 = append(r4, tuple.D) - r5 = append(r5, tuple.E) - r6 = append(r6, tuple.F) + for i := range tuples { + r1 = append(r1, tuples[i].A) + r2 = append(r2, tuples[i].B) + r3 = append(r3, tuples[i].C) + r4 = append(r4, tuples[i].D) + r5 = append(r5, tuples[i].E) + r6 = append(r6, tuples[i].F) } return r1, r2, r3, r4, r5, r6 @@ -613,14 +613,14 @@ func Unzip7[A, B, C, D, E, F, G any](tuples []Tuple7[A, B, C, D, E, F, G]) ([]A, r6 := make([]F, 0, size) r7 := make([]G, 0, size) - for _, tuple := range tuples { - r1 = append(r1, tuple.A) - r2 = append(r2, tuple.B) - r3 = append(r3, tuple.C) - r4 = append(r4, tuple.D) - r5 = append(r5, tuple.E) - r6 = append(r6, tuple.F) - r7 = append(r7, tuple.G) + for i := range tuples { + r1 = append(r1, tuples[i].A) + r2 = append(r2, tuples[i].B) + r3 = append(r3, tuples[i].C) + r4 = append(r4, tuples[i].D) + r5 = append(r5, tuples[i].E) + r6 = append(r6, tuples[i].F) + r7 = append(r7, tuples[i].G) } return r1, r2, r3, r4, r5, r6, r7 @@ -640,15 +640,15 @@ func Unzip8[A, B, C, D, E, F, G, H any](tuples []Tuple8[A, B, C, D, E, F, G, H]) r7 := make([]G, 0, size) r8 := make([]H, 0, size) - for _, tuple := range tuples { - r1 = append(r1, tuple.A) - r2 = append(r2, tuple.B) - r3 = append(r3, tuple.C) - r4 = append(r4, tuple.D) - r5 = append(r5, tuple.E) - r6 = append(r6, tuple.F) - r7 = append(r7, tuple.G) - r8 = append(r8, tuple.H) + for i := range tuples { + r1 = append(r1, tuples[i].A) + r2 = append(r2, tuples[i].B) + r3 = append(r3, tuples[i].C) + r4 = append(r4, tuples[i].D) + r5 = append(r5, tuples[i].E) + r6 = append(r6, tuples[i].F) + r7 = append(r7, tuples[i].G) + r8 = append(r8, tuples[i].H) } return r1, r2, r3, r4, r5, r6, r7, r8 @@ -669,16 +669,16 @@ func Unzip9[A, B, C, D, E, F, G, H, I any](tuples []Tuple9[A, B, C, D, E, F, G, r8 := make([]H, 0, size) r9 := make([]I, 0, size) - for _, tuple := range tuples { - r1 = append(r1, tuple.A) - r2 = append(r2, tuple.B) - r3 = append(r3, tuple.C) - r4 = append(r4, tuple.D) - r5 = append(r5, tuple.E) - r6 = append(r6, tuple.F) - r7 = append(r7, tuple.G) - r8 = append(r8, tuple.H) - r9 = append(r9, tuple.I) + for i := range tuples { + r1 = append(r1, tuples[i].A) + r2 = append(r2, tuples[i].B) + r3 = append(r3, tuples[i].C) + r4 = append(r4, tuples[i].D) + r5 = append(r5, tuples[i].E) + r6 = append(r6, tuples[i].F) + r7 = append(r7, tuples[i].G) + r8 = append(r8, tuples[i].H) + r9 = append(r9, tuples[i].I) } return r1, r2, r3, r4, r5, r6, r7, r8, r9 @@ -691,8 +691,8 @@ func UnzipBy2[In any, A any, B any](items []In, iteratee func(In) (a A, b B)) ([ r1 := make([]A, 0, size) r2 := make([]B, 0, size) - for _, item := range items { - a, b := iteratee(item) + for i := range items { + a, b := iteratee(items[i]) r1 = append(r1, a) r2 = append(r2, b) } @@ -708,8 +708,8 @@ func UnzipBy3[In any, A any, B any, C any](items []In, iteratee func(In) (a A, b r2 := make([]B, 0, size) r3 := make([]C, 0, size) - for _, item := range items { - a, b, c := iteratee(item) + for i := range items { + a, b, c := iteratee(items[i]) r1 = append(r1, a) r2 = append(r2, b) r3 = append(r3, c) @@ -727,8 +727,8 @@ func UnzipBy4[In any, A any, B any, C any, D any](items []In, iteratee func(In) r3 := make([]C, 0, size) r4 := make([]D, 0, size) - for _, item := range items { - a, b, c, d := iteratee(item) + for i := range items { + a, b, c, d := iteratee(items[i]) r1 = append(r1, a) r2 = append(r2, b) r3 = append(r3, c) @@ -748,8 +748,8 @@ func UnzipBy5[In any, A any, B any, C any, D any, E any](items []In, iteratee fu r4 := make([]D, 0, size) r5 := make([]E, 0, size) - for _, item := range items { - a, b, c, d, e := iteratee(item) + for i := range items { + a, b, c, d, e := iteratee(items[i]) r1 = append(r1, a) r2 = append(r2, b) r3 = append(r3, c) @@ -771,8 +771,8 @@ func UnzipBy6[In any, A any, B any, C any, D any, E any, F any](items []In, iter r5 := make([]E, 0, size) r6 := make([]F, 0, size) - for _, item := range items { - a, b, c, d, e, f := iteratee(item) + for i := range items { + a, b, c, d, e, f := iteratee(items[i]) r1 = append(r1, a) r2 = append(r2, b) r3 = append(r3, c) @@ -796,8 +796,8 @@ func UnzipBy7[In any, A any, B any, C any, D any, E any, F any, G any](items []I r6 := make([]F, 0, size) r7 := make([]G, 0, size) - for _, item := range items { - a, b, c, d, e, f, g := iteratee(item) + for i := range items { + a, b, c, d, e, f, g := iteratee(items[i]) r1 = append(r1, a) r2 = append(r2, b) r3 = append(r3, c) @@ -823,8 +823,8 @@ func UnzipBy8[In any, A any, B any, C any, D any, E any, F any, G any, H any](it r7 := make([]G, 0, size) r8 := make([]H, 0, size) - for _, item := range items { - a, b, c, d, e, f, g, h := iteratee(item) + for i := range items { + a, b, c, d, e, f, g, h := iteratee(items[i]) r1 = append(r1, a) r2 = append(r2, b) r3 = append(r3, c) @@ -852,8 +852,8 @@ func UnzipBy9[In any, A any, B any, C any, D any, E any, F any, G any, H any, I r8 := make([]H, 0, size) r9 := make([]I, 0, size) - for _, item := range items { - a, b, c, d, e, f, g, h, i := iteratee(item) + for i := range items { + a, b, c, d, e, f, g, h, i := iteratee(items[i]) r1 = append(r1, a) r2 = append(r2, b) r3 = append(r3, c) diff --git a/type_manipulation.go b/type_manipulation.go index bb4f6779..3e965183 100644 --- a/type_manipulation.go +++ b/type_manipulation.go @@ -61,8 +61,8 @@ func ToSlicePtr[T any](collection []T) []*T { // ToAnySlice returns a slice with all elements mapped to `any` type func ToAnySlice[T any](collection []T) []any { result := make([]any, len(collection)) - for i, item := range collection { - result[i] = item + for i := range collection { + result[i] = collection[i] } return result } @@ -78,8 +78,8 @@ func FromAnySlice[T any](in []any) (out []T, ok bool) { }() result := make([]T, len(in)) - for i, item := range in { - result[i] = item.(T) + for i := range in { + result[i] = in[i].(T) } return result, true } @@ -103,10 +103,10 @@ func IsNotEmpty[T comparable](v T) bool { } // Coalesce returns the first non-empty arguments. Arguments must be comparable. -func Coalesce[T comparable](v ...T) (result T, ok bool) { - for _, e := range v { - if e != result { - result = e +func Coalesce[T comparable](values ...T) (result T, ok bool) { + for i := range values { + if values[i] != result { + result = values[i] ok = true return }