mirror of
https://github.com/samber/lo.git
synced 2025-09-27 04:15:58 +08:00
lint: enable gofumpt extra rules (#690)
Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
This commit is contained in:
@@ -93,6 +93,9 @@ formatters:
|
||||
enable:
|
||||
- gofmt
|
||||
- gofumpt
|
||||
settings:
|
||||
gofumpt:
|
||||
extra-rules: true
|
||||
exclusions:
|
||||
generated: lax
|
||||
paths:
|
||||
|
10
channel.go
10
channel.go
@@ -15,7 +15,7 @@ type DispatchingStrategy[T any] func(msg T, index uint64, channels []<-chan T) i
|
||||
// Close events are propagated to children.
|
||||
// Underlying channels can have a fixed buffer capacity or be unbuffered when cap is 0.
|
||||
// Play: https://go.dev/play/p/UZGu2wVg3J2
|
||||
func ChannelDispatcher[T any](stream <-chan T, count int, channelBufferCap int, strategy DispatchingStrategy[T]) []<-chan T {
|
||||
func ChannelDispatcher[T any](stream <-chan T, count, channelBufferCap int, strategy DispatchingStrategy[T]) []<-chan T {
|
||||
children := createChannels[T](count, channelBufferCap)
|
||||
|
||||
roChildren := channelsToReadOnly(children)
|
||||
@@ -42,7 +42,7 @@ func ChannelDispatcher[T any](stream <-chan T, count int, channelBufferCap int,
|
||||
return roChildren
|
||||
}
|
||||
|
||||
func createChannels[T any](count int, channelBufferCap int) []chan T {
|
||||
func createChannels[T any](count, channelBufferCap int) []chan T {
|
||||
children := make([]chan T, 0, count)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
@@ -145,7 +145,7 @@ func DispatchingStrategyFirst[T any](msg T, index uint64, channels []<-chan T) i
|
||||
func DispatchingStrategyLeast[T any](msg T, index uint64, channels []<-chan T) int {
|
||||
seq := Range(len(channels))
|
||||
|
||||
return MinBy(seq, func(item int, mIn int) bool {
|
||||
return MinBy(seq, func(item, mIn int) bool {
|
||||
return len(channels[item]) < len(channels[mIn])
|
||||
})
|
||||
}
|
||||
@@ -156,7 +156,7 @@ func DispatchingStrategyLeast[T any](msg T, index uint64, channels []<-chan T) i
|
||||
func DispatchingStrategyMost[T any](msg T, index uint64, channels []<-chan T) int {
|
||||
seq := Range(len(channels))
|
||||
|
||||
return MaxBy(seq, func(item int, mAx int) bool {
|
||||
return MaxBy(seq, func(item, mAx int) bool {
|
||||
return len(channels[item]) > len(channels[mAx]) && channelIsNotFull(channels[item])
|
||||
})
|
||||
}
|
||||
@@ -310,7 +310,7 @@ func ChannelMerge[T any](channelBufferCap int, upstreams ...<-chan T) <-chan T {
|
||||
// When upstream channel reaches EOF, downstream channels close. If any downstream
|
||||
// channels is full, broadcasting is paused.
|
||||
// Play: https://go.dev/play/p/2LHxcjKX23L
|
||||
func FanOut[T any](count int, channelsBufferCap int, upstream <-chan T) []<-chan T {
|
||||
func FanOut[T any](count, channelsBufferCap int, upstream <-chan T) []<-chan T {
|
||||
downstreams := createChannels[T](count, channelsBufferCap)
|
||||
|
||||
go func() {
|
||||
|
@@ -109,7 +109,7 @@ func Async6[A, B, C, D, E, F any](f func() (A, B, C, D, E, F)) <-chan Tuple6[A,
|
||||
|
||||
// WaitFor runs periodically until a condition is validated.
|
||||
// Play: https://go.dev/play/p/t_wTDmubbK3
|
||||
func WaitFor(condition func(i int) bool, timeout time.Duration, heartbeatDelay time.Duration) (totalIterations int, elapsed time.Duration, conditionFound bool) {
|
||||
func WaitFor(condition func(i int) bool, timeout, heartbeatDelay time.Duration) (totalIterations int, elapsed time.Duration, conditionFound bool) {
|
||||
conditionWithContext := func(_ context.Context, currentIteration int) bool {
|
||||
return condition(currentIteration)
|
||||
}
|
||||
@@ -118,7 +118,7 @@ func WaitFor(condition func(i int) bool, timeout time.Duration, heartbeatDelay t
|
||||
|
||||
// WaitForWithContext runs periodically until a condition is validated or context is canceled.
|
||||
// Play: https://go.dev/play/p/t_wTDmubbK3
|
||||
func WaitForWithContext(ctx context.Context, condition func(ctx context.Context, currentIteration int) bool, timeout time.Duration, heartbeatDelay time.Duration) (totalIterations int, elapsed time.Duration, conditionFound bool) {
|
||||
func WaitForWithContext(ctx context.Context, condition func(ctx context.Context, currentIteration int) bool, timeout, heartbeatDelay time.Duration) (totalIterations int, elapsed time.Duration, conditionFound bool) {
|
||||
start := time.Now()
|
||||
|
||||
if ctx.Err() != nil {
|
||||
|
@@ -3,7 +3,7 @@ package lo
|
||||
// Ternary is a single line if/else statement.
|
||||
// Take care to avoid dereferencing potentially nil pointers in your A/B expressions, because they are both evaluated. See TernaryF to avoid this problem.
|
||||
// Play: https://go.dev/play/p/t-D7WBL44h2
|
||||
func Ternary[T any](condition bool, ifOutput T, elseOutput T) T {
|
||||
func Ternary[T any](condition bool, ifOutput, elseOutput T) T {
|
||||
if condition {
|
||||
return ifOutput
|
||||
}
|
||||
@@ -13,7 +13,7 @@ func Ternary[T any](condition bool, ifOutput T, elseOutput T) T {
|
||||
|
||||
// TernaryF is a single line if/else statement whose options are functions.
|
||||
// Play: https://go.dev/play/p/AO4VW20JoqM
|
||||
func TernaryF[T any](condition bool, ifFunc func() T, elseFunc func() T) T {
|
||||
func TernaryF[T any](condition bool, ifFunc, elseFunc func() T) T {
|
||||
if condition {
|
||||
return ifFunc()
|
||||
}
|
||||
|
14
find.go
14
find.go
@@ -38,7 +38,7 @@ func LastIndexOf[T comparable](collection []T, element T) int {
|
||||
|
||||
// HasPrefix returns true if the collection has the prefix.
|
||||
// Play: https://go.dev/play/p/SrljzVDpMQM
|
||||
func HasPrefix[T comparable](collection []T, prefix []T) bool {
|
||||
func HasPrefix[T comparable](collection, prefix []T) bool {
|
||||
if len(collection) < len(prefix) {
|
||||
return false
|
||||
}
|
||||
@@ -54,7 +54,7 @@ func HasPrefix[T comparable](collection []T, prefix []T) bool {
|
||||
|
||||
// HasSuffix returns true if the collection has the suffix.
|
||||
// Play: https://go.dev/play/p/bJeLetQNAON
|
||||
func HasSuffix[T comparable](collection []T, suffix []T) bool {
|
||||
func HasSuffix[T comparable](collection, suffix []T) bool {
|
||||
if len(collection) < len(suffix) {
|
||||
return false
|
||||
}
|
||||
@@ -125,7 +125,7 @@ func FindOrElse[T any](collection []T, fallback T, predicate func(item T) bool)
|
||||
|
||||
// FindKey returns the key of the first value matching.
|
||||
// Play: https://go.dev/play/p/Bg0w1VDPYXx
|
||||
func FindKey[K comparable, V comparable](object map[K]V, value V) (K, bool) {
|
||||
func FindKey[K, V comparable](object map[K]V, value V) (K, bool) {
|
||||
for k := range object {
|
||||
if object[k] == value {
|
||||
return k, true
|
||||
@@ -311,7 +311,7 @@ func MinIndex[T constraints.Ordered](collection []T) (T, int) {
|
||||
// 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 the collection is empty.
|
||||
func MinBy[T any](collection []T, comparison func(a T, b T) bool) T {
|
||||
func MinBy[T any](collection []T, comparison func(a, b T) bool) T {
|
||||
var mIn T
|
||||
|
||||
if len(collection) == 0 {
|
||||
@@ -334,7 +334,7 @@ func MinBy[T any](collection []T, comparison func(a T, b T) bool) T {
|
||||
// MinIndexBy search the minimum value of a collection using the given comparison function and the index of the minimum value.
|
||||
// If several values of the collection are equal to the smallest value, returns the first such value.
|
||||
// Returns (zero value, -1) when the collection is empty.
|
||||
func MinIndexBy[T any](collection []T, comparison func(a T, b T) bool) (T, int) {
|
||||
func MinIndexBy[T any](collection []T, comparison func(a, b T) bool) (T, int) {
|
||||
var (
|
||||
mIn T
|
||||
index int
|
||||
@@ -456,7 +456,7 @@ func MaxIndex[T constraints.Ordered](collection []T) (T, int) {
|
||||
// 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 the collection is empty.
|
||||
func MaxBy[T any](collection []T, comparison func(a T, b T) bool) T {
|
||||
func MaxBy[T any](collection []T, comparison func(a, b T) bool) T {
|
||||
var mAx T
|
||||
|
||||
if len(collection) == 0 {
|
||||
@@ -479,7 +479,7 @@ func MaxBy[T any](collection []T, comparison func(a T, b T) bool) T {
|
||||
// MaxIndexBy search the maximum value of a collection using the given comparison function and the index of the maximum value.
|
||||
// If several values of the collection are equal to the greatest value, returns the first such value.
|
||||
// Returns (zero value, -1) when the collection is empty.
|
||||
func MaxIndexBy[T any](collection []T, comparison func(a T, b T) bool) (T, int) {
|
||||
func MaxIndexBy[T any](collection []T, comparison func(a, b T) bool) (T, int) {
|
||||
var (
|
||||
mAx T
|
||||
index int
|
||||
|
24
find_test.go
24
find_test.go
@@ -316,13 +316,13 @@ func TestMinBy(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := MinBy([]string{"s1", "string2", "s3"}, func(item string, mIn string) bool {
|
||||
result1 := MinBy([]string{"s1", "string2", "s3"}, func(item, mIn string) bool {
|
||||
return len(item) < len(mIn)
|
||||
})
|
||||
result2 := MinBy([]string{"string1", "string2", "s3"}, func(item string, mIn string) bool {
|
||||
result2 := MinBy([]string{"string1", "string2", "s3"}, func(item, mIn string) bool {
|
||||
return len(item) < len(mIn)
|
||||
})
|
||||
result3 := MinBy([]string{}, func(item string, mIn string) bool {
|
||||
result3 := MinBy([]string{}, func(item, mIn string) bool {
|
||||
return len(item) < len(mIn)
|
||||
})
|
||||
|
||||
@@ -335,13 +335,13 @@ func TestMinIndexBy(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1, index1 := MinIndexBy([]string{"s1", "string2", "s3"}, func(item string, mIn string) bool {
|
||||
result1, index1 := MinIndexBy([]string{"s1", "string2", "s3"}, func(item, mIn string) bool {
|
||||
return len(item) < len(mIn)
|
||||
})
|
||||
result2, index2 := MinIndexBy([]string{"string1", "string2", "s3"}, func(item string, mIn string) bool {
|
||||
result2, index2 := MinIndexBy([]string{"string1", "string2", "s3"}, func(item, mIn string) bool {
|
||||
return len(item) < len(mIn)
|
||||
})
|
||||
result3, index3 := MinIndexBy([]string{}, func(item string, mIn string) bool {
|
||||
result3, index3 := MinIndexBy([]string{}, func(item, mIn string) bool {
|
||||
return len(item) < len(mIn)
|
||||
})
|
||||
|
||||
@@ -435,13 +435,13 @@ func TestMaxBy(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := MaxBy([]string{"s1", "string2", "s3"}, func(item string, mAx string) bool {
|
||||
result1 := MaxBy([]string{"s1", "string2", "s3"}, func(item, mAx string) bool {
|
||||
return len(item) > len(mAx)
|
||||
})
|
||||
result2 := MaxBy([]string{"string1", "string2", "s3"}, func(item string, mAx string) bool {
|
||||
result2 := MaxBy([]string{"string1", "string2", "s3"}, func(item, mAx string) bool {
|
||||
return len(item) > len(mAx)
|
||||
})
|
||||
result3 := MaxBy([]string{}, func(item string, mAx string) bool {
|
||||
result3 := MaxBy([]string{}, func(item, mAx string) bool {
|
||||
return len(item) > len(mAx)
|
||||
})
|
||||
|
||||
@@ -454,13 +454,13 @@ func TestMaxIndexBy(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1, index1 := MaxIndexBy([]string{"s1", "string2", "s3"}, func(item string, mAx string) bool {
|
||||
result1, index1 := MaxIndexBy([]string{"s1", "string2", "s3"}, func(item, mAx string) bool {
|
||||
return len(item) > len(mAx)
|
||||
})
|
||||
result2, index2 := MaxIndexBy([]string{"string1", "string2", "s3"}, func(item string, mAx string) bool {
|
||||
result2, index2 := MaxIndexBy([]string{"string1", "string2", "s3"}, func(item, mAx string) bool {
|
||||
return len(item) > len(mAx)
|
||||
})
|
||||
result3, index3 := MaxIndexBy([]string{}, func(item string, mAx string) bool {
|
||||
result3, index3 := MaxIndexBy([]string{}, func(item, mAx string) bool {
|
||||
return len(item) > len(mAx)
|
||||
})
|
||||
|
||||
|
@@ -35,7 +35,7 @@ func TestPartial2(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
add := func(x float64, y int, z int) string {
|
||||
add := func(x float64, y, z int) string {
|
||||
return strconv.Itoa(int(x) + y + z)
|
||||
}
|
||||
f := Partial2(add, 5)
|
||||
@@ -47,7 +47,7 @@ func TestPartial3(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
add := func(x float64, y int, z int, a float32) string {
|
||||
add := func(x float64, y, z int, a float32) string {
|
||||
return strconv.Itoa(int(x) + y + z + int(a))
|
||||
}
|
||||
f := Partial3(add, 5)
|
||||
@@ -59,7 +59,7 @@ func TestPartial4(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
add := func(x float64, y int, z int, a float32, b int32) string {
|
||||
add := func(x float64, y, z int, a float32, b int32) string {
|
||||
return strconv.Itoa(int(x) + y + z + int(a) + int(b))
|
||||
}
|
||||
f := Partial4(add, 5)
|
||||
@@ -71,7 +71,7 @@ func TestPartial5(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
add := func(x float64, y int, z int, a float32, b int32, c int) string {
|
||||
add := func(x float64, y, z int, a float32, b int32, c int) string {
|
||||
return strconv.Itoa(int(x) + y + z + int(a) + int(b) + c)
|
||||
}
|
||||
f := Partial5(add, 5)
|
||||
|
14
intersect.go
14
intersect.go
@@ -26,7 +26,7 @@ func ContainsBy[T any](collection []T, predicate func(item T) bool) bool {
|
||||
|
||||
// Every returns true if all elements of a subset are contained in a collection or if the subset is empty.
|
||||
// Play: https://go.dev/play/p/W1EvyqY6t9j
|
||||
func Every[T comparable](collection []T, subset []T) bool {
|
||||
func Every[T comparable](collection, subset []T) bool {
|
||||
for i := range subset {
|
||||
if !Contains(collection, subset[i]) {
|
||||
return false
|
||||
@@ -51,7 +51,7 @@ 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 in a collection.
|
||||
// If the subset is empty Some returns false.
|
||||
// Play: https://go.dev/play/p/Lj4ceFkeT9V
|
||||
func Some[T comparable](collection []T, subset []T) bool {
|
||||
func Some[T comparable](collection, subset []T) bool {
|
||||
for i := range subset {
|
||||
if Contains(collection, subset[i]) {
|
||||
return true
|
||||
@@ -76,7 +76,7 @@ func SomeBy[T any](collection []T, predicate func(item T) bool) bool {
|
||||
|
||||
// None returns true if no element of a subset is contained in a collection or if the subset is empty.
|
||||
// Play: https://go.dev/play/p/fye7JsmxzPV
|
||||
func None[T comparable](collection []T, subset []T) bool {
|
||||
func None[T comparable](collection, subset []T) bool {
|
||||
for i := range subset {
|
||||
if Contains(collection, subset[i]) {
|
||||
return false
|
||||
@@ -100,7 +100,7 @@ func NoneBy[T any](collection []T, predicate func(item T) bool) bool {
|
||||
|
||||
// Intersect returns the intersection between two collections.
|
||||
// Play: https://go.dev/play/p/uuElL9X9e58
|
||||
func Intersect[T comparable, Slice ~[]T](list1 Slice, list2 Slice) Slice {
|
||||
func Intersect[T comparable, Slice ~[]T](list1, list2 Slice) Slice {
|
||||
result := Slice{}
|
||||
seen := map[T]struct{}{}
|
||||
|
||||
@@ -121,7 +121,7 @@ func Intersect[T comparable, Slice ~[]T](list1 Slice, list2 Slice) Slice {
|
||||
// The first value is the collection of elements absent from list2.
|
||||
// The second value is the collection of elements absent from list1.
|
||||
// Play: https://go.dev/play/p/pKE-JgzqRpz
|
||||
func Difference[T comparable, Slice ~[]T](list1 Slice, list2 Slice) (Slice, Slice) {
|
||||
func Difference[T comparable, Slice ~[]T](list1, list2 Slice) (Slice, Slice) {
|
||||
left := Slice{}
|
||||
right := Slice{}
|
||||
|
||||
@@ -244,7 +244,7 @@ func WithoutNth[T comparable, Slice ~[]T](collection Slice, nths ...int) Slice {
|
||||
// If there are duplicate elements, the number of occurrences in each list should match.
|
||||
// The order of elements is not checked.
|
||||
// Play: https://go.dev/play/p/XWSEM4Ic_t0
|
||||
func ElementsMatch[T comparable, Slice ~[]T](list1 Slice, list2 Slice) bool {
|
||||
func ElementsMatch[T comparable, Slice ~[]T](list1, list2 Slice) bool {
|
||||
return ElementsMatchBy(list1, list2, func(item T) T { return item })
|
||||
}
|
||||
|
||||
@@ -252,7 +252,7 @@ func ElementsMatch[T comparable, Slice ~[]T](list1 Slice, list2 Slice) bool {
|
||||
// If there are duplicate keys, the number of occurrences in each list should match.
|
||||
// The order of elements is not checked.
|
||||
// Play: https://go.dev/play/p/XWSEM4Ic_t0
|
||||
func ElementsMatchBy[T any, K comparable](list1 []T, list2 []T, iteratee func(item T) K) bool {
|
||||
func ElementsMatchBy[T any, K comparable](list1, list2 []T, iteratee func(item T) K) bool {
|
||||
if len(list1) != len(list2) {
|
||||
return false
|
||||
}
|
||||
|
14
map.go
14
map.go
@@ -69,7 +69,7 @@ func Values[K comparable, V any](in ...map[K]V) []V {
|
||||
|
||||
// UniqValues creates a slice of unique values in the map.
|
||||
// Play: https://go.dev/play/p/nf6bXMh7rM3
|
||||
func UniqValues[K comparable, V comparable](in ...map[K]V) []V {
|
||||
func UniqValues[K, V comparable](in ...map[K]V) []V {
|
||||
size := 0
|
||||
for i := range in {
|
||||
size += len(in[i])
|
||||
@@ -127,7 +127,7 @@ func PickByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map {
|
||||
|
||||
// PickByValues returns same map type filtered by given values.
|
||||
// Play: https://go.dev/play/p/1zdzSvbfsJc
|
||||
func PickByValues[K comparable, V comparable, Map ~map[K]V](in Map, values []V) Map {
|
||||
func PickByValues[K, V comparable, Map ~map[K]V](in Map, values []V) Map {
|
||||
r := Map{}
|
||||
for k := range in {
|
||||
if Contains(values, in[k]) {
|
||||
@@ -164,7 +164,7 @@ func OmitByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map {
|
||||
|
||||
// OmitByValues returns same map type filtered by given values.
|
||||
// Play: https://go.dev/play/p/9UYZi-hrs8j
|
||||
func OmitByValues[K comparable, V comparable, Map ~map[K]V](in Map, values []V) Map {
|
||||
func OmitByValues[K, V comparable, Map ~map[K]V](in Map, values []V) Map {
|
||||
r := Map{}
|
||||
for k := range in {
|
||||
if !Contains(values, in[k]) {
|
||||
@@ -219,7 +219,7 @@ func FromPairs[K comparable, V any](entries []Entry[K, V]) map[K]V {
|
||||
// contains duplicate values, subsequent values overwrite property assignments
|
||||
// of previous values.
|
||||
// Play: https://go.dev/play/p/rFQ4rak6iA1
|
||||
func Invert[K comparable, V comparable](in map[K]V) map[V]K {
|
||||
func Invert[K, V comparable](in map[K]V) map[V]K {
|
||||
out := make(map[V]K, len(in))
|
||||
|
||||
for k := range in {
|
||||
@@ -292,7 +292,7 @@ func MapKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(value
|
||||
|
||||
// MapValues manipulates 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(value V, key K) R) map[K]R {
|
||||
func MapValues[K comparable, V, 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 := range in {
|
||||
@@ -317,7 +317,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 specified iteratee.
|
||||
// Play: https://go.dev/play/p/ZuiCZpDt6LD
|
||||
func MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) R) []R {
|
||||
func MapToSlice[K comparable, V, R any](in map[K]V, iteratee func(key K, value V) R) []R {
|
||||
result := make([]R, 0, len(in))
|
||||
|
||||
for k := range in {
|
||||
@@ -332,7 +332,7 @@ func MapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, val
|
||||
// If the boolean is false, the value is not added to the result slice.
|
||||
// The order of the keys in the input map is not specified and the order of the keys in the output slice is not guaranteed.
|
||||
// Play: https://go.dev/play/p/jgsD_Kil9pV
|
||||
func FilterMapToSlice[K comparable, V any, R any](in map[K]V, iteratee func(key K, value V) (R, bool)) []R {
|
||||
func FilterMapToSlice[K comparable, V, R any](in map[K]V, iteratee func(key K, value V) (R, bool)) []R {
|
||||
result := make([]R, 0, len(in))
|
||||
|
||||
for k := range in {
|
||||
|
@@ -191,7 +191,7 @@ func ExampleChunkEntries() {
|
||||
func ExampleMapKeys() {
|
||||
kv := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}
|
||||
|
||||
result := MapKeys(kv, func(_ int, k int) string {
|
||||
result := MapKeys(kv, func(_, k int) string {
|
||||
return strconv.FormatInt(int64(k), 10)
|
||||
})
|
||||
|
||||
@@ -202,7 +202,7 @@ func ExampleMapKeys() {
|
||||
func ExampleMapValues() {
|
||||
kv := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}
|
||||
|
||||
result := MapValues(kv, func(v int, _ int) string {
|
||||
result := MapValues(kv, func(v, _ int) string {
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
})
|
||||
|
||||
|
16
map_test.go
16
map_test.go
@@ -351,10 +351,10 @@ func TestMapKeys(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := MapKeys(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(x int, _ int) string {
|
||||
result1 := MapKeys(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(x, _ int) string {
|
||||
return "Hello"
|
||||
})
|
||||
result2 := MapKeys(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(_ int, v int) string {
|
||||
result2 := MapKeys(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(_, v int) string {
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
})
|
||||
|
||||
@@ -366,10 +366,10 @@ func TestMapValues(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := MapValues(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(x int, _ int) string {
|
||||
result1 := MapValues(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(x, _ int) string {
|
||||
return "Hello"
|
||||
})
|
||||
result2 := MapValues(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(x int, _ int) string {
|
||||
result2 := MapValues(map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(x, _ int) string {
|
||||
return strconv.FormatInt(int64(x), 10)
|
||||
})
|
||||
|
||||
@@ -504,10 +504,10 @@ func TestMapToSlice(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := MapToSlice(map[int]int{1: 5, 2: 6, 3: 7, 4: 8}, func(k int, v int) string {
|
||||
result1 := MapToSlice(map[int]int{1: 5, 2: 6, 3: 7, 4: 8}, func(k, v int) string {
|
||||
return fmt.Sprintf("%d_%d", k, v)
|
||||
})
|
||||
result2 := MapToSlice(map[int]int{1: 5, 2: 6, 3: 7, 4: 8}, func(k int, _ int) string {
|
||||
result2 := MapToSlice(map[int]int{1: 5, 2: 6, 3: 7, 4: 8}, func(k, _ int) string {
|
||||
return strconv.FormatInt(int64(k), 10)
|
||||
})
|
||||
|
||||
@@ -519,10 +519,10 @@ func TestFilterMapToSlice(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := FilterMapToSlice(map[int]int{1: 5, 2: 6, 3: 7, 4: 8}, func(k int, v int) (string, bool) {
|
||||
result1 := FilterMapToSlice(map[int]int{1: 5, 2: 6, 3: 7, 4: 8}, func(k, v int) (string, bool) {
|
||||
return fmt.Sprintf("%d_%d", k, v), k%2 == 0
|
||||
})
|
||||
result2 := FilterMapToSlice(map[int]int{1: 5, 2: 6, 3: 7, 4: 8}, func(k int, _ int) (string, bool) {
|
||||
result2 := FilterMapToSlice(map[int]int{1: 5, 2: 6, 3: 7, 4: 8}, func(k, _ int) (string, bool) {
|
||||
return strconv.FormatInt(int64(k), 10), k%2 == 0
|
||||
})
|
||||
|
||||
|
2
math.go
2
math.go
@@ -56,7 +56,7 @@ func RangeWithSteps[T constraints.Integer | constraints.Float](start, end, step
|
||||
|
||||
// Clamp clamps number within the inclusive lower and upper bounds.
|
||||
// Play: https://go.dev/play/p/RU4lJNC2hlI
|
||||
func Clamp[T constraints.Ordered](value T, mIn T, mAx T) T {
|
||||
func Clamp[T constraints.Ordered](value, mIn, mAx T) T {
|
||||
if value < mIn {
|
||||
return mIn
|
||||
} else if value > mAx {
|
||||
|
@@ -42,7 +42,7 @@ func ExampleMap() {
|
||||
func ExampleMapI() {
|
||||
list := []int{1, 2, 3, 4}
|
||||
|
||||
MapI(list, func(nbr int, index int) int {
|
||||
MapI(list, func(nbr, index int) int {
|
||||
return nbr * index
|
||||
})
|
||||
|
||||
|
@@ -31,7 +31,7 @@ func TestFilterI(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
r1 := FilterI([]int{1, 2, 3, 4}, func(x int, i int) bool {
|
||||
r1 := FilterI([]int{1, 2, 3, 4}, func(x, i int) bool {
|
||||
is.Equal(i, x-1)
|
||||
return x%2 == 0
|
||||
})
|
||||
@@ -61,14 +61,14 @@ func TestMapI(t *testing.T) {
|
||||
is := assert.New(t)
|
||||
|
||||
list := []int{1, 2, 3, 4}
|
||||
MapI(list, func(x int, index int) int {
|
||||
MapI(list, func(x, index int) int {
|
||||
is.Equal(index, x-1)
|
||||
return x * 2
|
||||
})
|
||||
is.Equal([]int{2, 4, 6, 8}, list)
|
||||
|
||||
list = []int{1, 2, 3, 4}
|
||||
MapI(list, func(x int, index int) int {
|
||||
MapI(list, func(x, index int) int {
|
||||
is.Equal(index, x-1)
|
||||
return x * 4
|
||||
})
|
||||
|
@@ -5,7 +5,7 @@ import "sync"
|
||||
// Map manipulates a slice and transforms it to a slice of another type.
|
||||
// `iteratee` is called in parallel. Result keep the same order.
|
||||
// Play: https://go.dev/play/p/sCJaB3quRMC
|
||||
func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R {
|
||||
func Map[T, R any](collection []T, iteratee func(item T, index int) R) []R {
|
||||
result := make([]R, len(collection))
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
@@ -13,7 +13,7 @@ func TestMap(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := Map([]int{1, 2, 3, 4}, func(x int, _ int) string {
|
||||
result1 := Map([]int{1, 2, 3, 4}, func(x, _ int) string {
|
||||
return "Hello"
|
||||
})
|
||||
result2 := Map([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
|
||||
@@ -30,7 +30,7 @@ func TestForEach(t *testing.T) {
|
||||
|
||||
var counter uint64
|
||||
collection := []int{1, 2, 3, 4}
|
||||
ForEach(collection, func(x int, i int) {
|
||||
ForEach(collection, func(x, i int) {
|
||||
atomic.AddUint64(&counter, 1)
|
||||
})
|
||||
|
||||
|
4
retry.go
4
retry.go
@@ -346,13 +346,13 @@ func (th *throttleBy[T]) reset() {
|
||||
// NewThrottle creates a throttled instance that invokes given functions only once in every interval.
|
||||
// This returns 2 functions, First one is throttled function and Second one is a function to reset interval.
|
||||
// Play: https://go.dev/play/p/qQn3fm8Z7jS
|
||||
func NewThrottle(interval time.Duration, f ...func()) (throttle func(), reset func()) {
|
||||
func NewThrottle(interval time.Duration, f ...func()) (throttle, reset func()) {
|
||||
return NewThrottleWithCount(interval, 1, f...)
|
||||
}
|
||||
|
||||
// NewThrottleWithCount is NewThrottle with count limit, throttled function will be invoked count times in every interval.
|
||||
// Play: https://go.dev/play/p/w5nc0MgWtjC
|
||||
func NewThrottleWithCount(interval time.Duration, count int, f ...func()) (throttle func(), reset func()) {
|
||||
func NewThrottleWithCount(interval time.Duration, count int, f ...func()) (throttle, reset func()) {
|
||||
callbacks := Map(f, func(item func(), _ int) func(struct{}) {
|
||||
return func(struct{}) {
|
||||
item()
|
||||
|
@@ -325,19 +325,19 @@ func TestDebounceBy(t *testing.T) {
|
||||
mu := sync.Mutex{}
|
||||
output := map[int]int{0: 0, 1: 0, 2: 0}
|
||||
|
||||
f1 := func(key int, count int) {
|
||||
f1 := func(key, count int) {
|
||||
mu.Lock()
|
||||
output[key] += count
|
||||
mu.Unlock()
|
||||
// fmt.Printf("[key=%d] 1. Called once after 10ms when func stopped invoking!\n", key)
|
||||
}
|
||||
f2 := func(key int, count int) {
|
||||
f2 := func(key, count int) {
|
||||
mu.Lock()
|
||||
output[key] += count
|
||||
mu.Unlock()
|
||||
// fmt.Printf("[key=%d] 2. Called once after 10ms when func stopped invoking!\n", key)
|
||||
}
|
||||
f3 := func(key int, count int) {
|
||||
f3 := func(key, count int) {
|
||||
mu.Lock()
|
||||
output[key] += count
|
||||
mu.Unlock()
|
||||
|
36
slice.go
36
slice.go
@@ -23,7 +23,7 @@ func Filter[T any, Slice ~[]T](collection Slice, predicate func(item T, index in
|
||||
|
||||
// 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(item T, index int) R) []R {
|
||||
func Map[T, R any](collection []T, iteratee func(item T, index int) R) []R {
|
||||
result := make([]R, len(collection))
|
||||
|
||||
for i := range collection {
|
||||
@@ -55,7 +55,7 @@ func UniqMap[T any, R comparable](collection []T, iteratee func(item T, index in
|
||||
// - whether the result element should be included or not.
|
||||
//
|
||||
// Play: https://go.dev/play/p/CgHYNUpOd1I
|
||||
func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R, bool)) []R {
|
||||
func FilterMap[T, R any](collection []T, callback func(item T, index int) (R, bool)) []R {
|
||||
result := make([]R, 0, len(collection))
|
||||
|
||||
for i := range collection {
|
||||
@@ -71,7 +71,7 @@ func FilterMap[T any, R any](collection []T, callback func(item T, index int) (R
|
||||
// The transform function can either return a slice or a `nil`, and in the `nil` case
|
||||
// no value is added to the final slice.
|
||||
// Play: https://go.dev/play/p/pFCF5WVB225
|
||||
func FlatMap[T any, R any](collection []T, iteratee func(item T, index int) []R) []R {
|
||||
func FlatMap[T, R any](collection []T, iteratee func(item T, index int) []R) []R {
|
||||
result := make([]R, 0, len(collection))
|
||||
|
||||
for i := range collection {
|
||||
@@ -84,7 +84,7 @@ func FlatMap[T any, R any](collection []T, iteratee func(item T, index int) []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/CgHYNUpOd1I
|
||||
func Reduce[T any, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R {
|
||||
func Reduce[T, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R {
|
||||
for i := range collection {
|
||||
initial = accumulator(initial, collection[i], i)
|
||||
}
|
||||
@@ -94,7 +94,7 @@ func Reduce[T any, R any](collection []T, accumulator func(agg R, item T, index
|
||||
|
||||
// ReduceRight 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(agg R, item T, index int) R, initial R) R {
|
||||
func ReduceRight[T, 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)
|
||||
}
|
||||
@@ -547,7 +547,7 @@ func Reject[T any, Slice ~[]T](collection Slice, predicate func(item T, index in
|
||||
// - whether the result element should be included or not.
|
||||
//
|
||||
// Play: https://go.dev/play/p/W9Ug9r0QFkL
|
||||
func RejectMap[T any, R any](collection []T, callback func(item T, index int) (R, bool)) []R {
|
||||
func RejectMap[T, R any](collection []T, callback func(item T, index int) (R, bool)) []R {
|
||||
result := []R{}
|
||||
|
||||
for i := range collection {
|
||||
@@ -562,7 +562,7 @@ func RejectMap[T any, R any](collection []T, callback func(item T, index int) (R
|
||||
// FilterReject mixes Filter and Reject, this method returns two slices, one for the elements of collection that
|
||||
// predicate returns true for and one for the elements that predicate does not return true for.
|
||||
// Play: https://go.dev/play/p/lHSEGSznJjB
|
||||
func FilterReject[T any, Slice ~[]T](collection Slice, predicate func(T, int) bool) (kept Slice, rejected Slice) {
|
||||
func FilterReject[T any, Slice ~[]T](collection Slice, predicate func(T, int) bool) (kept, rejected Slice) {
|
||||
kept = make(Slice, 0, len(collection))
|
||||
rejected = make(Slice, 0, len(collection))
|
||||
|
||||
@@ -655,7 +655,7 @@ func Subset[T any, Slice ~[]T](collection Slice, offset int, length uint) Slice
|
||||
|
||||
// Slice returns a copy of a slice from `start` up to, but not including `end`. Like `slice[start:end]`, but does not panic on overflow.
|
||||
// Play: https://go.dev/play/p/8XWYhfMMA1h
|
||||
func Slice[T any, Slice ~[]T](collection Slice, start int, end int) Slice {
|
||||
func Slice[T any, Slice ~[]T](collection Slice, start, end int) Slice {
|
||||
size := len(collection)
|
||||
|
||||
if start >= end {
|
||||
@@ -681,7 +681,7 @@ func Slice[T any, Slice ~[]T](collection Slice, start int, end int) Slice {
|
||||
|
||||
// Replace returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
|
||||
// Play: https://go.dev/play/p/XfPzmf9gql6
|
||||
func Replace[T comparable, Slice ~[]T](collection Slice, old T, nEw T, n int) Slice {
|
||||
func Replace[T comparable, Slice ~[]T](collection Slice, old, nEw T, n int) Slice {
|
||||
result := make(Slice, len(collection))
|
||||
copy(result, collection)
|
||||
|
||||
@@ -697,7 +697,7 @@ func Replace[T comparable, Slice ~[]T](collection Slice, old T, nEw T, n int) Sl
|
||||
|
||||
// ReplaceAll returns a copy of the slice with all non-overlapping instances of old replaced by new.
|
||||
// Play: https://go.dev/play/p/a9xZFUHfYcV
|
||||
func ReplaceAll[T comparable, Slice ~[]T](collection Slice, old T, nEw T) Slice {
|
||||
func ReplaceAll[T comparable, Slice ~[]T](collection Slice, old, nEw T) Slice {
|
||||
return Replace(collection, old, nEw, -1)
|
||||
}
|
||||
|
||||
@@ -771,7 +771,7 @@ func Splice[T any, Slice ~[]T](collection Slice, i int, elements ...T) Slice {
|
||||
// before and after separator. The found result reports whether separator appears in collection.
|
||||
// If separator does not appear in s, cut returns collection, empty slice of []T, false.
|
||||
// Play: https://go.dev/play/p/GiL3qhpIP3f
|
||||
func Cut[T comparable, Slice ~[]T](collection Slice, separator Slice) (before Slice, after Slice, found bool) {
|
||||
func Cut[T comparable, Slice ~[]T](collection, separator Slice) (before, after Slice, found bool) {
|
||||
if len(separator) == 0 {
|
||||
return make(Slice, 0), collection, true
|
||||
}
|
||||
@@ -797,7 +797,7 @@ func Cut[T comparable, Slice ~[]T](collection Slice, separator Slice) (before Sl
|
||||
// If s doesn't start with prefix, CutPrefix returns collection, false.
|
||||
// If prefix is the empty []T, CutPrefix returns collection, true.
|
||||
// Play: https://go.dev/play/p/7Plak4a1ICl
|
||||
func CutPrefix[T comparable, Slice ~[]T](collection Slice, separator Slice) (after Slice, found bool) {
|
||||
func CutPrefix[T comparable, Slice ~[]T](collection, separator Slice) (after Slice, found bool) {
|
||||
if len(separator) == 0 {
|
||||
return collection, true
|
||||
}
|
||||
@@ -818,7 +818,7 @@ func CutPrefix[T comparable, Slice ~[]T](collection Slice, separator Slice) (aft
|
||||
// whether it found the suffix. If s doesn't end with suffix, CutSuffix returns collection, false.
|
||||
// If suffix is the empty []T, CutSuffix returns collection, true.
|
||||
// Play: https://go.dev/play/p/7FKfBFvPTaT
|
||||
func CutSuffix[T comparable, Slice ~[]T](collection Slice, separator Slice) (before Slice, found bool) {
|
||||
func CutSuffix[T comparable, Slice ~[]T](collection, separator Slice) (before Slice, found bool) {
|
||||
if len(separator) == 0 {
|
||||
return collection, true
|
||||
}
|
||||
@@ -838,7 +838,7 @@ func CutSuffix[T comparable, Slice ~[]T](collection Slice, separator Slice) (bef
|
||||
|
||||
// Trim removes all the leading and trailing cutset from the collection.
|
||||
// Play: https://go.dev/play/p/1an9mxLdRG5
|
||||
func Trim[T comparable, Slice ~[]T](collection Slice, cutset Slice) Slice {
|
||||
func Trim[T comparable, Slice ~[]T](collection, cutset Slice) Slice {
|
||||
set := Keyify(cutset)
|
||||
|
||||
i := 0
|
||||
@@ -865,7 +865,7 @@ func Trim[T comparable, Slice ~[]T](collection Slice, cutset Slice) Slice {
|
||||
|
||||
// TrimLeft removes all the leading cutset from the collection.
|
||||
// Play: https://go.dev/play/p/74aqfAYLmyi
|
||||
func TrimLeft[T comparable, Slice ~[]T](collection Slice, cutset Slice) Slice {
|
||||
func TrimLeft[T comparable, Slice ~[]T](collection, cutset Slice) Slice {
|
||||
set := Keyify(cutset)
|
||||
|
||||
return DropWhile(collection, func(item T) bool {
|
||||
@@ -876,7 +876,7 @@ func TrimLeft[T comparable, Slice ~[]T](collection Slice, cutset Slice) Slice {
|
||||
|
||||
// TrimPrefix removes all the leading prefix from the collection.
|
||||
// Play: https://go.dev/play/p/SHO6X-YegPg
|
||||
func TrimPrefix[T comparable, Slice ~[]T](collection Slice, prefix Slice) Slice {
|
||||
func TrimPrefix[T comparable, Slice ~[]T](collection, prefix Slice) Slice {
|
||||
if len(prefix) == 0 {
|
||||
return collection
|
||||
}
|
||||
@@ -891,7 +891,7 @@ func TrimPrefix[T comparable, Slice ~[]T](collection Slice, prefix Slice) Slice
|
||||
|
||||
// TrimRight removes all the trailing cutset from the collection.
|
||||
// Play: https://go.dev/play/p/MRpAfR6sf0g
|
||||
func TrimRight[T comparable, Slice ~[]T](collection Slice, cutset Slice) Slice {
|
||||
func TrimRight[T comparable, Slice ~[]T](collection, cutset Slice) Slice {
|
||||
set := Keyify(cutset)
|
||||
|
||||
return DropRightWhile(collection, func(item T) bool {
|
||||
@@ -902,7 +902,7 @@ func TrimRight[T comparable, Slice ~[]T](collection Slice, cutset Slice) Slice {
|
||||
|
||||
// TrimSuffix removes all the trailing suffix from the collection.
|
||||
// Play: https://go.dev/play/p/IjEUrV0iofq
|
||||
func TrimSuffix[T comparable, Slice ~[]T](collection Slice, suffix Slice) Slice {
|
||||
func TrimSuffix[T comparable, Slice ~[]T](collection, suffix Slice) Slice {
|
||||
if len(suffix) == 0 {
|
||||
return collection
|
||||
}
|
||||
|
@@ -71,7 +71,7 @@ func ExampleFlatMap() {
|
||||
func ExampleReduce() {
|
||||
list := []int64{1, 2, 3, 4}
|
||||
|
||||
result := Reduce(list, func(agg int64, item int64, index int) int64 {
|
||||
result := Reduce(list, func(agg, item int64, index int) int64 {
|
||||
return agg + item
|
||||
}, 0)
|
||||
|
||||
@@ -82,7 +82,7 @@ func ExampleReduce() {
|
||||
func ExampleReduceRight() {
|
||||
list := [][]int{{0, 1}, {2, 3}, {4, 5}}
|
||||
|
||||
result := ReduceRight(list, func(agg []int, item []int, index int) []int {
|
||||
result := ReduceRight(list, func(agg, item []int, index int) []int {
|
||||
return append(agg, item...)
|
||||
}, []int{})
|
||||
|
||||
@@ -382,7 +382,7 @@ func ExampleDropByIndex() {
|
||||
func ExampleReject() {
|
||||
list := []int{0, 1, 2, 3, 4, 5}
|
||||
|
||||
result := Reject(list, func(x int, _ int) bool {
|
||||
result := Reject(list, func(x, _ int) bool {
|
||||
return x%2 == 0
|
||||
})
|
||||
|
||||
|
@@ -14,7 +14,7 @@ func TestFilter(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
r1 := Filter([]int{1, 2, 3, 4}, func(x int, _ int) bool {
|
||||
r1 := Filter([]int{1, 2, 3, 4}, func(x, _ int) bool {
|
||||
return x%2 == 0
|
||||
})
|
||||
is.Equal([]int{2, 4}, r1)
|
||||
@@ -36,7 +36,7 @@ func TestMap(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := Map([]int{1, 2, 3, 4}, func(x int, _ int) string {
|
||||
result1 := Map([]int{1, 2, 3, 4}, func(x, _ int) string {
|
||||
return "Hello"
|
||||
})
|
||||
result2 := Map([]int64{1, 2, 3, 4}, func(x int64, _ int) string {
|
||||
@@ -89,7 +89,7 @@ func TestFlatMap(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := FlatMap([]int{0, 1, 2, 3, 4}, func(x int, _ int) []string {
|
||||
result1 := FlatMap([]int{0, 1, 2, 3, 4}, func(x, _ int) []string {
|
||||
return []string{"Hello"}
|
||||
})
|
||||
result2 := FlatMap([]int64{0, 1, 2, 3, 4}, func(x int64, _ int) []string {
|
||||
@@ -118,10 +118,10 @@ func TestReduce(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := Reduce([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int {
|
||||
result1 := Reduce([]int{1, 2, 3, 4}, func(agg, item, _ int) int {
|
||||
return agg + item
|
||||
}, 0)
|
||||
result2 := Reduce([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int {
|
||||
result2 := Reduce([]int{1, 2, 3, 4}, func(agg, item, _ int) int {
|
||||
return agg + item
|
||||
}, 10)
|
||||
|
||||
@@ -133,14 +133,14 @@ func TestReduceRight(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
result1 := ReduceRight([][]int{{0, 1}, {2, 3}, {4, 5}}, func(agg []int, item []int, _ int) []int {
|
||||
result1 := ReduceRight([][]int{{0, 1}, {2, 3}, {4, 5}}, func(agg, item []int, _ int) []int {
|
||||
return append(agg, item...)
|
||||
}, []int{})
|
||||
|
||||
is.Equal([]int{4, 5, 2, 3, 0, 1}, result1)
|
||||
|
||||
type collection []int
|
||||
result3 := ReduceRight(collection{1, 2, 3, 4}, func(agg int, item int, _ int) int {
|
||||
result3 := ReduceRight(collection{1, 2, 3, 4}, func(agg, item, _ int) int {
|
||||
return agg + item
|
||||
}, 10)
|
||||
is.Equal(20, result3)
|
||||
@@ -738,7 +738,7 @@ func TestReject(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
r1 := Reject([]int{1, 2, 3, 4}, func(x int, _ int) bool {
|
||||
r1 := Reject([]int{1, 2, 3, 4}, func(x, _ int) bool {
|
||||
return x%2 == 0
|
||||
})
|
||||
|
||||
@@ -783,7 +783,7 @@ func TestFilterReject(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
left1, right1 := FilterReject([]int{1, 2, 3, 4}, func(x int, _ int) bool {
|
||||
left1, right1 := FilterReject([]int{1, 2, 3, 4}, func(x, _ int) bool {
|
||||
return x%2 == 0
|
||||
})
|
||||
|
||||
|
32
tuples.go
32
tuples.go
@@ -332,7 +332,7 @@ func Zip9[A, B, C, D, E, F, G, H, I any](a []A, b []B, c []C, d []D, e []E, f []
|
||||
// of the given slices, the second of which contains the second elements of the given slices, and so on.
|
||||
// When collections are different sizes, the Tuple attributes are filled with zero value.
|
||||
// Play: https://go.dev/play/p/wlHur6yO8rR
|
||||
func ZipBy2[A any, B any, Out any](a []A, b []B, iteratee func(a A, b B) Out) []Out {
|
||||
func ZipBy2[A, B, Out any](a []A, b []B, iteratee func(a A, b B) Out) []Out {
|
||||
size := Max([]int{len(a), len(b)})
|
||||
|
||||
result := make([]Out, 0, size)
|
||||
@@ -351,7 +351,7 @@ func ZipBy2[A any, B any, Out any](a []A, b []B, iteratee func(a A, b B) Out) []
|
||||
// of the given slices, the second of which contains the second elements of the given slices, and so on.
|
||||
// When collections are different sizes, the Tuple attributes are filled with zero value.
|
||||
// Play: https://go.dev/play/p/j9maveOnSQX
|
||||
func ZipBy3[A any, B any, C any, Out any](a []A, b []B, c []C, iteratee func(a A, b B, c C) Out) []Out {
|
||||
func ZipBy3[A, B, C, Out any](a []A, b []B, c []C, iteratee func(a A, b B, c C) Out) []Out {
|
||||
size := Max([]int{len(a), len(b), len(c)})
|
||||
|
||||
result := make([]Out, 0, size)
|
||||
@@ -371,7 +371,7 @@ func ZipBy3[A any, B any, C any, Out any](a []A, b []B, c []C, iteratee func(a A
|
||||
// of the given slices, the second of which contains the second elements of the given slices, and so on.
|
||||
// When collections are different sizes, the Tuple attributes are filled with zero value.
|
||||
// Play: https://go.dev/play/p/Y1eF2Ke0Ayz
|
||||
func ZipBy4[A any, B any, C any, D any, Out any](a []A, b []B, c []C, d []D, iteratee func(a A, b B, c C, d D) Out) []Out {
|
||||
func ZipBy4[A, B, C, D, Out any](a []A, b []B, c []C, d []D, iteratee func(a A, b B, c C, d D) Out) []Out {
|
||||
size := Max([]int{len(a), len(b), len(c), len(d)})
|
||||
|
||||
result := make([]Out, 0, size)
|
||||
@@ -392,7 +392,7 @@ func ZipBy4[A any, B any, C any, D any, Out any](a []A, b []B, c []C, d []D, ite
|
||||
// of the given slices, the second of which contains the second elements of the given slices, and so on.
|
||||
// When collections are different sizes, the Tuple attributes are filled with zero value.
|
||||
// Play: https://go.dev/play/p/SLynyalh5Oa
|
||||
func ZipBy5[A any, B any, C any, D any, E any, Out any](a []A, b []B, c []C, d []D, e []E, iteratee func(a A, b B, c C, d D, e E) Out) []Out {
|
||||
func ZipBy5[A, B, C, D, E, Out any](a []A, b []B, c []C, d []D, e []E, iteratee func(a A, b B, c C, d D, e E) Out) []Out {
|
||||
size := Max([]int{len(a), len(b), len(c), len(d), len(e)})
|
||||
|
||||
result := make([]Out, 0, size)
|
||||
@@ -414,7 +414,7 @@ func ZipBy5[A any, B any, C any, D any, E any, Out any](a []A, b []B, c []C, d [
|
||||
// of the given slices, the second of which contains the second elements of the given slices, and so on.
|
||||
// When collections are different sizes, the Tuple attributes are filled with zero value.
|
||||
// Play: https://go.dev/play/p/IK6KVgw9e-S
|
||||
func ZipBy6[A any, B any, C any, D any, E any, F any, Out any](a []A, b []B, c []C, d []D, e []E, f []F, iteratee func(a A, b B, c C, d D, e E, f F) Out) []Out {
|
||||
func ZipBy6[A, B, C, D, E, F, Out any](a []A, b []B, c []C, d []D, e []E, f []F, iteratee func(a A, b B, c C, d D, e E, f F) Out) []Out {
|
||||
size := Max([]int{len(a), len(b), len(c), len(d), len(e), len(f)})
|
||||
|
||||
result := make([]Out, 0, size)
|
||||
@@ -437,7 +437,7 @@ func ZipBy6[A any, B any, C any, D any, E any, F any, Out any](a []A, b []B, c [
|
||||
// of the given slices, the second of which contains the second elements of the given slices, and so on.
|
||||
// When collections are different sizes, the Tuple attributes are filled with zero value.
|
||||
// Play: https://go.dev/play/p/4uW6a2vXh8w
|
||||
func ZipBy7[A any, B any, C any, D any, E any, F any, G any, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, iteratee func(a A, b B, c C, d D, e E, f F, g G) Out) []Out {
|
||||
func ZipBy7[A, B, C, D, E, F, G, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, iteratee func(a A, b B, c C, d D, e E, f F, g G) Out) []Out {
|
||||
size := Max([]int{len(a), len(b), len(c), len(d), len(e), len(f), len(g)})
|
||||
|
||||
result := make([]Out, 0, size)
|
||||
@@ -461,7 +461,7 @@ func ZipBy7[A any, B any, C any, D any, E any, F any, G any, Out any](a []A, b [
|
||||
// of the given slices, the second of which contains the second elements of the given slices, and so on.
|
||||
// When collections are different sizes, the Tuple attributes are filled with zero value.
|
||||
// Play: https://go.dev/play/p/tk8xW7XzY4v
|
||||
func ZipBy8[A any, B any, C any, D any, E any, F any, G any, H any, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, iteratee func(a A, b B, c C, d D, e E, f F, g G, h H) Out) []Out {
|
||||
func ZipBy8[A, B, C, D, E, F, G, H, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, iteratee func(a A, b B, c C, d D, e E, f F, g G, h H) Out) []Out {
|
||||
size := Max([]int{len(a), len(b), len(c), len(d), len(e), len(f), len(g), len(h)})
|
||||
|
||||
result := make([]Out, 0, size)
|
||||
@@ -486,7 +486,7 @@ func ZipBy8[A any, B any, C any, D any, E any, F any, G any, H any, Out any](a [
|
||||
// of the given slices, the second of which contains the second elements of the given slices, and so on.
|
||||
// When collections are different sizes, the Tuple attributes are filled with zero value.
|
||||
// Play: https://go.dev/play/p/VGqjDmQ9YqX
|
||||
func ZipBy9[A any, B any, C any, D any, E any, F any, G any, H any, I any, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, i []I, iteratee func(a A, b B, c C, d D, e E, f F, g G, h H, i I) Out) []Out {
|
||||
func ZipBy9[A, B, C, D, E, F, G, H, I, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H, i []I, iteratee func(a A, b B, c C, d D, e E, f F, g G, h H, i I) Out) []Out {
|
||||
size := Max([]int{len(a), len(b), len(c), len(d), len(e), len(f), len(g), len(h), len(i)})
|
||||
|
||||
result := make([]Out, 0, size)
|
||||
@@ -695,7 +695,7 @@ func Unzip9[A, B, C, D, E, F, G, H, I any](tuples []Tuple9[A, B, C, D, E, F, G,
|
||||
// UnzipBy2 iterates over a collection and creates a slice regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
// Play: https://go.dev/play/p/tN8yqaRZz0r
|
||||
func UnzipBy2[In any, A any, B any](items []In, iteratee func(In) (a A, b B)) ([]A, []B) {
|
||||
func UnzipBy2[In, A, B any](items []In, iteratee func(In) (a A, b B)) ([]A, []B) {
|
||||
size := len(items)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
@@ -712,7 +712,7 @@ func UnzipBy2[In any, A any, B any](items []In, iteratee func(In) (a A, b B)) ([
|
||||
// UnzipBy3 iterates over a collection and creates a slice regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
// Play: https://go.dev/play/p/36ITO2DlQq1
|
||||
func UnzipBy3[In any, A any, B any, C any](items []In, iteratee func(In) (a A, b B, c C)) ([]A, []B, []C) {
|
||||
func UnzipBy3[In, A, B, C any](items []In, iteratee func(In) (a A, b B, c C)) ([]A, []B, []C) {
|
||||
size := len(items)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
@@ -731,7 +731,7 @@ func UnzipBy3[In any, A any, B any, C any](items []In, iteratee func(In) (a A, b
|
||||
// UnzipBy4 iterates over a collection and creates a slice regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
// Play: https://go.dev/play/p/zJ6qY1dD1rL
|
||||
func UnzipBy4[In any, A any, B any, C any, D any](items []In, iteratee func(In) (a A, b B, c C, d D)) ([]A, []B, []C, []D) {
|
||||
func UnzipBy4[In, A, B, C, D any](items []In, iteratee func(In) (a A, b B, c C, d D)) ([]A, []B, []C, []D) {
|
||||
size := len(items)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
@@ -752,7 +752,7 @@ func UnzipBy4[In any, A any, B any, C any, D any](items []In, iteratee func(In)
|
||||
// UnzipBy5 iterates over a collection and creates a slice regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
// Play: https://go.dev/play/p/3f7jKkV9xZt
|
||||
func UnzipBy5[In any, A any, B any, C any, D any, E any](items []In, iteratee func(In) (a A, b B, c C, d D, e E)) ([]A, []B, []C, []D, []E) {
|
||||
func UnzipBy5[In, A, B, C, D, E any](items []In, iteratee func(In) (a A, b B, c C, d D, e E)) ([]A, []B, []C, []D, []E) {
|
||||
size := len(items)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
@@ -775,7 +775,7 @@ func UnzipBy5[In any, A any, B any, C any, D any, E any](items []In, iteratee fu
|
||||
// UnzipBy6 iterates over a collection and creates a slice regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
// Play: https://go.dev/play/p/8Y1b7tKu2pL
|
||||
func UnzipBy6[In any, A any, B any, C any, D any, E any, F any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, f F)) ([]A, []B, []C, []D, []E, []F) {
|
||||
func UnzipBy6[In, A, B, C, D, E, F any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, f F)) ([]A, []B, []C, []D, []E, []F) {
|
||||
size := len(items)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
@@ -800,7 +800,7 @@ func UnzipBy6[In any, A any, B any, C any, D any, E any, F any](items []In, iter
|
||||
// UnzipBy7 iterates over a collection and creates a slice regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
// Play: https://go.dev/play/p/7j1kLmVn3pM
|
||||
func UnzipBy7[In any, A any, B any, C any, D any, E any, F any, G any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, f F, g G)) ([]A, []B, []C, []D, []E, []F, []G) {
|
||||
func UnzipBy7[In, A, B, C, D, E, F, G any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, f F, g G)) ([]A, []B, []C, []D, []E, []F, []G) {
|
||||
size := len(items)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
@@ -827,7 +827,7 @@ func UnzipBy7[In any, A any, B any, C any, D any, E any, F any, G any](items []I
|
||||
// UnzipBy8 iterates over a collection and creates a slice regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
// Play: https://go.dev/play/p/1n2k3L4m5N6
|
||||
func UnzipBy8[In any, A any, B any, C any, D any, E any, F any, G any, H any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, f F, g G, h H)) ([]A, []B, []C, []D, []E, []F, []G, []H) {
|
||||
func UnzipBy8[In, A, B, C, D, E, F, G, H any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, f F, g G, h H)) ([]A, []B, []C, []D, []E, []F, []G, []H) {
|
||||
size := len(items)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
@@ -856,7 +856,7 @@ func UnzipBy8[In any, A any, B any, C any, D any, E any, F any, G any, H any](it
|
||||
// UnzipBy9 iterates over a collection and creates a slice regrouping the elements
|
||||
// to their pre-zip configuration.
|
||||
// Play: https://go.dev/play/p/7o8p9q0r1s2
|
||||
func UnzipBy9[In any, A any, B any, C any, D any, E any, F any, G any, H any, I any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, f F, g G, h H, i I)) ([]A, []B, []C, []D, []E, []F, []G, []H, []I) {
|
||||
func UnzipBy9[In, A, B, C, D, E, F, G, H, I any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, f F, g G, h H, i I)) ([]A, []B, []C, []D, []E, []F, []G, []H, []I) {
|
||||
size := len(items)
|
||||
r1 := make([]A, 0, size)
|
||||
r2 := make([]B, 0, size)
|
||||
|
Reference in New Issue
Block a user