lo package - github.com/samber/lo - Go Packages
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
result2 := If(false, 1).
ElseIf(true, 2).
Else(3)
result3 := If(false, 1).
ElseIf(false, 2).
Else(3)
result4 := IfF(true, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
result5 := IfF(false, func() int { return 1 }).
ElseIfF(true, func() int { return 2 }).
ElseF(func() int { return 3 })
result6 := IfF(false, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output: 1 2 3 1 2 3
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
result2 := If(false, 1).
ElseIf(true, 2).
Else(3)
result3 := If(false, 1).
ElseIf(false, 2).
Else(3)
result4 := IfF(true, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
result5 := IfF(false, func() int { return 1 }).
ElseIfF(true, func() int { return 2 }).
ElseF(func() int { return 3 })
result6 := IfF(false, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output: 1 2 3 1 2 3
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
result2 := If(false, 1).
ElseIf(true, 2).
Else(3)
result3 := If(false, 1).
ElseIf(false, 2).
Else(3)
result4 := IfF(true, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
result5 := IfF(false, func() int { return 1 }).
ElseIfF(true, func() int { return 2 }).
ElseF(func() int { return 3 })
result6 := IfF(false, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output: 1 2 3 1 2 3
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
result2 := If(false, 1).
ElseIf(true, 2).
Else(3)
result3 := If(false, 1).
ElseIf(false, 2).
Else(3)
result4 := IfF(true, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
result5 := IfF(false, func() int { return 1 }).
ElseIfF(true, func() int { return 2 }).
ElseF(func() int { return 3 })
result6 := IfF(false, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output: 1 2 3 1 2 3
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
Default("3")
result2 := Switch[int, string](2).
Case(1, "1").
Case(2, "2").
Default("3")
result3 := Switch[int, string](42).
Case(1, "1").
Case(2, "2").
Default("3")
result4 := Switch[int, string](1).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result5 := Switch[int, string](2).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result6 := Switch[int, string](42).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output: 1 2 3 1 2 3
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
Default("3")
result2 := Switch[int, string](2).
Case(1, "1").
Case(2, "2").
Default("3")
result3 := Switch[int, string](42).
Case(1, "1").
Case(2, "2").
Default("3")
result4 := Switch[int, string](1).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result5 := Switch[int, string](2).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result6 := Switch[int, string](42).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output: 1 2 3 1 2 3
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
Default("3")
result2 := Switch[int, string](2).
Case(1, "1").
Case(2, "2").
Default("3")
result3 := Switch[int, string](42).
Case(1, "1").
Case(2, "2").
Default("3")
result4 := Switch[int, string](1).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result5 := Switch[int, string](2).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result6 := Switch[int, string](42).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output: 1 2 3 1 2 3
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
Default("3")
result2 := Switch[int, string](2).
Case(1, "1").
Case(2, "2").
Default("3")
result3 := Switch[int, string](42).
Case(1, "1").
Case(2, "2").
Default("3")
result4 := Switch[int, string](1).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result5 := Switch[int, string](2).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result6 := Switch[int, string](42).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output: 1 2 3 1 2 3
- Variables
- func Assign[K comparable, V any, Map ~map[K]V](maps ...Map) Map
- func Associate[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V
- func AssociateI[T any, K comparable, V any](collection []T, transform func(item T, index int) (K, V)) map[K]V
- func Async[A any](f func() A) <-chan A
- func Async0(f func()) <-chan struct{}
- func Async1[A any](f func() A) <-chan A
- func Async2[A, B any](f func() (A, B)) <-chan Tuple2[A, B]
- func Async3[A, B, C any](f func() (A, B, C)) <-chan Tuple3[A, B, C]
- func Async4[A, B, C, D any](f func() (A, B, C, D)) <-chan Tuple4[A, B, C, D]
- func Async5[A, B, C, D, E any](f func() (A, B, C, D, E)) <-chan Tuple5[A, B, C, D, E]
- func Async6[A, B, C, D, E, F any](f func() (A, B, C, D, E, F)) <-chan Tuple6[A, B, C, D, E, F]
- func Attempt(maxIteration int, f func(index int) error) (int, error)
- func AttemptWhile(maxIteration int, f func(int) (error, bool)) (int, error)
- func AttemptWhileWithDelay(maxIteration int, delay time.Duration, ...) (int, time.Duration, error)
- func AttemptWithDelay(maxIteration int, delay time.Duration, ...) (int, time.Duration, error)
- func Buffer[T any](ch <-chan T, size int) (collection []T, length int, readTime time.Duration, ok bool)
- func BufferWithContext[T any](ctx context.Context, ch <-chan T, size int) (collection []T, length int, readTime time.Duration, ok bool)
- func BufferWithTimeout[T any](ch <-chan T, size int, timeout time.Duration) (collection []T, length int, readTime time.Duration, ok bool)
- func CamelCase(str string) string
- func Capitalize(str string) string
- func ChannelDispatcher[T any](stream <-chan T, count, channelBufferCap int, strategy DispatchingStrategy[T]) []<-chan T
- func ChannelToSlice[T any](ch <-chan T) []T
- func Chunk[T any, Slice ~[]T](collection Slice, size int) []Slice
- func ChunkEntries[K comparable, V any](m map[K]V, size int) []map[K]V
- func ChunkString[T ~string](str T, size int) []T
- func Clamp[T constraints.Ordered](value, mIn, mAx T) T
- func Clone[T any, Slice ~[]T](collection Slice) Slice
- func Coalesce[T comparable](values ...T) (T, bool)
- func CoalesceMap[K comparable, V any](v ...map[K]V) (map[K]V, bool)
- func CoalesceMapOrEmpty[K comparable, V any](v ...map[K]V) map[K]V
- func CoalesceOrEmpty[T comparable](v ...T) T
- func CoalesceSlice[T any](v ...[]T) ([]T, bool)
- func CoalesceSliceOrEmpty[T any](v ...[]T) []T
- func Compact[T comparable, Slice ~[]T](collection Slice) Slice
- func Concat[T any, Slice ~[]T](collections ...Slice) Slice
- func Contains[T comparable](collection []T, element T) bool
- func ContainsBy[T any](collection []T, predicate func(item T) bool) bool
- func Count[T comparable](collection []T, value T) int
- func CountBy[T any](collection []T, predicate func(item T) bool) int
- func CountByErr[T any](collection []T, predicate func(item T) (bool, error)) (int, error)
- func CountValues[T comparable](collection []T) map[T]int
- func CountValuesBy[T any, U comparable](collection []T, transform func(item T) U) map[U]int
- func CrossJoinBy2[A, B, Out any](listA []A, listB []B, transform func(a A, b B) Out) []Out
- func CrossJoinBy3[A, B, C, Out any](listA []A, listB []B, listC []C, transform func(a A, b B, c C) Out) []Out
- func CrossJoinBy4[A, B, C, D, Out any](listA []A, listB []B, listC []C, listD []D, ...) []Out
- func CrossJoinBy5[A, B, C, D, E, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, ...) []Out
- func CrossJoinBy6[A, B, C, D, E, F, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, ...) []Out
- func CrossJoinBy7[A, B, C, D, E, F, G, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, ...) []Out
- func CrossJoinBy8[A, B, C, D, E, F, G, H, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, ...) []Out
- func CrossJoinBy9[A, B, C, D, E, F, G, H, I, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, ...) []Out
- func CrossJoinByErr2[A, B, Out any](listA []A, listB []B, transform func(a A, b B) (Out, error)) ([]Out, error)
- func CrossJoinByErr3[A, B, C, Out any](listA []A, listB []B, listC []C, transform func(a A, b B, c C) (Out, error)) ([]Out, error)
- func CrossJoinByErr4[A, B, C, D, Out any](listA []A, listB []B, listC []C, listD []D, ...) ([]Out, error)
- func CrossJoinByErr5[A, B, C, D, E, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, ...) ([]Out, error)
- func CrossJoinByErr6[A, B, C, D, E, F, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, ...) ([]Out, error)
- func CrossJoinByErr7[A, B, C, D, E, F, G, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, ...) ([]Out, error)
- func CrossJoinByErr8[A, B, C, D, E, F, G, H, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, ...) ([]Out, error)
- func CrossJoinByErr9[A, B, C, D, E, F, G, H, I, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, ...) ([]Out, error)
- func Cut[T comparable, Slice ~[]T](collection, separator Slice) (before, after Slice, found bool)
- func CutPrefix[T comparable, Slice ~[]T](collection, separator Slice) (after Slice, found bool)
- func CutSuffix[T comparable, Slice ~[]T](collection, separator Slice) (before Slice, found bool)
- func Difference[T comparable, Slice ~[]T](list1, list2 Slice) (Slice, Slice)
- func DispatchingStrategyFirst[T any](msg T, index uint64, channels []<-chan T) int
- func DispatchingStrategyLeast[T any](msg T, index uint64, channels []<-chan T) int
- func DispatchingStrategyMost[T any](msg T, index uint64, channels []<-chan T) int
- func DispatchingStrategyRandom[T any](msg T, index uint64, channels []<-chan T) int
- func DispatchingStrategyRoundRobin[T any](msg T, index uint64, channels []<-chan T) int
- func Drop[T any, Slice ~[]T](collection Slice, n int) Slice
- func DropByIndex[T any, Slice ~[]T](collection Slice, indexes ...int) Slice
- func DropRight[T any, Slice ~[]T](collection Slice, n int) Slice
- func DropRightWhile[T any, Slice ~[]T](collection Slice, predicate func(item T) bool) Slice
- func DropWhile[T any, Slice ~[]T](collection Slice, predicate func(item T) bool) Slice
- func Duration(callback func()) time.Duration
- func Duration0(callback func()) time.Duration
- func Duration1[A any](callback func() A) (A, time.Duration)
- func Duration2[A, B any](callback func() (A, B)) (A, B, time.Duration)
- func Duration3[A, B, C any](callback func() (A, B, C)) (A, B, C, time.Duration)
- func Duration4[A, B, C, D any](callback func() (A, B, C, D)) (A, B, C, D, time.Duration)
- func Duration5[A, B, C, D, E any](callback func() (A, B, C, D, E)) (A, B, C, D, E, time.Duration)
- func Duration6[A, B, C, D, E, F any](callback func() (A, B, C, D, E, F)) (A, B, C, D, E, F, time.Duration)
- func Duration7[A, B, C, D, E, F, G any](callback func() (A, B, C, D, E, F, G)) (A, B, C, D, E, F, G, time.Duration)
- func Duration8[A, B, C, D, E, F, G, H any](callback func() (A, B, C, D, E, F, G, H)) (A, B, C, D, E, F, G, H, time.Duration)
- func Duration9[A, B, C, D, E, F, G, H, I any](callback func() (A, B, C, D, E, F, G, H, I)) (A, B, C, D, E, F, G, H, I, time.Duration)
- func Duration10[A, B, C, D, E, F, G, H, I, J any](callback func() (A, B, C, D, E, F, G, H, I, J)) (A, B, C, D, E, F, G, H, I, J, time.Duration)
- func Earliest(times ...time.Time) time.Time
- func EarliestBy[T any](collection []T, iteratee func(item T) time.Time) T
- func EarliestByErr[T any](collection []T, iteratee func(item T) (time.Time, error)) (T, error)
- func ElementsMatch[T comparable, Slice ~[]T](list1, list2 Slice) bool
- func ElementsMatchBy[T any, K comparable](list1, list2 []T, iteratee func(item T) K) bool
- func Ellipsis(str string, length int) string
- func Empty[T any]() T
- func EmptyableToPtr[T any](x T) *T
- func ErrorsAs[T error](err error) (T, bool)
- func Every[T comparable](collection, subset []T) bool
- func EveryBy[T any](collection []T, predicate func(item T) bool) bool
- func FanIn[T any](channelBufferCap int, upstreams ...<-chan T) <-chan T
- func FanOut[T any](count, channelsBufferCap int, upstream <-chan T) []<-chan T
- func Fill[T Clonable[T], Slice ~[]T](collection Slice, initial T) Slice
- func Filter[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) bool) Slice
- func FilterErr[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) (bool, error)) (Slice, error)
- func FilterKeys[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) []K
- func FilterKeysErr[K comparable, V any](in map[K]V, predicate func(key K, value V) (bool, error)) ([]K, error)
- func FilterMap[T, R any](collection []T, callback func(item T, index int) (R, bool)) []R
- func FilterMapToSlice[K comparable, V, R any](in map[K]V, iteratee func(key K, value V) (R, bool)) []R
- func FilterMapToSliceErr[K comparable, V, R any](in map[K]V, iteratee func(key K, value V) (R, bool, error)) ([]R, error)
- func FilterReject[T any, Slice ~[]T](collection Slice, predicate func(T, int) bool) (kept, rejected Slice)
- func FilterSliceToMap[T any, K comparable, V any](collection []T, transform func(item T) (K, V, bool)) map[K]V
- func FilterSliceToMapI[T any, K comparable, V any](collection []T, transform func(item T, index int) (K, V, bool)) map[K]V
- func FilterValues[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) []V
- func FilterValuesErr[K comparable, V any](in map[K]V, predicate func(key K, value V) (bool, error)) ([]V, error)
- func Find[T any](collection []T, predicate func(item T) bool) (T, bool)
- func FindDuplicates[T comparable, Slice ~[]T](collection Slice) Slice
- func FindDuplicatesBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) Slice
- func FindDuplicatesByErr[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) (U, error)) (Slice, error)
- func FindErr[T any](collection []T, predicate func(item T) (bool, error)) (T, error)
- func FindIndexOf[T any](collection []T, predicate func(item T) bool) (T, int, bool)
- func FindKey[K, V comparable](object map[K]V, value V) (K, bool)
- func FindKeyBy[K comparable, V any](object map[K]V, predicate func(key K, value V) bool) (K, bool)
- func FindLastIndexOf[T any](collection []T, predicate func(item T) bool) (T, int, bool)
- func FindOrElse[T any](collection []T, fallback T, predicate func(item T) bool) T
- func FindUniques[T comparable, Slice ~[]T](collection Slice) Slice
- func FindUniquesBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) Slice
- func First[T any](collection []T) (T, bool)
- func FirstOr[T any](collection []T, fallback T) T
- func FirstOrEmpty[T any](collection []T) T
- func FlatMap[T, R any](collection []T, transform func(item T, index int) []R) []R
- func FlatMapErr[T, R any](collection []T, transform func(item T, index int) ([]R, error)) ([]R, error)
- func Flatten[T any, Slice ~[]T](collection []Slice) Slice
- func ForEach[T any](collection []T, callback func(item T, index int))
- func ForEachWhile[T any](collection []T, predicate func(item T, index int) bool)
- func FromAnySlice[T any](in []any) ([]T, bool)
- func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V
- func FromPairs[K comparable, V any](entries []Entry[K, V]) map[K]V
- func FromPtr[T any](x *T) T
- func FromPtrOr[T any](x *T, fallback T) T
- func FromSlicePtr[T any](collection []*T) []T
- func FromSlicePtrOr[T any](collection []*T, fallback T) []T
- func Generator[T any](bufferSize int, generator func(yield func(T))) <-chan Tdeprecated
- func GroupBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) map[U]Slice
- func GroupByErr[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) (U, error)) (map[U]Slice, error)
- func GroupByMap[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K][]V
- func GroupByMapErr[T any, K comparable, V any](collection []T, transform func(item T) (K, V, error)) (map[K][]V, error)
- func HasKey[K comparable, V any](in map[K]V, key K) bool
- func HasPrefix[T comparable](collection, prefix []T) bool
- func HasSuffix[T comparable](collection, suffix []T) bool
- func If[T any](condition bool, result T) *ifElse[T]
- func IfF[T any](condition bool, resultF func() T) *ifElse[T]
- func IndexOf[T comparable](collection []T, element T) int
- func Interleave[T any, Slice ~[]T](collections ...Slice) Slice
- func Intersect[T comparable, Slice ~[]T](lists ...Slice) Slice
- func IntersectBy[T any, K comparable, Slice ~[]T](transform func(T) K, lists ...Slice) Slice
- func Invert[K, V comparable](in map[K]V) map[V]K
- func IsEmpty[T comparable](v T) bool
- func IsNil(x any) bool
- func IsNotEmpty[T comparable](v T) bool
- func IsNotNil(x any) bool
- func IsSorted[T constraints.Ordered](collection []T) bool
- func IsSortedBy[T any, K constraints.Ordered](collection []T, iteratee func(item T) K) bool
- func IsSortedByKey[T any, K constraints.Ordered](collection []T, iteratee func(item T) K) booldeprecated
- func KebabCase(str string) string
- func KeyBy[K comparable, V any](collection []V, iteratee func(item V) K) map[K]V
- func KeyByErr[K comparable, V any](collection []V, iteratee func(item V) (K, error)) (map[K]V, error)
- func Keyify[T comparable, Slice ~[]T](collection Slice) map[T]struct{}
- func Keys[K comparable, V any](in ...map[K]V) []K
- func Last[T any](collection []T) (T, bool)
- func LastIndexOf[T comparable](collection []T, element T) int
- func LastOr[T any](collection []T, fallback T) T
- func LastOrEmpty[T any](collection []T) T
- func Latest(times ...time.Time) time.Time
- func LatestBy[T any](collection []T, iteratee func(item T) time.Time) T
- func LatestByErr[T any](collection []T, iteratee func(item T) (time.Time, error)) (T, error)
- func Map[T, R any](collection []T, transform func(item T, index int) R) []R
- 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
- func MapEntriesErr[K1 comparable, V1 any, K2 comparable, V2 any](in map[K1]V1, iteratee func(key K1, value V1) (K2, V2, error)) (map[K2]V2, error)
- func MapErr[T, R any](collection []T, transform func(item T, index int) (R, error)) ([]R, error)
- func MapKeys[K comparable, V any, R comparable](in map[K]V, iteratee func(value V, key K) R) map[R]V
- func MapKeysErr[K comparable, V any, R comparable](in map[K]V, iteratee func(value V, key K) (R, error)) (map[R]V, error)
- func MapToSlice[K comparable, V, R any](in map[K]V, iteratee func(key K, value V) R) []R
- func MapToSliceErr[K comparable, V, R any](in map[K]V, iteratee func(key K, value V) (R, error)) ([]R, error)
- func MapValues[K comparable, V, R any](in map[K]V, iteratee func(value V, key K) R) map[K]R
- func MapValuesErr[K comparable, V, R any](in map[K]V, iteratee func(value V, key K) (R, error)) (map[K]R, error)
- func Max[T constraints.Ordered](collection []T) T
- func MaxBy[T any](collection []T, greater func(a, b T) bool) T
- func MaxByErr[T any](collection []T, greater func(a, b T) (bool, error)) (T, error)
- func MaxIndex[T constraints.Ordered](collection []T) (T, int)
- func MaxIndexBy[T any](collection []T, greater func(a, b T) bool) (T, int)
- func MaxIndexByErr[T any](collection []T, greater func(a, b T) (bool, error)) (T, int, error)
- func Mean[T constraints.Float | constraints.Integer](collection []T) T
- func MeanBy[T any, R constraints.Float | constraints.Integer](collection []T, iteratee func(item T) R) R
- func MeanByErr[T any, R constraints.Float | constraints.Integer](collection []T, iteratee func(item T) (R, error)) (R, error)
- func Min[T constraints.Ordered](collection []T) T
- func MinBy[T any](collection []T, less func(a, b T) bool) T
- func MinByErr[T any](collection []T, less func(a, b T) (bool, error)) (T, error)
- func MinIndex[T constraints.Ordered](collection []T) (T, int)
- func MinIndexBy[T any](collection []T, less func(a, b T) bool) (T, int)
- func MinIndexByErr[T any](collection []T, less func(a, b T) (bool, error)) (T, int, error)
- func Mode[T constraints.Integer | constraints.Float](collection []T) []T
- func Must[T any](val T, err any, messageArgs ...any) T
- func Must0(err any, messageArgs ...any)
- func Must1[T any](val T, err any, messageArgs ...any) T
- func Must2[T1, T2 any](val1 T1, val2 T2, err any, messageArgs ...any) (T1, T2)
- func Must3[T1, T2, T3 any](val1 T1, val2 T2, val3 T3, err any, messageArgs ...any) (T1, T2, T3)
- func Must4[T1, T2, T3, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err any, messageArgs ...any) (T1, T2, T3, T4)
- func Must5[T1, T2, T3, T4, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, err any, messageArgs ...any) (T1, T2, T3, T4, T5)
- func Must6[T1, T2, T3, T4, T5, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, val6 T6, err any, ...) (T1, T2, T3, T4, T5, T6)
- func NewDebounce(duration time.Duration, f ...func()) (func(), func())
- func NewDebounceBy[T comparable](duration time.Duration, f ...func(key T, count int)) (func(key T), func(key T))
- func NewThrottle(interval time.Duration, f ...func()) (throttle, reset func())
- func NewThrottleBy[T comparable](interval time.Duration, f ...func(key T)) (throttle func(key T), reset func())
- func NewThrottleByWithCount[T comparable](interval time.Duration, count int, f ...func(key T)) (throttle func(key T), reset func())
- func NewThrottleWithCount(interval time.Duration, count int, f ...func()) (throttle, reset func())
- func Nil[T any]() *T
- func None[T comparable](collection, subset []T) bool
- func NoneBy[T any](collection []T, predicate func(item T) bool) bool
- func Nth[T any, N constraints.Integer](collection []T, nth N) (T, error)
- func NthOr[T any, N constraints.Integer](collection []T, nth N, fallback T) T
- func NthOrEmpty[T any, N constraints.Integer](collection []T, nth N) T
- func OmitBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map
- func OmitByErr[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) (bool, error)) (Map, error)
- func OmitByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map
- func OmitByValues[K, V comparable, Map ~map[K]V](in Map, values []V) Map
- func Partial[T1, T2, R any](f func(a T1, b T2) R, arg1 T1) func(T2) R
- func Partial1[T1, T2, R any](f func(T1, T2) R, arg1 T1) func(T2) R
- func Partial2[T1, T2, T3, R any](f func(T1, T2, T3) R, arg1 T1) func(T2, T3) R
- func Partial3[T1, T2, T3, T4, R any](f func(T1, T2, T3, T4) R, arg1 T1) func(T2, T3, T4) R
- func Partial4[T1, T2, T3, T4, T5, R any](f func(T1, T2, T3, T4, T5) R, arg1 T1) func(T2, T3, T4, T5) R
- func Partial5[T1, T2, T3, T4, T5, T6, R any](f func(T1, T2, T3, T4, T5, T6) R, arg1 T1) func(T2, T3, T4, T5, T6) R
- func PartitionBy[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) K) []Slice
- func PartitionByErr[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) (K, error)) ([]Slice, error)
- func PascalCase(str string) string
- func PickBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map
- func PickByErr[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) (bool, error)) (Map, error)
- func PickByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map
- func PickByValues[K, V comparable, Map ~map[K]V](in Map, values []V) Map
- func Product[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T
- func ProductBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R
- func ProductByErr[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) (R, error)) (R, error)
- func RandomString(size int, charset []rune) string
- func Range(elementNum int) []int
- func RangeFrom[T constraints.Integer | constraints.Float](start T, elementNum int) []T
- func RangeWithSteps[T constraints.Integer | constraints.Float](start, end, step T) []T
- func Reduce[T, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R
- func ReduceErr[T, R any](collection []T, accumulator func(agg R, item T, index int) (R, error), ...) (R, error)
- func ReduceRight[T, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R
- func ReduceRightErr[T, R any](collection []T, accumulator func(agg R, item T, index int) (R, error), ...) (R, error)
- func Reject[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) bool) Slice
- func RejectErr[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) (bool, error)) (Slice, error)
- func RejectMap[T, R any](collection []T, callback func(item T, index int) (R, bool)) []R
- func Repeat[T Clonable[T]](count int, initial T) []T
- func RepeatBy[T any](count int, callback func(index int) T) []T
- func RepeatByErr[T any](count int, callback func(index int) (T, error)) ([]T, error)
- func Replace[T comparable, Slice ~[]T](collection Slice, old, nEw T, n int) Slice
- func ReplaceAll[T comparable, Slice ~[]T](collection Slice, old, nEw T) Slice
- func Reverse[T any, Slice ~[]T](collection Slice) Slicedeprecated
- func RuneLength(str string) int
- func Sample[T any](collection []T) T
- func SampleBy[T any](collection []T, randomIntGenerator randomIntGenerator) T
- func Samples[T any, Slice ~[]T](collection Slice, count int) Slice
- func SamplesBy[T any, Slice ~[]T](collection Slice, count int, randomIntGenerator randomIntGenerator) Slice
- func Shuffle[T any, Slice ~[]T](collection Slice) Slicedeprecated
- func Slice[T any, Slice ~[]T](collection Slice, start, end int) Slice
- func SliceToChannel[T any](bufferSize int, collection []T) <-chan T
- func SliceToMap[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V
- func SliceToMapI[T any, K comparable, V any](collection []T, transform func(item T, index int) (K, V)) map[K]V
- func Sliding[T any, Slice ~[]T](collection Slice, size, step int) []Slice
- func SnakeCase(str string) string
- func Some[T comparable](collection, subset []T) bool
- func SomeBy[T any](collection []T, predicate func(item T) bool) bool
- func Splice[T any, Slice ~[]T](collection Slice, i int, elements ...T) Slice
- func Subset[T any, Slice ~[]T](collection Slice, offset int, length uint) Slice
- func Substring[T ~string](str T, offset int, length uint) T
- func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T
- func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R
- func SumByErr[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) (R, error)) (R, error)
- func Switch[T comparable, R any](predicate T) *switchCase[T, R]
- func Synchronize(opt ...sync.Locker) *synchronize
- func Take[T any, Slice ~[]T](collection Slice, n int) Slice
- func TakeFilter[T any, Slice ~[]T](collection Slice, n int, predicate func(item T, index int) bool) Slice
- func TakeWhile[T any, Slice ~[]T](collection Slice, predicate func(item T) bool) Slice
- func Ternary[T any](condition bool, ifOutput, elseOutput T) T
- func TernaryF[T any](condition bool, ifFunc, elseFunc func() T) T
- func Times[T any](count int, iteratee func(index int) T) []T
- func ToAnySlice[T any](collection []T) []any
- func ToPtr[T any](x T) *T
- func ToSlicePtr[T any](collection []T) []*T
- func Trim[T comparable, Slice ~[]T](collection, cutset Slice) Slice
- func TrimLeft[T comparable, Slice ~[]T](collection, cutset Slice) Slice
- func TrimPrefix[T comparable, Slice ~[]T](collection, prefix Slice) Slice
- func TrimRight[T comparable, Slice ~[]T](collection, cutset Slice) Slice
- func TrimSuffix[T comparable, Slice ~[]T](collection, suffix Slice) Slice
- func Try(callback func() error) (ok bool)
- func Try0(callback func()) bool
- func Try1(callback func() error) bool
- func Try2[T any](callback func() (T, error)) bool
- func Try3[T, R any](callback func() (T, R, error)) bool
- func Try4[T, R, S any](callback func() (T, R, S, error)) bool
- func Try5[T, R, S, Q any](callback func() (T, R, S, Q, error)) bool
- func Try6[T, R, S, Q, U any](callback func() (T, R, S, Q, U, error)) bool
- func TryCatch(callback func() error, catch func())
- func TryCatchWithErrorValue(callback func() error, catch func(any))
- func TryOr[A any](callback func() (A, error), fallbackA A) (A, bool)
- func TryOr1[A any](callback func() (A, error), fallbackA A) (A, bool)
- func TryOr2[A, B any](callback func() (A, B, error), fallbackA A, fallbackB B) (A, B, bool)
- func TryOr3[A, B, C any](callback func() (A, B, C, error), fallbackA A, fallbackB B, fallbackC C) (A, B, C, bool)
- func TryOr4[A, B, C, D any](callback func() (A, B, C, D, error), fallbackA A, fallbackB B, fallbackC C, ...) (A, B, C, D, bool)
- func TryOr5[A, B, C, D, E any](callback func() (A, B, C, D, E, error), fallbackA A, fallbackB B, fallbackC C, ...) (A, B, C, D, E, bool)
- func TryOr6[A, B, C, D, E, F any](callback func() (A, B, C, D, E, F, error), fallbackA A, fallbackB B, ...) (A, B, C, D, E, F, bool)
- func TryWithErrorValue(callback func() error) (errorValue any, ok bool)
- func Union[T comparable, Slice ~[]T](lists ...Slice) Slice
- func Uniq[T comparable, Slice ~[]T](collection Slice) Slice
- func UniqBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) Slice
- func UniqByErr[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) (U, error)) (Slice, error)
- func UniqKeys[K comparable, V any](in ...map[K]V) []K
- func UniqMap[T any, R comparable](collection []T, transform func(item T, index int) R) []R
- func UniqValues[K, V comparable](in ...map[K]V) []V
- func Unpack2[A, B any](tuple Tuple2[A, B]) (A, B)
- func Unpack3[A, B, C any](tuple Tuple3[A, B, C]) (A, B, C)
- func Unpack4[A, B, C, D any](tuple Tuple4[A, B, C, D]) (A, B, C, D)
- func Unpack5[A, B, C, D, E any](tuple Tuple5[A, B, C, D, E]) (A, B, C, D, E)
- func Unpack6[A, B, C, D, E, F any](tuple Tuple6[A, B, C, D, E, F]) (A, B, C, D, E, F)
- func Unpack7[A, B, C, D, E, F, G any](tuple Tuple7[A, B, C, D, E, F, G]) (A, B, C, D, E, F, G)
- func Unpack8[A, B, C, D, E, F, G, H any](tuple Tuple8[A, B, C, D, E, F, G, H]) (A, B, C, D, E, F, G, H)
- func Unpack9[A, B, C, D, E, F, G, H, I any](tuple Tuple9[A, B, C, D, E, F, G, H, I]) (A, B, C, D, E, F, G, H, I)
- func Unzip2[A, B any](tuples []Tuple2[A, B]) ([]A, []B)
- func Unzip3[A, B, C any](tuples []Tuple3[A, B, C]) ([]A, []B, []C)
- func Unzip4[A, B, C, D any](tuples []Tuple4[A, B, C, D]) ([]A, []B, []C, []D)
- func Unzip5[A, B, C, D, E any](tuples []Tuple5[A, B, C, D, E]) ([]A, []B, []C, []D, []E)
- func Unzip6[A, B, C, D, E, F any](tuples []Tuple6[A, B, C, D, E, F]) ([]A, []B, []C, []D, []E, []F)
- func Unzip7[A, B, C, D, E, F, G any](tuples []Tuple7[A, B, C, D, E, F, G]) ([]A, []B, []C, []D, []E, []F, []G)
- func Unzip8[A, B, C, D, E, F, G, H any](tuples []Tuple8[A, B, C, D, E, F, G, H]) ([]A, []B, []C, []D, []E, []F, []G, []H)
- func Unzip9[A, B, C, D, E, F, G, H, I any](tuples []Tuple9[A, B, C, D, E, F, G, H, I]) ([]A, []B, []C, []D, []E, []F, []G, []H, []I)
- func UnzipBy2[In, A, B any](items []In, iteratee func(In) (a A, b B)) ([]A, []B)
- func UnzipBy3[In, A, B, C any](items []In, iteratee func(In) (a A, b B, c C)) ([]A, []B, []C)
- 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)
- 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)
- 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)
- 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)
- 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)
- 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)
- func UnzipByErr2[In, A, B any](items []In, iteratee func(In) (a A, b B, err error)) ([]A, []B, error)
- func UnzipByErr3[In, A, B, C any](items []In, iteratee func(In) (a A, b B, c C, err error)) ([]A, []B, []C, error)
- func UnzipByErr4[In, A, B, C, D any](items []In, iteratee func(In) (a A, b B, c C, d D, err error)) ([]A, []B, []C, []D, error)
- func UnzipByErr5[In, A, B, C, D, E any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, err error)) ([]A, []B, []C, []D, []E, error)
- func UnzipByErr6[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, err error)) ([]A, []B, []C, []D, []E, []F, error)
- func UnzipByErr7[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, err error)) ([]A, []B, []C, []D, []E, []F, []G, error)
- func UnzipByErr8[In, A, B, C, D, E, F, G, H any](items []In, ...) ([]A, []B, []C, []D, []E, []F, []G, []H, error)
- func UnzipByErr9[In, A, B, C, D, E, F, G, H, I any](items []In, ...) ([]A, []B, []C, []D, []E, []F, []G, []H, []I, error)
- func Validate(ok bool, format string, args ...any) error
- func ValueOr[K comparable, V any](in map[K]V, key K, fallback V) V
- func Values[K comparable, V any](in ...map[K]V) []V
- func WaitFor(condition func(i int) bool, timeout, heartbeatDelay time.Duration) (totalIterations int, elapsed time.Duration, conditionFound bool)
- func WaitForWithContext(ctx context.Context, ...) (totalIterations int, elapsed time.Duration, conditionFound bool)
- func Window[T any, Slice ~[]T](collection Slice, size int) []Slice
- func Without[T comparable, Slice ~[]T](collection Slice, exclude ...T) Slice
- func WithoutBy[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) K, exclude ...K) Slice
- func WithoutByErr[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) (K, error), exclude ...K) (Slice, error)
- func WithoutEmpty[T comparable, Slice ~[]T](collection Slice) Slicedeprecated
- func WithoutNth[T any, Slice ~[]T](collection Slice, nths ...int) Slice
- func Words(str string) []string
- func ZipBy2[A, B, Out any](a []A, b []B, iteratee func(a A, b B) 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
- 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
- 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
- func ZipBy6[A, B, C, D, E, F, Out any](a []A, b []B, c []C, d []D, e []E, f []F, ...) []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, ...) []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, ...) []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, ...) []Out
- func ZipByErr2[A, B, Out any](a []A, b []B, iteratee func(a A, b B) (Out, error)) ([]Out, error)
- func ZipByErr3[A, B, C, Out any](a []A, b []B, c []C, iteratee func(a A, b B, c C) (Out, error)) ([]Out, error)
- func ZipByErr4[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, error)) ([]Out, error)
- func ZipByErr5[A, B, C, D, E, Out any](a []A, b []B, c []C, d []D, e []E, ...) ([]Out, error)
- func ZipByErr6[A, B, C, D, E, F, Out any](a []A, b []B, c []C, d []D, e []E, f []F, ...) ([]Out, error)
- func ZipByErr7[A, B, C, D, E, F, G, Out any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, ...) ([]Out, error)
- func ZipByErr8[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, ...) ([]Out, error)
- func ZipByErr9[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, ...) ([]Out, error)
- type Clonable
- type DispatchingStrategy
- type Entry
- type Transaction
- type Tuple2
- type Tuple3
- type Tuple4
- type Tuple5
- type Tuple6
- func CrossJoin6[A, B, C, D, E, F any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F) []Tuple6[A, B, C, D, E, F]
- func T6[A, B, C, D, E, F any](a A, b B, c C, d D, e E, f F) Tuple6[A, B, C, D, E, F]
- func Zip6[A, B, C, D, E, F any](a []A, b []B, c []C, d []D, e []E, f []F) []Tuple6[A, B, C, D, E, F]
- type Tuple7
- func CrossJoin7[A, B, C, D, E, F, G any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G) []Tuple7[A, B, C, D, E, F, G]
- func T7[A, B, C, D, E, F, G any](a A, b B, c C, d D, e E, f F, g G) Tuple7[A, B, C, D, E, F, G]
- func Zip7[A, B, C, D, E, F, G any](a []A, b []B, c []C, d []D, e []E, f []F, g []G) []Tuple7[A, B, C, D, E, F, G]
- type Tuple8
- func CrossJoin8[A, B, C, D, E, F, G, H any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, ...) []Tuple8[A, B, C, D, E, F, G, H]
- func T8[A, B, C, D, E, F, G, H any](a A, b B, c C, d D, e E, f F, g G, h H) Tuple8[A, B, C, D, E, F, G, H]
- func Zip8[A, B, C, D, E, F, G, H any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H) []Tuple8[A, B, C, D, E, F, G, H]
- type Tuple9
- func CrossJoin9[A, B, C, D, E, F, G, H, I any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, ...) []Tuple9[A, B, C, D, E, F, G, H, I]
- func T9[A, B, C, D, E, F, G, H, 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]
- func Zip9[A, B, C, D, E, F, G, H, 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]
- Package (IfElse_Else)
- Package (IfElse_ElseF)
- Package (IfElse_ElseIf)
- Package (IfElse_ElseIfF)
- Package (SwitchCase_Case)
- Package (SwitchCase_CaseF)
- Package (SwitchCase_Default)
- Package (SwitchCase_DefaultF)
- Assign
- Attempt
- AttemptWithDelay
- Chunk
- ChunkEntries
- ChunkString
- Clamp
- Clone
- Compact
- Concat
- Count
- CountBy
- CountByErr
- CountValues
- CountValuesBy
- CrossJoin2
- CrossJoin3
- CrossJoin4
- CrossJoin5
- CrossJoin6
- CrossJoin7
- CrossJoin8
- CrossJoin9
- CrossJoinBy2
- CrossJoinBy3
- CrossJoinBy4
- CrossJoinBy5
- CrossJoinBy6
- CrossJoinBy7
- CrossJoinBy8
- CrossJoinBy9
- CrossJoinByErr2
- Cut
- CutPrefix
- CutSuffix
- Drop
- DropByIndex
- DropRight
- DropRightWhile
- DropWhile
- Earliest
- EarliestBy
- EarliestByErr
- Entries
- ErrorsAs
- Fill
- Filter
- FilterErr
- FilterKeys
- FilterKeysErr
- FilterMap
- FilterMapToSlice
- FilterMapToSliceErr
- FilterSliceToMap
- FilterValues
- FilterValuesErr
- Find
- Find (NotFound)
- FindDuplicates
- FindDuplicatesBy
- FindDuplicatesByErr
- FindErr
- FindIndexOf
- FindIndexOf (NotFound)
- FindKey
- FindKey (NotFound)
- FindKeyBy
- FindKeyBy (NotFound)
- FindLastIndexOf
- FindLastIndexOf (NotFound)
- FindOrElse
- FindOrElse (Found)
- FindUniques
- FindUniquesBy
- First
- First (Empty)
- FirstOr
- FirstOr (Empty)
- FirstOrEmpty
- FirstOrEmpty (Empty)
- FlatMap
- FlatMapErr
- Flatten
- ForEach
- ForEachWhile
- FromEntries
- GroupBy
- GroupByErr
- GroupByMap
- GroupByMapErr
- If
- IfF
- IndexOf
- IndexOf (NotFound)
- Interleave
- Intersect
- IntersectBy
- Invert
- IsSorted
- IsSortedBy
- KeyBy
- KeyByErr
- Keyify
- Keys
- Last
- Last (Empty)
- LastIndexOf
- LastIndexOf (NotFound)
- LastOr
- LastOr (Empty)
- LastOrEmpty
- LastOrEmpty (Empty)
- Latest
- LatestBy
- LatestByErr
- Map
- MapEntries
- MapEntriesErr
- MapErr
- MapKeys
- MapKeysErr
- MapToSlice
- MapToSliceErr
- MapValues
- MapValuesErr
- Max
- Max (Empty)
- MaxBy
- MaxByErr
- MaxIndex
- MaxIndex (Empty)
- MaxIndexBy
- MaxIndexByErr
- Mean
- MeanBy
- MeanByErr
- Min
- Min (Empty)
- MinBy
- MinByErr
- MinIndex
- MinIndex (Empty)
- MinIndexBy
- MinIndexByErr
- Must
- Must0
- Must1
- Must2
- Must3
- Must4
- Must5
- Must6
- NewDebounce
- NewDebounceBy
- NewThrottle
- NewThrottleBy
- NewThrottleByWithCount
- NewThrottleWithCount
- Nth
- Nth (Negative)
- Nth (OutOfBounds)
- NthOr
- NthOr (OutOfBounds)
- NthOrEmpty
- NthOrEmpty (OutOfBounds)
- OmitBy
- OmitByErr
- OmitByKeys
- OmitByValues
- PartitionBy
- PartitionByErr
- PickBy
- PickByErr
- PickByKeys
- PickByValues
- Product
- ProductBy
- ProductByErr
- Range
- Reduce
- ReduceErr
- ReduceRight
- ReduceRightErr
- Reject
- RejectErr
- Repeat
- RepeatBy
- RepeatByErr
- Replace
- Reverse
- RuneLength
- Shuffle
- Slice
- SliceToMap
- Sliding
- Subset
- Substring
- Sum
- SumBy
- SumByErr
- Switch
- T2
- T3
- T4
- T5
- T6
- T7
- T8
- T9
- Take
- TakeFilter
- TakeWhile
- Ternary
- TernaryF
- Times
- Transaction
- Transaction (Error)
- Transaction (Ok)
- Trim
- TrimLeft
- TrimPrefix
- TrimRight
- TrimSuffix
- Try
- Try1
- Try2
- Try3
- Try4
- Try5
- Try6
- TryCatchWithErrorValue
- TryOr
- TryOr1
- TryOr2
- TryOr3
- TryOr4
- TryOr5
- TryOr6
- TryWithErrorValue
- Uniq
- UniqBy
- UniqByErr
- UniqKeys
- UniqMap
- UniqValues
- Unpack2
- Unpack3
- Unpack4
- Unpack5
- Unpack6
- Unpack7
- Unpack8
- Unpack9
- Unzip2
- Unzip3
- Unzip4
- Unzip5
- Unzip6
- Unzip7
- Unzip8
- Unzip9
- UnzipByErr2
- UnzipByErr3
- UnzipByErr4
- UnzipByErr5
- UnzipByErr6
- UnzipByErr7
- UnzipByErr8
- UnzipByErr9
- Validate
- ValueOr
- Values
- Window
- WithoutBy
- WithoutByErr
- Zip2
- Zip3
- Zip4
- Zip5
- Zip6
- Zip7
- Zip8
- Zip9
- ZipByErr2
This section is empty.
var ( LowerCaseLettersCharset = []rune("abcdefghijklmnopqrstuvwxyz") UpperCaseLettersCharset = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ") LettersCharset = append(LowerCaseLettersCharset, UpperCaseLettersCharset...) NumbersCharset = []rune("0123456789") AlphanumericCharset = append(LettersCharset, NumbersCharset...) SpecialCharset = []rune("!@#$%^&*()_+-=[]{}|;':\",./<>?") AllCharset = append(AlphanumericCharset, SpecialCharset...) )
var Assert = func(condition bool, message ...string) { if condition { return } panicMessage := defaultAssertionFailureMessage if len(message) > 0 { panicMessage = fmt.Sprintf("%s: %s", defaultAssertionFailureMessage, message[0]) } panic(panicMessage) }
Assert does nothing when the condition is true, otherwise it panics with an optional message. Play: https://go.dev/play/p/Xv8LLKBMNwI
Assertf does nothing when the condition is true, otherwise it panics with a formatted message. Play: https://go.dev/play/p/TVPEmVcyrdY
var MustChecker = func(err any, messageArgs ...any) { if err == nil { return } switch e := err.(type) { case bool: if !e { message := messageFromMsgAndArgs(messageArgs...) if message == "" { message = "not ok" } panic(message) } case error: message := messageFromMsgAndArgs(messageArgs...) if message != "" { panic(message + ": " + e.Error()) } panic(e.Error()) default: panic("must: invalid err type '" + reflect.TypeOf(err).Name() + "', should either be a bool or an error") } }
MustChecker panics if err is error or false.
Assign merges multiple maps from left to right. Play: https://go.dev/play/p/VhwfJOyxf5o
result := Assign(
map[string]int{"a": 1, "b": 2},
map[string]int{"b": 3, "c": 4},
)
fmt.Printf("%v %v %v %v", len(result), result["a"], result["b"], result["c"])
Output: 3 1 3 4
func Associate[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V
Associate returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs 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 slice. Play: https://go.dev/play/p/WHa2CfMO3Lr
func AssociateI[T any, K comparable, V any](collection []T, transform func(item T, index int) (K, V)) map[K]V
AssociateI returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs 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 slice. Play: https://go.dev/play/p/Ugmz6S22rRO
func Async0(f func()) <-chan struct{}
Async0 executes a function in a goroutine and returns a channel set once the function finishes. Play: https://go.dev/play/p/tNqf1cClG_o
Async2 has the same behavior as Async, but returns the 2 results as a tuple inside the channel. Play: https://go.dev/play/p/5SzzDjssXOH
Async3 has the same behavior as Async, but returns the 3 results as a tuple inside the channel. Play: https://go.dev/play/p/cZpZsDXNmlx
Async4 has the same behavior as Async, but returns the 4 results as a tuple inside the channel. Play: https://go.dev/play/p/9X5O2VrLzkR
Async5 has the same behavior as Async, but returns the 5 results as a tuple inside the channel. Play: https://go.dev/play/p/MqnUJpkmopA
Async6 has the same behavior as Async, but returns the 6 results as a tuple inside the channel. Play: https://go.dev/play/p/kM1X67JPdSP
Attempt invokes a function N times until it returns valid output. Returns either the caught error or nil. When the first argument is less than `1`, the function runs until a successful response is returned. Play: https://go.dev/play/p/3ggJZ2ZKcMj
count1, err1 := Attempt(2, func(i int) error {
if i == 0 {
return errors.New("error")
}
return nil
})
count2, err2 := Attempt(2, func(i int) error {
if i < 10 {
return errors.New("error")
}
return nil
})
fmt.Printf("%v %v\n", count1, err1)
fmt.Printf("%v %v\n", count2, err2)
Output: 2 <nil> 2 error
AttemptWhile invokes a function N times until it returns valid output. Returns either the caught error or nil, along with a bool value to determine whether the function should be invoked again. It will terminate the invoke immediately if the second return value is false. When the first argument is less than `1`, the function runs until a successful response is returned. Play: https://go.dev/play/p/1VS7HxlYMOG
AttemptWhileWithDelay invokes a function N times until it returns valid output, with a pause between each call. Returns either the caught error or nil, along with a bool value to determine whether the function should be invoked again. It will terminate the invoke immediately if the second return value is false. When the first argument is less than `1`, the function runs until a successful response is returned. Play: https://go.dev/play/p/mhufUjJfLEF
AttemptWithDelay invokes a function N times until it returns valid output, with a pause between each call. Returns either the caught error or nil. When the first argument is less than `1`, the function runs until a successful response is returned. Play: https://go.dev/play/p/tVs6CygC7m1
count1, time1, err1 := AttemptWithDelay(2, time.Millisecond, func(i int, _ time.Duration) error {
if i == 0 {
return errors.New("error")
}
return nil
})
count2, time2, err2 := AttemptWithDelay(2, time.Millisecond, func(i int, _ time.Duration) error {
if i < 10 {
return errors.New("error")
}
return nil
})
fmt.Printf("%v %v %v\n", count1, time1.Truncate(time.Millisecond), err1)
fmt.Printf("%v %v %v\n", count2, time2.Truncate(time.Millisecond), err2)
Output: 2 1ms <nil> 2 1ms error
Buffer creates a slice of n elements from a channel. Returns the slice and the slice length. @TODO: we should probably provide a helper that reuses the same buffer. Play: https://go.dev/play/p/gPQ-6xmcKQI
BufferWithContext creates a slice of n elements from a channel, with context. Returns the slice and the slice length. @TODO: we should probably provide a helper that reuses the same buffer. Play: https://go.dev/play/p/oRfOyJWK9YF
func ChannelDispatcher[T any](stream <-chan T, count, channelBufferCap int, strategy DispatchingStrategy[T]) []<-chan T
ChannelDispatcher distributes messages from input channels into N child channels. 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
Chunk returns a slice of elements split into groups of length size. If the slice can't be split evenly, the final chunk will be the remaining elements. Play: https://go.dev/play/p/kEMkFbdu85g
list := []int{0, 1, 2, 3, 4}
result := Chunk(list, 2)
for _, item := range result {
fmt.Printf("%v\n", item)
}
Output: [0 1] [2 3] [4]
ChunkEntries splits a map into a slice of elements in groups of length equal to its size. If the map cannot be split evenly, the final chunk will contain the remaining elements. Play: https://go.dev/play/p/X_YQL6mmoD-
result := ChunkEntries(
map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
},
3,
)
for i := range result {
fmt.Printf("%d\n", len(result[i]))
}
Output: 3 2
ChunkString returns a slice of strings split into groups of length size. If the string can't be split evenly, the final chunk will be the remaining characters. Play: https://go.dev/play/p/__FLTuJVz54
Note: lo.ChunkString and lo.Chunk functions behave inconsistently for empty input: lo.ChunkString("", n) returns [""] instead of []. See https://github.com/samber/lo/issues/788
result1 := ChunkString("123456", 2)
result2 := ChunkString("1234567", 2)
result3 := ChunkString("", 2)
result4 := ChunkString("1", 2)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
Output: [12 34 56] [12 34 56 7] [] [1]
Clamp clamps number within the inclusive lower and upper bounds. Play: https://go.dev/play/p/RU4lJNC2hlI
result1 := Clamp(0, -10, 10)
result2 := Clamp(-42, -10, 10)
result3 := Clamp(42, -10, 10)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
Output: 0 -10 10
func Clone[T any, Slice ~[]T](collection Slice) Slice
Clone returns a shallow copy of the collection.
input := []int{1, 2, 3, 4, 5}
output := Clone(input)
input[0] = 42
fmt.Printf("%v", output)
Output: [1 2 3 4 5]
func Compact[T comparable, Slice ~[]T](collection Slice) Slice
Compact returns a slice of all non-zero elements. Play: https://go.dev/play/p/tXiy-iK6PAc
list := []string{"", "foo", "", "bar", ""}
result := Compact(list)
fmt.Printf("%v", result)
Output: [foo bar]
func Concat[T any, Slice ~[]T](collections ...Slice) Slice
Concat returns a new slice containing all the elements in collections. Concat conserves the order of the elements. See also: Flatten, Union.
list := []int{0, 1, 2, 3, 4, 5}
result := Concat(list, list)
fmt.Printf("%v", result)
Output: [0 1 2 3 4 5 0 1 2 3 4 5]
Count counts the number of elements in the collection that equal value. Play: https://go.dev/play/p/Y3FlK54yveC
list := []int{0, 1, 2, 3, 4, 5, 0, 1, 2, 3}
result := Count(list, 2)
fmt.Printf("%v", result)
Output: 2
CountBy counts the number of elements in the collection for which predicate is true. Play: https://go.dev/play/p/ByQbNYQQi4X
list := []int{0, 1, 2, 3, 4, 5, 0, 1, 2, 3}
result := CountBy(list, func(i int) bool {
return i < 4
})
fmt.Printf("%v", result)
Output: 8
CountByErr counts the number of elements in the collection for which predicate is true. It returns the first error returned by the predicate.
list := []int{0, 1, 2, 3, 4, 5, 0, 1, 2, 3}
_, err := CountByErr(list, func(i int) (bool, error) {
if i == 3 {
return false, errors.New("number 3 is not allowed")
}
return i < 4, nil
})
fmt.Printf("%v", err)
Output: number 3 is not allowed
CountValues counts the number of each element in the collection. Play: https://go.dev/play/p/-p-PyLT4dfy
result1 := CountValues([]int{})
result2 := CountValues([]int{1, 2})
result3 := CountValues([]int{1, 2, 2})
result4 := CountValues([]string{"foo", "bar", ""})
result5 := CountValues([]string{"foo", "bar", "bar"})
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
Output: map[] map[1:1 2:1] map[1:1 2:2] map[:1 bar:1 foo:1] map[bar:2 foo:1]
func CountValuesBy[T any, U comparable](collection []T, transform func(item T) U) map[U]int
CountValuesBy counts the number of each element returned from transform function. Is equivalent to chaining lo.Map and lo.CountValues. Play: https://go.dev/play/p/2U0dG1SnOmS
isEven := func(v int) bool {
return v%2 == 0
}
result1 := CountValuesBy([]int{}, isEven)
result2 := CountValuesBy([]int{1, 2}, isEven)
result3 := CountValuesBy([]int{1, 2, 2}, isEven)
length := func(v string) int {
return len(v)
}
result4 := CountValuesBy([]string{"foo", "bar", ""}, length)
result5 := CountValuesBy([]string{"foo", "bar", "bar"}, length)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
Output: map[] map[false:1 true:1] map[false:1 true:2] map[0:1 3:2] map[3:3]
func CrossJoinBy2[A, B, Out any](listA []A, listB []B, transform func(a A, b B) Out) []Out
CrossJoinBy2 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/8Y7btpvuA-C
result := CrossJoinBy2([]string{"a", "b"}, []int{1, 2, 3, 4}, func(a string, b int) string {
return fmt.Sprintf("%v-%v", a, b)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: a-1 a-2 a-3 a-4 b-1 b-2 b-3 b-4
func CrossJoinBy3[A, B, C, Out any](listA []A, listB []B, listC []C, transform func(a A, b B, c C) Out) []Out
CrossJoinBy3 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/3z4y5x6w7v8
result := CrossJoinBy3([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, func(a string, b int, c bool) string {
return fmt.Sprintf("%v-%v-%v", a, b, c)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: a-1-true a-1-false a-2-true a-2-false a-3-true a-3-false a-4-true a-4-false b-1-true b-1-false b-2-true b-2-false b-3-true b-3-false b-4-true b-4-false
func CrossJoinBy4[A, B, C, D, Out any](listA []A, listB []B, listC []C, listD []D, transform func(a A, b B, c C, d D) Out) []Out
CrossJoinBy4 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/8b9c0d1e2f3
result := CrossJoinBy4([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, func(a string, b int, c bool, d foo) string {
return fmt.Sprintf("%v-%v-%v-%v", a, b, c, d)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: a-1-true-{bar} a-1-false-{bar} a-2-true-{bar} a-2-false-{bar} a-3-true-{bar} a-3-false-{bar} a-4-true-{bar} a-4-false-{bar} b-1-true-{bar} b-1-false-{bar} b-2-true-{bar} b-2-false-{bar} b-3-true-{bar} b-3-false-{bar} b-4-true-{bar} b-4-false-{bar}
func CrossJoinBy5[A, B, C, D, E, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, transform func(a A, b B, c C, d D, e E) Out) []Out
CrossJoinBy5 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/4g5h6i7j8k9
result := CrossJoinBy5([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, func(a string, b int, c bool, d foo, e float64) string {
return fmt.Sprintf("%v-%v-%v-%v-%v", a, b, c, d, e)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: a-1-true-{bar}-4.2 a-1-false-{bar}-4.2 a-2-true-{bar}-4.2 a-2-false-{bar}-4.2 a-3-true-{bar}-4.2 a-3-false-{bar}-4.2 a-4-true-{bar}-4.2 a-4-false-{bar}-4.2 b-1-true-{bar}-4.2 b-1-false-{bar}-4.2 b-2-true-{bar}-4.2 b-2-false-{bar}-4.2 b-3-true-{bar}-4.2 b-3-false-{bar}-4.2 b-4-true-{bar}-4.2 b-4-false-{bar}-4.2
func CrossJoinBy6[A, B, C, D, E, F, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, transform func(a A, b B, c C, d D, e E, f F) Out) []Out
CrossJoinBy6 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/1l2m3n4o5p6
result := CrossJoinBy6([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, func(a string, b int, c bool, d foo, e float64, f string) string {
return fmt.Sprintf("%v-%v-%v-%v-%v-%v", a, b, c, d, e, f)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: a-1-true-{bar}-4.2-plop a-1-false-{bar}-4.2-plop a-2-true-{bar}-4.2-plop a-2-false-{bar}-4.2-plop a-3-true-{bar}-4.2-plop a-3-false-{bar}-4.2-plop a-4-true-{bar}-4.2-plop a-4-false-{bar}-4.2-plop b-1-true-{bar}-4.2-plop b-1-false-{bar}-4.2-plop b-2-true-{bar}-4.2-plop b-2-false-{bar}-4.2-plop b-3-true-{bar}-4.2-plop b-3-false-{bar}-4.2-plop b-4-true-{bar}-4.2-plop b-4-false-{bar}-4.2-plop
func CrossJoinBy7[A, B, C, D, E, F, G, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, transform func(a A, b B, c C, d D, e E, f F, g G) Out) []Out
CrossJoinBy7 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/7q8r9s0t1u2
result := CrossJoinBy7([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, func(a string, b int, c bool, d foo, e float64, f string, g bool) string {
return fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v", a, b, c, d, e, f, g)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: a-1-true-{bar}-4.2-plop-false a-1-false-{bar}-4.2-plop-false a-2-true-{bar}-4.2-plop-false a-2-false-{bar}-4.2-plop-false a-3-true-{bar}-4.2-plop-false a-3-false-{bar}-4.2-plop-false a-4-true-{bar}-4.2-plop-false a-4-false-{bar}-4.2-plop-false b-1-true-{bar}-4.2-plop-false b-1-false-{bar}-4.2-plop-false b-2-true-{bar}-4.2-plop-false b-2-false-{bar}-4.2-plop-false b-3-true-{bar}-4.2-plop-false b-3-false-{bar}-4.2-plop-false b-4-true-{bar}-4.2-plop-false b-4-false-{bar}-4.2-plop-false
func CrossJoinBy8[A, B, C, D, E, F, G, H, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H, transform func(a A, b B, c C, d D, e E, f F, g G, h H) Out) []Out
CrossJoinBy8 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/3v4w5x6y7z8
result := CrossJoinBy8([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, func(a string, b int, c bool, d foo, e float64, f string, g bool, h int) string {
return fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v-%v", a, b, c, d, e, f, g, h)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: a-1-true-{bar}-4.2-plop-false-42 a-1-false-{bar}-4.2-plop-false-42 a-2-true-{bar}-4.2-plop-false-42 a-2-false-{bar}-4.2-plop-false-42 a-3-true-{bar}-4.2-plop-false-42 a-3-false-{bar}-4.2-plop-false-42 a-4-true-{bar}-4.2-plop-false-42 a-4-false-{bar}-4.2-plop-false-42 b-1-true-{bar}-4.2-plop-false-42 b-1-false-{bar}-4.2-plop-false-42 b-2-true-{bar}-4.2-plop-false-42 b-2-false-{bar}-4.2-plop-false-42 b-3-true-{bar}-4.2-plop-false-42 b-3-false-{bar}-4.2-plop-false-42 b-4-true-{bar}-4.2-plop-false-42 b-4-false-{bar}-4.2-plop-false-42
func CrossJoinBy9[A, B, C, D, E, F, G, H, I, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H, listI []I, transform func(a A, b B, c C, d D, e E, f F, g G, h H, i I) Out) []Out
CrossJoinBy9 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. Play: https://go.dev/play/p/9a0b1c2d3e4
result := CrossJoinBy9([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, []string{"hello world"}, func(a string, b int, c bool, d foo, e float64, f string, g bool, h int, i string) string {
return fmt.Sprintf("%v-%v-%v-%v-%v-%v-%v-%v-%v", a, b, c, d, e, f, g, h, i)
})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: a-1-true-{bar}-4.2-plop-false-42-hello world a-1-false-{bar}-4.2-plop-false-42-hello world a-2-true-{bar}-4.2-plop-false-42-hello world a-2-false-{bar}-4.2-plop-false-42-hello world a-3-true-{bar}-4.2-plop-false-42-hello world a-3-false-{bar}-4.2-plop-false-42-hello world a-4-true-{bar}-4.2-plop-false-42-hello world a-4-false-{bar}-4.2-plop-false-42-hello world b-1-true-{bar}-4.2-plop-false-42-hello world b-1-false-{bar}-4.2-plop-false-42-hello world b-2-true-{bar}-4.2-plop-false-42-hello world b-2-false-{bar}-4.2-plop-false-42-hello world b-3-true-{bar}-4.2-plop-false-42-hello world b-3-false-{bar}-4.2-plop-false-42-hello world b-4-true-{bar}-4.2-plop-false-42-hello world b-4-false-{bar}-4.2-plop-false-42-hello world
func CrossJoinByErr2[A, B, Out any](listA []A, listB []B, transform func(a A, b B) (Out, error)) ([]Out, error)
CrossJoinByErr2 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. It returns the first error returned by the transform function.
result, err := CrossJoinByErr2([]string{"a", "b"}, []int{1, 2}, func(a string, b int) (string, error) {
if a == "b" {
return "", errors.New("b not allowed")
}
return fmt.Sprintf("%v-%v", a, b), nil
})
if err != nil {
fmt.Printf("error: %v\n", err)
return
}
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: error: b not allowed
func CrossJoinByErr3[A, B, C, Out any](listA []A, listB []B, listC []C, transform func(a A, b B, c C) (Out, error)) ([]Out, error)
CrossJoinByErr3 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. It returns the first error returned by the transform function.
func CrossJoinByErr4[A, B, C, D, Out any](listA []A, listB []B, listC []C, listD []D, transform func(a A, b B, c C, d D) (Out, error)) ([]Out, error)
CrossJoinByErr4 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. It returns the first error returned by the transform function.
func CrossJoinByErr5[A, B, C, D, E, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, transform func(a A, b B, c C, d D, e E) (Out, error)) ([]Out, error)
CrossJoinByErr5 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. It returns the first error returned by the transform function.
func CrossJoinByErr6[A, B, C, D, E, F, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, transform func(a A, b B, c C, d D, e E, f F) (Out, error)) ([]Out, error)
CrossJoinByErr6 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. It returns the first error returned by the transform function.
func CrossJoinByErr7[A, B, C, D, E, F, G, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, transform func(a A, b B, c C, d D, e E, f F, g G) (Out, error)) ([]Out, error)
CrossJoinByErr7 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. It returns the first error returned by the transform function.
func CrossJoinByErr8[A, B, C, D, E, F, G, H, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H, transform func(a A, b B, c C, d D, e E, f F, g G, h H) (Out, error)) ([]Out, error)
CrossJoinByErr8 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. It returns the first error returned by the transform function.
func CrossJoinByErr9[A, B, C, D, E, F, G, H, I, Out any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H, listI []I, transform func(a A, b B, c C, d D, e E, f F, g G, h H, i I) (Out, error)) ([]Out, error)
CrossJoinByErr9 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. The transform function is used to create the output values. Returns an empty list if a list is empty. It returns the first error returned by the transform function.
func Cut[T comparable, Slice ~[]T](collection, separator Slice) (before, after Slice, found bool)
Cut slices collection around the first instance of separator, returning the part of collection 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
collection := []string{"a", "b", "c", "d", "e", "f", "g"}
// Test with valid separator
before, after, found := Cut(collection, []string{"b", "c", "d"})
fmt.Printf("Before: %v, After: %v, Found: %t\n", before, after, found)
// Test with separator not found
before2, after2, found2 := Cut(collection, []string{"z"})
fmt.Printf("Before: %v, After: %v, Found: %t\n", before2, after2, found2)
// Test with separator at beginning
before3, after3, found3 := Cut(collection, []string{"a", "b"})
fmt.Printf("Before: %v, After: %v, Found: %t\n", before3, after3, found3)
Output: Before: [a], After: [e f g], Found: true Before: [a b c d e f g], After: [], Found: false Before: [], After: [c d e f g], Found: true
func CutPrefix[T comparable, Slice ~[]T](collection, separator Slice) (after Slice, found bool)
CutPrefix returns collection without the provided leading prefix []T and reports whether it found the prefix. 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
collection := []string{"a", "b", "c", "d", "e", "f", "g"}
// Test with valid prefix
after, found := CutPrefix(collection, []string{"a", "b", "c"})
fmt.Printf("After: %v, Found: %t\n", after, found)
// Test with prefix not found
after2, found2 := CutPrefix(collection, []string{"b"})
fmt.Printf("After: %v, Found: %t\n", after2, found2)
// Test with empty prefix
after3, found3 := CutPrefix(collection, []string{})
fmt.Printf("After: %v, Found: %t\n", after3, found3)
Output: After: [d e f g], Found: true After: [a b c d e f g], Found: false After: [a b c d e f g], Found: true
func CutSuffix[T comparable, Slice ~[]T](collection, separator Slice) (before Slice, found bool)
CutSuffix returns collection without the provided ending suffix []T and reports 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
collection := []string{"a", "b", "c", "d", "e", "f", "g"}
// Test with valid suffix
before, found := CutSuffix(collection, []string{"f", "g"})
fmt.Printf("Before: %v, Found: %t\n", before, found)
// Test with suffix not found
before2, found2 := CutSuffix(collection, []string{"b"})
fmt.Printf("Before: %v, Found: %t\n", before2, found2)
// Test with empty suffix
before3, found3 := CutSuffix(collection, []string{})
fmt.Printf("Before: %v, Found: %t\n", before3, found3)
Output: Before: [a b c d e], Found: true Before: [a b c d e f g], Found: false Before: [a b c d e f g], Found: true
func Difference[T comparable, Slice ~[]T](list1, list2 Slice) (Slice, Slice)
Difference returns the difference between two collections. 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
DispatchingStrategyFirst distributes messages in the first non-full channel. If the capacity of the first channel is exceeded, the second channel will be selected and so on. Play: https://go.dev/play/p/OrJCvOmk42f
DispatchingStrategyMost distributes messages in the fullest channel. If the channel capacity is exceeded, the next channel will be selected and so on. Play: https://go.dev/play/p/erHHone7rF9
DispatchingStrategyRoundRobin distributes messages in a rotating sequential manner. If the channel capacity is exceeded, the next channel will be selected and so on. Play: https://go.dev/play/p/UZGu2wVg3J2
Drop drops n elements from the beginning of a slice. Play: https://go.dev/play/p/JswS7vXRJP2
list := []int{0, 1, 2, 3, 4, 5}
result := Drop(list, 2)
fmt.Printf("%v", result)
Output: [2 3 4 5]
DropByIndex drops elements from a slice by the index. A negative index will drop elements from the end of the slice. Play: https://go.dev/play/p/bPIH4npZRxS
list := []int{0, 1, 2, 3, 4, 5}
result := DropByIndex(list, 2)
fmt.Printf("%v", result)
Output: [0 1 3 4 5]
DropRight drops n elements from the end of a slice. Play: https://go.dev/play/p/GG0nXkSJJa3
list := []int{0, 1, 2, 3, 4, 5}
result := DropRight(list, 2)
fmt.Printf("%v", result)
Output: [0 1 2 3]
DropRightWhile drops elements from the end of a slice while the predicate returns true. Play: https://go.dev/play/p/3-n71oEC0Hz
list := []int{0, 1, 2, 3, 4, 5}
result := DropRightWhile(list, func(val int) bool {
return val > 2
})
fmt.Printf("%v", result)
Output: [0 1 2]
DropWhile drops elements from the beginning of a slice while the predicate returns true. Play: https://go.dev/play/p/7gBPYw2IK16
list := []int{0, 1, 2, 3, 4, 5}
result := DropWhile(list, func(val int) bool {
return val < 2
})
fmt.Printf("%v", result)
Output: [2 3 4 5]
Duration4 returns the time taken to execute a function. Play: https://go.dev/play/p/xr863iwkAxQ
Duration5 returns the time taken to execute a function. Play: https://go.dev/play/p/xr863iwkAxQ
func Duration6[A, B, C, D, E, F any](callback func() (A, B, C, D, E, F)) (A, B, C, D, E, F, time.Duration)
Duration6 returns the time taken to execute a function. Play: https://go.dev/play/p/mR4bTQKO-Tf
func Duration7[A, B, C, D, E, F, G any](callback func() (A, B, C, D, E, F, G)) (A, B, C, D, E, F, G, time.Duration)
Duration7 returns the time taken to execute a function. Play: https://go.dev/play/p/jgIAcBWWInS
func Duration8[A, B, C, D, E, F, G, H any](callback func() (A, B, C, D, E, F, G, H)) (A, B, C, D, E, F, G, H, time.Duration)
Duration8 returns the time taken to execute a function. Play: https://go.dev/play/p/T8kxpG1c5Na
func Duration9[A, B, C, D, E, F, G, H, I any](callback func() (A, B, C, D, E, F, G, H, I)) (A, B, C, D, E, F, G, H, I, time.Duration)
Duration9 returns the time taken to execute a function. Play: https://go.dev/play/p/bg9ix2VrZ0j
func Duration10[A, B, C, D, E, F, G, H, I, J any](callback func() (A, B, C, D, E, F, G, H, I, J)) (A, B, C, D, E, F, G, H, I, J, time.Duration)
Duration10 returns the time taken to execute a function. Play: https://go.dev/play/p/Y3n7oJXqJbk
Earliest search the minimum time.Time of a collection. Returns zero value when the collection is empty.
now := time.Now()
past := now.Add(-time.Hour)
future := now.Add(time.Hour)
result := Earliest(future, now, past)
fmt.Printf("%t", result.Equal(past))
Output: true
EarliestBy search the minimum time.Time of a collection using the given iteratee function. Returns zero value when the collection is empty.
type Event struct {
Name string
Time time.Time
}
now := time.Now()
events := []Event{
{Name: "Event A", Time: now.Add(time.Hour)},
{Name: "Event B", Time: now},
{Name: "Event C", Time: now.Add(-time.Hour)},
}
result := EarliestBy(events, func(event Event) time.Time {
return event.Time
})
fmt.Printf("%s", result.Name)
Output: Event C
EarliestByErr search the minimum time.Time of a collection using the given iteratee function. Returns zero value and nil error when the collection is empty. If the iteratee returns an error, iteration stops and the error is returned.
type Event struct {
Name string
Time time.Time
}
now := time.Now()
events := []Event{
{Name: "Event A", Time: now.Add(time.Hour)},
{Name: "Event B", Time: now},
{Name: "Event C", Time: now.Add(-time.Hour)},
}
_, err := EarliestByErr(events, func(event Event) (time.Time, error) {
if event.Name == "Event B" {
return time.Time{}, errors.New("event b is not allowed")
}
return event.Time, nil
})
fmt.Printf("%v", err)
Output: event b is not allowed
ElementsMatch returns true if lists contain the same set of elements (including empty set). 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 ElementsMatchBy[T any, K comparable](list1, list2 []T, iteratee func(item T) K) bool
ElementsMatchBy returns true if lists contain the same set of elements' keys (including empty set). 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
Ellipsis trims and truncates a string to a specified length in runes and appends an ellipsis if truncated. The length parameter counts Unicode code points (runes), not bytes, so multi-byte characters such as emoji or CJK ideographs are never split in the middle. Play: https://go.dev/play/p/qE93rgqe1TW
ErrorsAs is a shortcut for errors.As(err, &&T). Play: https://go.dev/play/p/8wk5rH8UfrE
doSomething := func() error {
return &myError{}
}
err := doSomething()
if rateLimitErr, ok := ErrorsAs[*myError](err); ok {
fmt.Printf("is type myError, err: %s", rateLimitErr.Error())
} else {
fmt.Printf("is not type myError")
}
Output: is type myError, err: my error
EveryBy returns true if the predicate returns true for all elements in the collection or if the collection is empty. Play: https://go.dev/play/p/dn1-vhHsq9x
FanIn collects messages from multiple input channels into a single buffered channel. Output messages have no priority. When all upstream channels reach EOF, downstream channel closes. Play: https://go.dev/play/p/FH8Wq-T04Jb
FanOut broadcasts all the upstream messages to multiple downstream channels. 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 Fill[T Clonable[T], Slice ~[]T](collection Slice, initial T) Slice
Fill fills elements of a slice with `initial` value. Play: https://go.dev/play/p/VwR34GzqEub
list := []foo{{"a"}, {"a"}}
result := Fill(list, foo{"b"})
fmt.Printf("%v", result)
Output: [{b} {b}]
Filter iterates over elements of collection, returning a slice of all elements predicate returns true for. Play: https://go.dev/play/p/Apjg3WeSi7K
list := []int64{1, 2, 3, 4}
result := Filter(list, func(nbr int64, index int) bool {
return nbr%2 == 0
})
fmt.Printf("%v", result)
Output: [2 4]
func FilterErr[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) (bool, error)) (Slice, error)
FilterErr iterates over elements of collection, returning a slice of all elements predicate returns true for. If the predicate returns an error, iteration stops immediately and returns the error. Play: https://go.dev/play/p/Apjg3WeSi7K
list := []int64{1, 2, 3, 4}
result, err := FilterErr(list, func(nbr int64, index int) (bool, error) {
if nbr == 3 {
return false, errors.New("number 3 is not allowed")
}
return nbr%2 == 0, nil
})
fmt.Printf("%v, %v\n", result, err)
result, err = FilterErr([]int64{1, 2, 4, 6}, func(nbr int64, index int) (bool, error) {
return nbr%2 == 0, nil
})
fmt.Printf("%v, %v\n", result, err)
Output: [], number 3 is not allowed [2 4 6], <nil>
func FilterKeys[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) []K
FilterKeys transforms a map into a slice based on predicate returns true for specific elements. It is a mix of lo.Filter() and lo.Keys(). Play: https://go.dev/play/p/OFlKXlPrBAe
kv := map[int]string{1: "foo", 2: "bar", 3: "baz"}
result := FilterKeys(kv, func(k int, v string) bool {
return v == "foo"
})
fmt.Printf("%v", result)
Output: [1]
FilterKeysErr transforms a map into a slice of keys based on predicate that can return an error. It is a mix of lo.Filter() and lo.Keys() with error handling. If the predicate returns true, the key is added to the result slice. If the predicate returns an error, iteration stops immediately and returns the error. The order of the keys in the input map is not specified.
kv := map[int]string{1: "foo", 2: "bar", 3: "baz"}
result, err := FilterKeysErr(kv, func(k int, v string) (bool, error) {
if k == 3 {
return false, errors.New("key 3 not allowed")
}
return v == "foo", nil
})
fmt.Printf("%v, %v\n", result, err)
result, err = FilterKeysErr(kv, func(k int, v string) (bool, error) {
return v == "bar", nil
})
fmt.Printf("%v, %v\n", result, err)
Output: [], key 3 not allowed [2], <nil>
FilterMap returns a slice obtained after both filtering and mapping using the given callback function. The callback function should return two values:
- the result of the mapping operation and
- whether the result element should be included or not.
Play: https://go.dev/play/p/CgHYNUpOd1I
list := []int64{1, 2, 3, 4}
result := FilterMap(list, func(nbr int64, index int) (string, bool) {
return strconv.FormatInt(nbr*2, 10), nbr%2 == 0
})
fmt.Printf("%v", result)
Output: [4 8]
func FilterMapToSlice[K comparable, V, R any](in map[K]V, iteratee func(key K, value V) (R, bool)) []R
FilterMapToSlice transforms a map into a slice based on specified iteratee. The iteratee returns a value and a boolean. If the boolean is true, the value is added to the result slice. 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
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}
result := FilterMapToSlice(kv, func(k int, v int64) (string, bool) {
return fmt.Sprintf("%d_%d", k, v), k%2 == 0
})
sort.Strings(result)
fmt.Printf("%v", result)
Output: [2_2 4_4]
FilterMapToSliceErr transforms a map into a slice based on specified iteratee. The iteratee returns a value, a boolean, and an error. If the boolean is true, the value is added to the result slice. If the boolean is false, the value is not added to the result slice. If an error is returned, iteration stops immediately and returns the error. 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.
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}
_, err := FilterMapToSliceErr(kv, func(k int, v int64) (string, bool, error) {
if k == 3 {
return "", false, errors.New("key 3 is not allowed")
}
return fmt.Sprintf("%d_%d", k, v), k%2 == 0, nil
})
fmt.Printf("%v", err)
Output: key 3 is not allowed
func FilterReject[T any, Slice ~[]T](collection Slice, predicate func(T, int) bool) (kept, rejected Slice)
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 FilterSliceToMap[T any, K comparable, V any](collection []T, transform func(item T) (K, V, bool)) map[K]V
FilterSliceToMap returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs 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 slice. The third return value of the transform function is a boolean that indicates whether the key-value pair should be included in the map. Play: https://go.dev/play/p/2z0rDz2ZSGU
list := []string{"a", "aa", "aaa"}
result := FilterSliceToMap(list, func(str string) (string, int, bool) {
return str, len(str), len(str) > 1
})
fmt.Printf("%v", result)
Output: map[aa:2 aaa:3]
func FilterSliceToMapI[T any, K comparable, V any](collection []T, transform func(item T, index int) (K, V, bool)) map[K]V
FilterSliceToMapI returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs 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 slice. The third return value of the transform function is a boolean that indicates whether the key-value pair should be included in the map. Play: https://go.dev/play/p/mSz_bUIk9aJ
func FilterValues[K comparable, V any](in map[K]V, predicate func(key K, value V) bool) []V
FilterValues transforms a map into a slice based on predicate returns true for specific elements. It is a mix of lo.Filter() and lo.Values(). Play: https://go.dev/play/p/YVD5r_h-LX-
kv := map[int]string{1: "foo", 2: "bar", 3: "baz"}
result := FilterValues(kv, func(k int, v string) bool {
return v == "foo"
})
fmt.Printf("%v", result)
Output: [foo]
FilterValuesErr transforms a map into a slice of values based on predicate that can return an error. It is a mix of lo.Filter() and lo.Values() with error handling. If the predicate returns true, the value is added to the result slice. If the predicate returns an error, iteration stops immediately and returns the error. The order of the keys in the input map is not specified.
kv := map[int]string{1: "foo", 2: "bar", 3: "baz"}
result, err := FilterValuesErr(kv, func(k int, v string) (bool, error) {
if k == 3 {
return false, errors.New("key 3 not allowed")
}
return v == "foo", nil
})
fmt.Printf("%v, %v\n", result, err)
result, err = FilterValuesErr(kv, func(k int, v string) (bool, error) {
return v == "bar", nil
})
fmt.Printf("%v, %v\n", result, err)
Output: [], key 3 not allowed [bar], <nil>
Find searches for an element in a slice based on a predicate. Returns element and true if element was found. Play: https://go.dev/play/p/Eo7W0lvKTky
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
result, found := Find(users, func(user User) bool {
return user.Age > 30
})
fmt.Printf("%s %t", result.Name, found)
Output: Charlie true
list := []int{1, 2, 3, 4, 5}
result, found := Find(list, func(n int) bool {
return n > 10
})
fmt.Printf("%d %t", result, found)
Output: 0 false
func FindDuplicates[T comparable, Slice ~[]T](collection Slice) Slice
FindDuplicates returns a slice with the first occurrence of each duplicated element in the collection. The order of result values is determined by the order they occur in the collection.
list := []int{1, 2, 2, 3, 3, 3, 4, 5}
result := FindDuplicates(list)
fmt.Printf("%v", result)
Output: [2 3]
func FindDuplicatesBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) Slice
FindDuplicatesBy returns a slice with the first occurrence of each duplicated element in the collection. The order of result values is determined by the order they occur in the slice. It accepts `iteratee` which is invoked for each element in the slice to generate the criterion by which uniqueness is computed.
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 25},
{Name: "David", Age: 30},
{Name: "Eve", Age: 35},
}
result := FindDuplicatesBy(users, func(user User) int {
return user.Age
})
fmt.Printf("%d", len(result))
Output: 2
func FindDuplicatesByErr[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) (U, error)) (Slice, error)
FindDuplicatesByErr returns a slice with the first occurrence of each duplicated element in the collection. The order of result values is determined by the order they occur in the slice. It accepts `iteratee` which is invoked for each element in the slice to generate the criterion by which uniqueness is computed. If the iteratee returns an error, iteration stops immediately and the error is returned with a nil slice.
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 25},
{Name: "David", Age: 30},
{Name: "Eve", Age: 35},
}
result, err := FindDuplicatesByErr(users, func(user User) (int, error) {
if user.Name == "Charlie" {
return 0, errors.New("charlie is not allowed")
}
return user.Age, nil
})
fmt.Printf("%d %v", len(result), err)
Output: 0 charlie is not allowed
FindErr searches for an element in a slice based on a predicate that can return an error. Returns the element and nil error if the element is found. Returns zero value and nil error if the element is not found. If the predicate returns an error, iteration stops immediately and returns zero value and the error.
result, err := FindErr([]string{"a", "b", "c", "d"}, func(i string) (bool, error) {
return i == "b", nil
})
fmt.Printf("%v %v\n", result, err)
result, err = FindErr([]string{"foobar"}, func(i string) (bool, error) {
return i == "b", nil
})
fmt.Printf("%v %v\n", result, err)
result, err = FindErr([]string{"a", "b", "c"}, func(i string) (bool, error) {
if i == "b" {
return false, errors.New("b is not allowed")
}
return i == "b", nil
})
fmt.Printf("%v %v", result, err)
Output: b <nil> <nil> b is not allowed
FindIndexOf searches for an element in a slice based on a predicate and returns the index and true. Returns -1 and false if the element is not found. Play: https://go.dev/play/p/XWSEM4Ic_t0
list := []int{1, 2, 3, 4, 5}
result, index, found := FindIndexOf(list, func(n int) bool {
return n > 2
})
fmt.Printf("%d %d %t", result, index, found)
Output: 3 2 true
list := []int{1, 2, 3, 4, 5}
result, index, found := FindIndexOf(list, func(n int) bool {
return n > 10
})
fmt.Printf("%d %d %t", result, index, found)
Output: 0 -1 false
FindKey returns the key of the first value matching. Play: https://go.dev/play/p/Bg0w1VDPYXx
users := map[string]int{
"Alice": 25,
"Bob": 30,
"Charlie": 35,
}
key, found := FindKey(users, 30)
fmt.Printf("%s %t", key, found)
Output: Bob true
users := map[string]int{
"Alice": 25,
"Bob": 30,
"Charlie": 35,
}
key, found := FindKey(users, 40)
fmt.Printf("%s %t", key, found)
Output: false
FindKeyBy returns the key of the first element predicate returns true for. Play: https://go.dev/play/p/9IbiPElcyo8
users := map[string]int{
"Alice": 25,
"Bob": 30,
"Charlie": 35,
}
key, found := FindKeyBy(users, func(name string, age int) bool {
return age > 30
})
fmt.Printf("%s %t", key, found)
Output: Charlie true
users := map[string]int{
"Alice": 25,
"Bob": 30,
"Charlie": 35,
}
key, found := FindKeyBy(users, func(name string, age int) bool {
return age > 40
})
fmt.Printf("%s %t", key, found)
Output: false
FindLastIndexOf searches for the last element in a slice based on a predicate and returns the index and true. Returns -1 and false if the element is not found. Play: https://go.dev/play/p/dPiMRtJ6cUx
list := []int{1, 2, 3, 4, 3, 5}
result, index, found := FindLastIndexOf(list, func(n int) bool {
return n == 3
})
fmt.Printf("%d %d %t", result, index, found)
Output: 3 4 true
list := []int{1, 2, 3, 4, 5}
result, index, found := FindLastIndexOf(list, func(n int) bool {
return n > 10
})
fmt.Printf("%d %d %t", result, index, found)
Output: 0 -1 false
FindOrElse searches for an element in a slice based on a predicate. Returns the element if found or a given fallback value otherwise. Play: https://go.dev/play/p/Eo7W0lvKTky
list := []int{1, 2, 3, 4, 5}
result := FindOrElse(list, -1, func(n int) bool {
return n > 10
})
fmt.Printf("%d", result)
Output: -1
list := []int{1, 2, 3, 4, 5}
result := FindOrElse(list, -1, func(n int) bool {
return n > 3
})
fmt.Printf("%d", result)
Output: 4
func FindUniques[T comparable, Slice ~[]T](collection Slice) Slice
FindUniques returns a slice with all the elements that appear in the collection only once. The order of result values is determined by the order they occur in the collection.
list := []int{1, 2, 2, 3, 3, 3, 4, 5}
result := FindUniques(list)
fmt.Printf("%v", result)
Output: [1 4 5]
func FindUniquesBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) Slice
FindUniquesBy returns a slice with all the elements that appear in the collection only once. The order of result values is determined by the order they occur in the slice. It accepts `iteratee` which is invoked for each element in the slice to generate the criterion by which uniqueness is computed.
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 25},
{Name: "David", Age: 30},
{Name: "Eve", Age: 35},
}
result := FindUniquesBy(users, func(user User) int {
return user.Age
})
fmt.Printf("%d", len(result))
Output: 1
First returns the first element of a collection and check for availability of the first element. Play: https://go.dev/play/p/ul45Z0y2EFO
list := []int{1, 2, 3, 4, 5}
result, found := First(list)
fmt.Printf("%d %t", result, found)
Output: 1 true
list := []int{}
result, found := First(list)
fmt.Printf("%d %t", result, found)
Output: 0 false
func FirstOr[T any](collection []T, fallback T) T
FirstOr returns the first element of a collection or the fallback value if empty. Play: https://go.dev/play/p/ul45Z0y2EFO
list := []int{1, 2, 3, 4, 5}
result := FirstOr(list, -1)
fmt.Printf("%d", result)
Output: 1
list := []int{}
result := FirstOr(list, -1)
fmt.Printf("%d", result)
Output: -1
func FirstOrEmpty[T any](collection []T) T
FirstOrEmpty returns the first element of a collection or zero value if empty. Play: https://go.dev/play/p/ul45Z0y2EFO
list := []int{1, 2, 3, 4, 5}
result := FirstOrEmpty(list)
fmt.Printf("%d", result)
Output: 1
list := []int{}
result := FirstOrEmpty(list)
fmt.Printf("%d", result)
Output: 0
FlatMap manipulates a slice and transforms and flattens it to a slice of another type. 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
list := []int64{1, 2, 3, 4}
result := FlatMap(list, func(nbr int64, index int) []string {
return []string{
strconv.FormatInt(nbr, 10), // base 10
strconv.FormatInt(nbr, 2), // base 2
}
})
fmt.Printf("%v", result)
Output: [1 1 2 10 3 11 4 100]
func FlatMapErr[T, R any](collection []T, transform func(item T, index int) ([]R, error)) ([]R, error)
FlatMapErr manipulates a slice and transforms and flattens it to a slice of another type. The transform function can either return a slice or a `nil`, and in the `nil` case no value is added to the final slice. It returns the first error returned by the transform function.
list := []int64{1, 2, 3, 4}
_, err := FlatMapErr(list, func(nbr int64, index int) ([]string, error) {
if nbr == 3 {
return nil, errors.New("number 3 is not allowed")
}
return []string{
strconv.FormatInt(nbr, 10), // base 10
strconv.FormatInt(nbr, 2), // base 2
}, nil
})
fmt.Printf("%v", err)
Output: number 3 is not allowed
func Flatten[T any, Slice ~[]T](collection []Slice) Slice
Flatten returns a slice a single level deep. See also: Concat Play: https://go.dev/play/p/rbp9ORaMpjw
list := [][]int{{0, 1, 2}, {3, 4, 5}}
result := Flatten(list)
fmt.Printf("%v", result)
Output: [0 1 2 3 4 5]
ForEach iterates over elements of collection and invokes callback for each element. Play: https://go.dev/play/p/oofyiUPRf8t
list := []int64{1, 2, 3, 4}
ForEach(list, func(x int64, _ int) {
fmt.Println(x)
})
Output: 1 2 3 4
ForEachWhile iterates over elements of collection and invokes predicate for each element collection return value decide to continue or break, like do while(). Play: https://go.dev/play/p/QnLGt35tnow
list := []int64{1, 2, -math.MaxInt, 4}
ForEachWhile(list, func(x int64, _ int) bool {
if x < 0 {
return false
}
fmt.Println(x)
return true
})
Output: 1 2
FromEntries transforms a slice of key/value pairs into a map. Play: https://go.dev/play/p/oIr5KHFGCEN
result := FromEntries([]Entry[string, int]{
{
Key: "foo",
Value: 1,
},
{
Key: "bar",
Value: 2,
},
{
Key: "baz",
Value: 3,
},
})
fmt.Printf("%v %v %v %v", len(result), result["foo"], result["bar"], result["baz"])
Output: 3 1 2 3
func FromSlicePtr[T any](collection []*T) []T
FromSlicePtr returns a slice with the pointer values. Returns a zero value in case of a nil pointer element. Play: https://go.dev/play/p/lbunFvzlUDX
Generator implements the generator design pattern. Play: https://go.dev/play/p/lIbSY3QmiEg
Deprecated: use "iter" package instead (Go >= 1.23).
func GroupBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) map[U]Slice
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
list := []int{0, 1, 2, 3, 4, 5}
result := GroupBy(list, func(i int) int {
return i % 3
})
fmt.Printf("%v\n", result[0])
fmt.Printf("%v\n", result[1])
fmt.Printf("%v\n", result[2])
Output: [0 3] [1 4] [2 5]
func GroupByErr[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) (U, error)) (map[U]Slice, error)
GroupByErr returns an object composed of keys generated from the results of running each element of collection through iteratee. It returns the first error returned by the iteratee function.
list := []int{0, 1, 2, 3, 4, 5}
_, err := GroupByErr(list, func(i int) (int, error) {
if i == 3 {
return 0, errors.New("number 3 is not allowed")
}
return i % 3, nil
})
fmt.Printf("%v", err)
Output: number 3 is not allowed
func GroupByMap[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K][]V
GroupByMap returns an object composed of keys generated from the results of running each element of collection through transform. Play: https://go.dev/play/p/iMeruQ3_W80
list := []int{0, 1, 2, 3, 4, 5}
result := GroupByMap(list, func(i int) (int, int) {
return i % 3, i * 2
})
fmt.Printf("%v\n", result[0])
fmt.Printf("%v\n", result[1])
fmt.Printf("%v\n", result[2])
Output: [0 6] [2 8] [4 10]
GroupByMapErr returns an object composed of keys generated from the results of running each element of collection through transform. It returns the first error returned by the transform function.
list := []int{0, 1, 2, 3, 4, 5}
_, err := GroupByMapErr(list, func(i int) (int, int, error) {
if i == 3 {
return 0, 0, errors.New("number 3 is not allowed")
}
return i % 3, i * 2, nil
})
fmt.Printf("%v", err)
Output: number 3 is not allowed
If is a single line if/else statement. Play: https://go.dev/play/p/WSw3ApMxhyW
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
result2 := If(false, 1).
ElseIf(true, 2).
Else(3)
result3 := If(false, 1).
ElseIf(false, 2).
Else(3)
result4 := IfF(true, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
result5 := IfF(false, func() int { return 1 }).
ElseIfF(true, func() int { return 2 }).
ElseF(func() int { return 3 })
result6 := IfF(false, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output: 1 2 3 1 2 3
IfF is a single line if/else statement whose options are functions. Play: https://go.dev/play/p/WSw3ApMxhyW
result1 := If(true, 1).
ElseIf(false, 2).
Else(3)
result2 := If(false, 1).
ElseIf(true, 2).
Else(3)
result3 := If(false, 1).
ElseIf(false, 2).
Else(3)
result4 := IfF(true, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
result5 := IfF(false, func() int { return 1 }).
ElseIfF(true, func() int { return 2 }).
ElseF(func() int { return 3 })
result6 := IfF(false, func() int { return 1 }).
ElseIfF(false, func() int { return 2 }).
ElseF(func() int { return 3 })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output: 1 2 3 1 2 3
IndexOf returns the index at which the first occurrence of a value is found in a slice or -1 if the value cannot be found. Play: https://go.dev/play/p/Eo7W0lvKTky
list := []string{"foo", "bar", "baz"}
result := IndexOf(list, "bar")
fmt.Printf("%d", result)
Output: 1
list := []string{"foo", "bar", "baz"}
result := IndexOf(list, "qux")
fmt.Printf("%d", result)
Output: -1
func Interleave[T any, Slice ~[]T](collections ...Slice) Slice
Interleave round-robin alternating input slices and sequentially appending value at index into result. Play: https://go.dev/play/p/-RJkTLQEDVt
list1 := [][]int{{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}
list2 := [][]int{{1}, {2, 5, 8}, {3, 6}, {4, 7, 9, 10}}
result1 := Interleave(list1...)
result2 := Interleave(list2...)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
Output: [1 2 3 4 5 6 7 8 9] [1 2 3 4 5 6 7 8 9 10]
func Intersect[T comparable, Slice ~[]T](lists ...Slice) Slice
Intersect returns the intersection between collections. Play: https://go.dev/play/p/uuElL9X9e58
result := Intersect([]int{0, 3, 5, 7}, []int{3, 5}, []int{0, 1, 2, 0, 3, 0})
fmt.Printf("%v", result)
Output: [3]
func IntersectBy[T any, K comparable, Slice ~[]T](transform func(T) K, lists ...Slice) Slice
IntersectBy returns the intersection between two collections using a custom key selector function.
result := IntersectBy(strconv.Itoa, []int{0, 6, 0, 3}, []int{0, 1, 2, 3, 4, 5}, []int{0, 6})
fmt.Printf("%v", result)
Output: [0]
Invert creates a map composed of the inverted keys and values. If map contains duplicate values, subsequent values overwrite property assignments of previous values. Play: https://go.dev/play/p/rFQ4rak6iA1
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := Invert(kv)
fmt.Printf("%v %v %v %v", len(result), result[1], result[2], result[3])
Output: 3 foo bar baz
IsSorted checks if a slice is sorted. Play: https://go.dev/play/p/mc3qR-t4mcx
list := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
result := IsSorted(list)
fmt.Printf("%v", result)
Output: true
IsSortedBy checks if a slice is sorted by iteratee.
list := []string{"a", "bb", "ccc"}
result := IsSortedBy(list, func(s string) int {
return len(s)
})
fmt.Printf("%v", result)
Output: true
IsSortedByKey checks if a slice is sorted by iteratee.
Deprecated: Use lo.IsSortedBy instead.
func KeyBy[K comparable, V any](collection []V, iteratee func(item V) K) map[K]V
KeyBy transforms a slice or a slice of structs to a map based on a pivot callback. Play: https://go.dev/play/p/ccUiUL_Lnel
list := []string{"a", "aa", "aaa"}
result := KeyBy(list, func(str string) int {
return len(str)
})
fmt.Printf("%v", result)
Output: map[1:a 2:aa 3:aaa]
KeyByErr transforms a slice or a slice of structs to a map based on a pivot callback to compute keys. Iteratee can return an error to stop iteration immediately. Play: https://go.dev/play/p/ccUiUL_Lnel
list := []string{"a", "aa", "aaa"}
_, err := KeyByErr(list, func(str string) (int, error) {
if str == "aa" {
return 0, errors.New("aa is not allowed")
}
return len(str), nil
})
fmt.Printf("%v", err)
Output: aa is not allowed
func Keyify[T comparable, Slice ~[]T](collection Slice) map[T]struct{}
Keyify returns a map with each unique element of the slice as a key. Play: https://go.dev/play/p/RYhhM_csqIG
list := []string{"a", "a", "b", "b", "d"}
set := Keyify(list)
_, ok1 := set["a"]
_, ok2 := set["c"]
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", set)
Output: true false map[a:{} b:{} d:{}]
Keys creates a slice of the map keys. Play: https://go.dev/play/p/Uu11fHASqrU
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"baz": 3}
result := Keys(kv, kv2)
sort.Strings(result)
fmt.Printf("%v", result)
Output: [bar baz foo]
Last returns the last element of a collection or error if empty. Play: https://go.dev/play/p/ul45Z0y2EFO
list := []int{1, 2, 3, 4, 5}
result, found := Last(list)
fmt.Printf("%d %t", result, found)
Output: 5 true
list := []int{}
result, found := Last(list)
fmt.Printf("%d %t", result, found)
Output: 0 false
LastIndexOf returns the index at which the last occurrence of a value is found in a slice or -1 if the value cannot be found. Play: https://go.dev/play/p/Eo7W0lvKTky
list := []string{"foo", "bar", "baz", "bar"}
result := LastIndexOf(list, "bar")
fmt.Printf("%d", result)
Output: 3
list := []string{"foo", "bar", "baz"}
result := LastIndexOf(list, "qux")
fmt.Printf("%d", result)
Output: -1
func LastOr[T any](collection []T, fallback T) T
LastOr returns the last element of a collection or the fallback value if empty. Play: https://go.dev/play/p/ul45Z0y2EFO
list := []int{1, 2, 3, 4, 5}
result := LastOr(list, -1)
fmt.Printf("%d", result)
Output: 5
list := []int{}
result := LastOr(list, -1)
fmt.Printf("%d", result)
Output: -1
func LastOrEmpty[T any](collection []T) T
LastOrEmpty returns the last element of a collection or zero value if empty. Play: https://go.dev/play/p/ul45Z0y2EFO
list := []int{1, 2, 3, 4, 5}
result := LastOrEmpty(list)
fmt.Printf("%d", result)
Output: 5
list := []int{}
result := LastOrEmpty(list)
fmt.Printf("%d", result)
Output: 0
Latest search the maximum time.Time of a collection. Returns zero value when the collection is empty.
now := time.Now()
past := now.Add(-time.Hour)
future := now.Add(time.Hour)
result := Latest(future, now, past)
fmt.Printf("%t", result.Equal(future))
Output: true
LatestBy search the maximum time.Time of a collection using the given iteratee function. Returns zero value when the collection is empty.
type Event struct {
Name string
Time time.Time
}
now := time.Now()
events := []Event{
{Name: "Event A", Time: now.Add(time.Hour)},
{Name: "Event B", Time: now},
{Name: "Event C", Time: now.Add(-time.Hour)},
}
result := LatestBy(events, func(event Event) time.Time {
return event.Time
})
fmt.Printf("%s", result.Name)
Output: Event A
LatestByErr search the maximum time.Time of a collection using the given iteratee function. Returns zero value and nil error when the collection is empty. If the iteratee returns an error, iteration stops and the error is returned.
type Event struct {
Name string
Time time.Time
Err error // Simulates error condition
}
now := time.Now()
events := []Event{
{Name: "Event A", Time: now.Add(time.Hour), Err: nil},
{Name: "Event B", Time: now, Err: errors.New("event b error")},
{Name: "Event C", Time: now.Add(-time.Hour), Err: nil},
}
_, err := LatestByErr(events, func(event Event) (time.Time, error) {
if event.Err != nil {
return time.Time{}, event.Err
}
return event.Time, nil
})
fmt.Printf("%v", err)
Output: event b error
Map manipulates a slice and transforms it to a slice of another type. Play: https://go.dev/play/p/OkPcYAhBo0D
list := []int64{1, 2, 3, 4}
result := Map(list, func(nbr int64, index int) string {
return strconv.FormatInt(nbr*2, 10)
})
fmt.Printf("%v", result)
Output: [2 4 6 8]
MapEntries manipulates map entries and transforms it to a map of another type. Play: https://go.dev/play/p/VuvNQzxKimT
kv := map[string]int{"foo": 1, "bar": 2}
result := MapEntries(kv, func(k string, v int) (int, string) {
return v, k
})
fmt.Printf("%v", result)
Output: map[1:foo 2:bar]
MapEntriesErr manipulates map entries and transforms it to a map of another type. It returns the first error returned by the iteratee.
kv := map[string]int{"foo": 1, "bar": 2}
_, err := MapEntriesErr(kv, func(k string, v int) (int, string, error) {
if k == "foo" {
return 0, "", errors.New("entry foo is not allowed")
}
return v, k, nil
})
fmt.Printf("%v", err)
Output: entry foo is not allowed
MapErr manipulates a slice and transforms it to a slice of another type. It returns the first error returned by the transform function.
list := []int64{1, 2, 3, 4}
_, err := MapErr(list, func(nbr int64, index int) (string, error) {
if nbr == 3 {
return "", errors.New("number 3 is not allowed")
}
return strconv.FormatInt(nbr*2, 10), nil
})
fmt.Printf("%v", err)
Output: number 3 is not allowed
MapKeys manipulates map keys and transforms it to a map of another type. Play: https://go.dev/play/p/9_4WPIqOetJ
kv := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}
result := MapKeys(kv, func(_, k int) string {
return strconv.FormatInt(int64(k), 10)
})
fmt.Printf("%v %v %v %v %v", len(result), result["1"], result["2"], result["3"], result["4"])
Output: 4 1 2 3 4
MapKeysErr manipulates map keys and transforms it to a map of another type. It returns the first error returned by the iteratee.
kv := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}
_, err := MapKeysErr(kv, func(_, k int) (string, error) {
if k == 3 {
return "", errors.New("key 3 is not allowed")
}
return strconv.FormatInt(int64(k), 10), nil
})
fmt.Printf("%v", err)
Output: key 3 is not allowed
func MapToSlice[K comparable, V, R any](in map[K]V, iteratee func(key K, value V) R) []R
MapToSlice transforms a map into a slice based on specified iteratee. Play: https://go.dev/play/p/ZuiCZpDt6LD
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}
result := MapToSlice(kv, func(k int, v int64) string {
return fmt.Sprintf("%d_%d", k, v)
})
sort.Strings(result)
fmt.Printf("%v", result)
Output: [1_1 2_2 3_3 4_4]
MapToSliceErr transforms a map into a slice based on specified iteratee. It returns the first error returned by the iteratee.
kv := map[int]int64{1: 1, 2: 2, 3: 3, 4: 4}
_, err := MapToSliceErr(kv, func(k int, v int64) (string, error) {
if k == 3 {
return "", errors.New("key 3 is not allowed")
}
return fmt.Sprintf("%d_%d", k, v), nil
})
fmt.Printf("%v", err)
Output: key 3 is not allowed
func MapValues[K comparable, V, R any](in map[K]V, iteratee func(value V, key K) R) map[K]R
MapValues manipulates map values and transforms it to a map of another type. Play: https://go.dev/play/p/T_8xAfvcf0W
kv := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}
result := MapValues(kv, func(v, _ int) string {
return strconv.FormatInt(int64(v), 10)
})
fmt.Printf("%v %q %q %q %q", len(result), result[1], result[2], result[3], result[4])
Output: 4 "1" "2" "3" "4"
MapValuesErr manipulates map values and transforms it to a map of another type. It returns the first error returned by the iteratee.
kv := map[int]int{1: 1, 2: 2, 3: 3, 4: 4}
_, err := MapValuesErr(kv, func(v, _ int) (string, error) {
if v == 3 {
return "", errors.New("value 3 is not allowed")
}
return strconv.FormatInt(int64(v), 10), nil
})
fmt.Printf("%v", err)
Output: value 3 is not allowed
Max searches the maximum value of a collection. Returns zero value when the collection is empty. Play: https://go.dev/play/p/r6e-Z8JozS8
list := []int{3, 1, 4, 1, 5, 9, 2, 6}
result := Max(list)
fmt.Printf("%d", result)
Output: 9
list := []int{}
result := Max(list)
fmt.Printf("%d", result)
Output: 0
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.
Note: the comparison function is inconsistent with most languages, since we use the opposite of the usual convention. See https://github.com/samber/lo/issues/129
Play: https://go.dev/play/p/JW1qu-ECwF7
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
result := MaxBy(users, func(a, b User) bool {
return a.Age > b.Age
})
fmt.Printf("%s", result.Name)
Output: Charlie
MaxByErr 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 and nil error when the collection is empty. If the comparison function returns an error, iteration stops and the error is returned.
Note: the comparison function is inconsistent with most languages, since we use the opposite of the usual convention. See https://github.com/samber/lo/issues/129
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
_, err := MaxByErr(users, func(a, b User) (bool, error) {
if b.Name == "Bob" {
return false, errors.New("bob is not allowed")
}
return a.Age > b.Age, nil
})
fmt.Printf("%v", err)
Output: bob is not allowed
MaxIndex searches the maximum value of a collection and the index of the maximum value. Returns (zero value, -1) when the collection is empty.
list := []int{3, 1, 4, 1, 5, 9, 2, 6}
result, index := MaxIndex(list)
fmt.Printf("%d %d", result, index)
Output: 9 5
list := []int{}
result, index := MaxIndex(list)
fmt.Printf("%d %d", result, index)
Output: 0 -1
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.
Note: the comparison function is inconsistent with most languages, since we use the opposite of the usual convention. See https://github.com/samber/lo/issues/129
Play: https://go.dev/play/p/uaUszc-c9QK
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
result, index := MaxIndexBy(users, func(a, b User) bool {
return a.Age > b.Age
})
fmt.Printf("%s %d", result.Name, index)
Output: Charlie 2
MaxIndexByErr 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, nil) when the collection is empty. If the comparison function returns an error, iteration stops and the error is returned.
Note: the comparison function is inconsistent with most languages, since we use the opposite of the usual convention. See https://github.com/samber/lo/issues/129
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
_, _, err := MaxIndexByErr(users, func(a, b User) (bool, error) {
if b.Name == "Bob" {
return false, errors.New("bob is not allowed")
}
return a.Age > b.Age, nil
})
fmt.Printf("%v", err)
Output: bob is not allowed
Mean calculates the mean of a collection of numbers. Play: https://go.dev/play/p/tPURSuteUsP
list := []int{1, 2, 3, 4, 5}
result := Mean(list)
fmt.Printf("%v", result)
Output: 3
MeanBy calculates the mean of a collection of numbers using the given return value from the iteration function. Play: https://go.dev/play/p/j7TsVwBOZ7P
list := []string{"foo", "bar"}
result := MeanBy(list, func(item string) int {
return len(item)
})
fmt.Printf("%v", result)
Output: 3
MeanByErr calculates the mean of a collection of numbers using the given return value from the iteration function. If the iteratee returns an error, iteration stops and the error is returned. If collection is empty 0 and nil error are returned.
list := []string{"foo", "bar", "baz"}
_, err := MeanByErr(list, func(item string) (int, error) {
if item == "bar" {
return 0, errors.New("bar is not allowed")
}
return len(item), nil
})
fmt.Printf("%v", err)
Output: bar is not allowed
Min search the minimum value of a collection. Returns zero value when the collection is empty. Play: https://go.dev/play/p/r6e-Z8JozS8
list := []int{3, 1, 4, 1, 5, 9, 2, 6}
result := Min(list)
fmt.Printf("%d", result)
Output: 1
list := []int{}
result := Min(list)
fmt.Printf("%d", result)
Output: 0
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.
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
result := MinBy(users, func(a, b User) bool {
return a.Age < b.Age
})
fmt.Printf("%s", result.Name)
Output: Alice
MinByErr 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 and nil error when the collection is empty. If the comparison function returns an error, iteration stops and the error is returned.
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
result, err := MinByErr(users, func(a, b User) (bool, error) {
if a.Name == "Bob" {
return false, errors.New("bob is not allowed")
}
return a.Age < b.Age, nil
})
fmt.Printf("%v %v", result.Name, err)
Output: bob is not allowed
MinIndex search the minimum value of a collection and the index of the minimum value. Returns (zero value, -1) when the collection is empty.
list := []int{3, 1, 4, 1, 5, 9, 2, 6}
result, index := MinIndex(list)
fmt.Printf("%d %d", result, index)
Output: 1 1
list := []int{}
result, index := MinIndex(list)
fmt.Printf("%d %d", result, index)
Output: 0 -1
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.
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
result, index := MinIndexBy(users, func(a, b User) bool {
return a.Age < b.Age
})
fmt.Printf("%s %d", result.Name, index)
Output: Alice 0
MinIndexByErr 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. Comparison function can return an error to stop iteration immediately.
type User struct {
Name string
Age int
}
users := []User{
{Name: "Alice", Age: 25},
{Name: "Bob", Age: 30},
{Name: "Charlie", Age: 35},
}
result, _, err := MinIndexByErr(users, func(a, b User) (bool, error) {
if a.Name == "Bob" {
return false, errors.New("bob is not allowed")
}
return a.Age < b.Age, nil
})
fmt.Printf("%v %v", result.Name, err)
Output: bob is not allowed
Mode returns the mode (most frequent value) of a collection. If multiple values have the same highest frequency, then multiple values are returned. If the collection is empty, then the zero value of T is returned.
Must is a helper that wraps a call to a function returning a value and an error and panics if err is error or false. Play: https://go.dev/play/p/fOqtX5HudtN
defer func() {
_ = recover()
}()
// won't panic
Must(42, nil)
// won't panic
cb := func() (int, error) {
return 42, nil
}
Must(cb())
// will panic
Must(42, errors.New("my error"))
// will panic with error message
Must(42, errors.New("world"), "hello")
Must0 has the same behavior as Must, but callback returns no variable. Play: https://go.dev/play/p/TMoWrRp3DyC
defer func() {
_ = recover()
}()
// won't panic
Must0(nil)
// will panic
Must0(errors.New("my error"))
// will panic with error message
Must0(errors.New("world"), "hello")
Must1 is an alias to Must. Play: https://go.dev/play/p/TMoWrRp3DyC
defer func() {
_ = recover()
}()
// won't panic
Must1(42, nil)
// won't panic
cb := func() (int, error) {
return 42, nil
}
Must1(cb())
// will panic
Must1(42, errors.New("my error"))
// will panic with error message
Must1(42, errors.New("world"), "hello")
Must2 has the same behavior as Must, but callback returns 2 variables. Play: https://go.dev/play/p/TMoWrRp3DyC
defer func() {
_ = recover()
}()
// won't panic
Must2(42, "hello", nil)
// will panic
Must2(42, "hello", errors.New("my error"))
// will panic with error message
Must2(42, "hello", errors.New("world"), "hello")
Must3 has the same behavior as Must, but callback returns 3 variables. Play: https://go.dev/play/p/TMoWrRp3DyC
defer func() {
_ = recover()
}()
// won't panic
Must3(42, "hello", 4.2, nil)
// will panic
Must3(42, "hello", 4.2, errors.New("my error"))
// will panic with error message
Must3(42, "hello", 4.2, errors.New("world"), "hello")
func Must4[T1, T2, T3, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err any, messageArgs ...any) (T1, T2, T3, T4)
Must4 has the same behavior as Must, but callback returns 4 variables. Play: https://go.dev/play/p/TMoWrRp3DyC
defer func() {
_ = recover()
}()
// won't panic
Must4(42, "hello", 4.2, true, nil)
// will panic
Must4(42, "hello", 4.2, true, errors.New("my error"))
// will panic with error message
Must4(42, "hello", 4.2, true, errors.New("world"), "hello")
func Must5[T1, T2, T3, T4, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, err any, messageArgs ...any) (T1, T2, T3, T4, T5)
Must5 has the same behavior as Must, but callback returns 5 variables. Play: https://go.dev/play/p/TMoWrRp3DyC
defer func() {
_ = recover()
}()
// won't panic
Must5(42, "hello", 4.2, true, foo{}, nil)
// will panic
Must5(42, "hello", 4.2, true, foo{}, errors.New("my error"))
// will panic with error message
Must5(42, "hello", 4.2, true, foo{}, errors.New("world"), "hello")
func Must6[T1, T2, T3, T4, T5, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, val6 T6, err any, messageArgs ...any) (T1, T2, T3, T4, T5, T6)
Must6 has the same behavior as Must, but callback returns 6 variables. Play: https://go.dev/play/p/TMoWrRp3DyC
defer func() {
_ = recover()
}()
// won't panic
Must5(42, "hello", 4.2, true, foo{}, "foobar", nil)
// will panic
Must5(42, "hello", 4.2, true, foo{}, "foobar", errors.New("my error"))
// will panic with error message
Must5(42, "hello", 4.2, true, foo{}, "foobar", errors.New("world"), "hello")
NewDebounce creates a debounced instance that delays invoking functions given until after wait milliseconds have elapsed. Play: https://go.dev/play/p/mz32VMK2nqe
i := int32(0)
calls := []int32{}
mu := sync.Mutex{}
debounce, cancel := NewDebounce(time.Millisecond, func() {
mu.Lock()
defer mu.Unlock()
calls = append(calls, atomic.LoadInt32(&i))
})
debounce()
atomic.AddInt32(&i, 1)
time.Sleep(5 * time.Millisecond)
debounce()
atomic.AddInt32(&i, 1)
debounce()
atomic.AddInt32(&i, 1)
debounce()
atomic.AddInt32(&i, 1)
time.Sleep(5 * time.Millisecond)
cancel()
mu.Lock()
fmt.Printf("%v", calls)
mu.Unlock()
Output: [1 4]
NewDebounceBy creates a debounced instance for each distinct key, that delays invoking functions given until after wait milliseconds have elapsed. Play: https://go.dev/play/p/d3Vpt6pxhY8
calls := map[string][]int{}
mu := sync.Mutex{}
debounce, cancel := NewDebounceBy(time.Millisecond, func(userID string, count int) {
mu.Lock()
defer mu.Unlock()
if _, ok := calls[userID]; !ok {
calls[userID] = []int{}
}
calls[userID] = append(calls[userID], count)
})
debounce("samuel")
debounce("john")
time.Sleep(5 * time.Millisecond)
debounce("john")
debounce("john")
debounce("samuel")
debounce("john")
time.Sleep(5 * time.Millisecond)
cancel("samuel")
cancel("john")
mu.Lock()
fmt.Printf("samuel: %v\n", calls["samuel"])
fmt.Printf("john: %v\n", calls["john"])
mu.Unlock()
Output: samuel: [1 1] john: [1 3]
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
throttle, reset := NewThrottle(100*time.Millisecond, func() {
fmt.Println("Called once in every 100ms")
})
for j := 0; j < 10; j++ {
throttle()
time.Sleep(30 * time.Millisecond)
}
reset()
Output: Called once in every 100ms Called once in every 100ms Called once in every 100ms
func NewThrottleBy[T comparable](interval time.Duration, f ...func(key T)) (throttle func(key T), reset func())
NewThrottleBy 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/0Wv6oX7dHdC
throttle, reset := NewThrottleBy(100*time.Millisecond, func(key string) {
fmt.Println(key, "Called once in every 100ms")
})
for j := 0; j < 10; j++ {
throttle("foo")
throttle("bar")
time.Sleep(30 * time.Millisecond)
}
reset()
Output: foo Called once in every 100ms bar Called once in every 100ms foo Called once in every 100ms bar Called once in every 100ms foo Called once in every 100ms bar Called once in every 100ms
func NewThrottleByWithCount[T comparable](interval time.Duration, count int, f ...func(key T)) (throttle func(key T), reset func())
NewThrottleByWithCount is NewThrottleBy with count limit, throttled function will be invoked count times in every interval. Play: https://go.dev/play/p/vQk3ECH7_EW
throttle, reset := NewThrottleByWithCount(100*time.Millisecond, 2, func(key string) {
fmt.Println(key, "Called once in every 100ms")
})
for j := 0; j < 10; j++ {
throttle("foo")
throttle("bar")
time.Sleep(30 * time.Millisecond)
}
reset()
Output: foo Called once in every 100ms bar Called once in every 100ms foo Called once in every 100ms bar Called once in every 100ms foo Called once in every 100ms bar Called once in every 100ms foo Called once in every 100ms bar Called once in every 100ms foo Called once in every 100ms bar Called once in every 100ms foo Called once in every 100ms bar Called once in every 100ms
NewThrottleWithCount is NewThrottle with count limit, throttled function will be invoked count times in every interval. Play: https://go.dev/play/p/w5nc0MgWtjC
throttle, reset := NewThrottleWithCount(100*time.Millisecond, 2, func() {
fmt.Println("Called once in every 100ms")
})
for j := 0; j < 10; j++ {
throttle()
time.Sleep(30 * time.Millisecond)
}
reset()
Output: Called once in every 100ms Called once in every 100ms Called once in every 100ms Called once in every 100ms Called once in every 100ms Called once in every 100ms
NoneBy returns true if the predicate returns true for none of the elements in the collection or if the collection is empty. Play: https://go.dev/play/p/O64WZ32H58S
Nth returns the element at index `nth` of collection. If `nth` is negative, the nth element from the end is returned. An error is returned when nth is out of slice bounds. Play: https://go.dev/play/p/sHoh88KWt6B
list := []int{1, 2, 3, 4, 5}
result, err := Nth(list, 2)
fmt.Printf("%d %v", result, err)
Output: 3 <nil>
list := []int{1, 2, 3, 4, 5}
result, err := Nth(list, -2)
fmt.Printf("%d %v", result, err)
Output: 4 <nil>
list := []int{1, 2, 3, 4, 5}
result, err := Nth(list, 10)
fmt.Printf("%d %v", result, err)
Output: 0 nth: 10 out of slice bounds
NthOr returns the element at index `nth` of collection. If `nth` is negative, it returns the nth element from the end. If `nth` is out of slice bounds, it returns the fallback value instead of an error. Play: https://go.dev/play/p/sHoh88KWt6B
list := []int{1, 2, 3, 4, 5}
result := NthOr(list, 2, -1)
fmt.Printf("%d", result)
Output: 3
list := []int{1, 2, 3, 4, 5}
result := NthOr(list, 10, -1)
fmt.Printf("%d", result)
Output: -1
NthOrEmpty returns the element at index `nth` of collection. If `nth` is negative, it returns the nth element from the end. If `nth` is out of slice bounds, it returns the zero value (empty value) for that type. Play: https://go.dev/play/p/sHoh88KWt6B
list := []int{1, 2, 3, 4, 5}
result := NthOrEmpty(list, 2)
fmt.Printf("%d", result)
Output: 3
list := []int{1, 2, 3, 4, 5}
result := NthOrEmpty(list, 10)
fmt.Printf("%d", result)
Output: 0
func OmitBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map
OmitBy returns same map type filtered by given predicate. Play: https://go.dev/play/p/EtBsR43bdsd
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := OmitBy(kv, func(key string, value int) bool {
return value%2 == 1
})
fmt.Printf("%v", result)
Output: map[bar:2]
OmitByErr returns same map type filtered by given predicate. It returns the first error returned by the predicate.
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
_, err := OmitByErr(kv, func(key string, value int) (bool, error) {
if key == "bar" {
return false, errors.New("key bar is not allowed")
}
return value%2 == 1, nil
})
fmt.Printf("%v", err)
Output: key bar is not allowed
func OmitByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map
OmitByKeys returns same map type filtered by given keys. Play: https://go.dev/play/p/t1QjCrs-ysk
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := OmitByKeys(kv, []string{"foo", "baz"})
fmt.Printf("%v", result)
Output: map[bar:2]
func OmitByValues[K, V comparable, Map ~map[K]V](in Map, values []V) Map
OmitByValues returns same map type filtered by given values. Play: https://go.dev/play/p/9UYZi-hrs8j
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := OmitByValues(kv, []int{1, 3})
fmt.Printf("%v", result)
Output: map[bar:2]
func Partial[T1, T2, R any](f func(a T1, b T2) R, arg1 T1) func(T2) R
Partial returns new function that, when called, has its first argument set to the provided value. Play: https://go.dev/play/p/Sy1gAQiQZ3v
func Partial1[T1, T2, R any](f func(T1, T2) R, arg1 T1) func(T2) R
Partial1 returns new function that, when called, has its first argument set to the provided value. Play: https://go.dev/play/p/D-ASTXCLBzw
func Partial2[T1, T2, T3, R any](f func(T1, T2, T3) R, arg1 T1) func(T2, T3) R
Partial2 returns new function that, when called, has its first argument set to the provided value. Play: https://go.dev/play/p/-xiPjy4JChJ
func Partial3[T1, T2, T3, T4, R any](f func(T1, T2, T3, T4) R, arg1 T1) func(T2, T3, T4) R
Partial3 returns new function that, when called, has its first argument set to the provided value. Play: https://go.dev/play/p/zWtSutpI26m
func Partial4[T1, T2, T3, T4, T5, R any](f func(T1, T2, T3, T4, T5) R, arg1 T1) func(T2, T3, T4, T5) R
Partial4 returns new function that, when called, has its first argument set to the provided value. Play: https://go.dev/play/p/kBrnnMTcJm0
func Partial5[T1, T2, T3, T4, T5, T6, R any](f func(T1, T2, T3, T4, T5, T6) R, arg1 T1) func(T2, T3, T4, T5, T6) R
Partial5 returns new function that, when called, has its first argument set to the provided value. Play: https://go.dev/play/p/7Is7K2y_VC3
func PartitionBy[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) K) []Slice
PartitionBy returns a slice of elements split into groups. The order of grouped values is 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
list := []int{-2, -1, 0, 1, 2, 3, 4}
result := PartitionBy(list, func(x int) string {
if x < 0 {
return "negative"
} else if x%2 == 0 {
return "even"
}
return "odd"
})
for _, item := range result {
fmt.Printf("%v\n", item)
}
Output: [-2 -1] [0 2 4] [1 3]
func PartitionByErr[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) (K, error)) ([]Slice, error)
PartitionByErr partitions a slice into groups determined by a key computed from each element. The order of the partitions is determined by the order they occur in collection. The grouping is generated from the results of running each element of collection through iteratee. It returns the first error returned by the iteratee function.
list := []int{-2, -1, 0, 1, 2, 3, 4}
_, err := PartitionByErr(list, func(x int) (string, error) {
if x == 0 {
return "", errors.New("zero is not allowed")
}
if x < 0 {
return "negative", nil
} else if x%2 == 0 {
return "even", nil
}
return "odd", nil
})
fmt.Printf("%v", err)
Output: zero is not allowed
func PickBy[K comparable, V any, Map ~map[K]V](in Map, predicate func(key K, value V) bool) Map
PickBy returns same map type filtered by given predicate. Play: https://go.dev/play/p/kdg8GR_QMmf
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := PickBy(kv, func(key string, value int) bool {
return value%2 == 1
})
fmt.Printf("%v %v %v", len(result), result["foo"], result["baz"])
Output: 2 1 3
PickByErr returns same map type filtered by given predicate. It returns the first error returned by the predicate.
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
_, err := PickByErr(kv, func(key string, value int) (bool, error) {
if key == "bar" {
return false, errors.New("key bar is not allowed")
}
return value%2 == 1, nil
})
fmt.Printf("%v", err)
Output: key bar is not allowed
func PickByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map
PickByKeys returns same map type filtered by given keys. Play: https://go.dev/play/p/R1imbuci9qU
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := PickByKeys(kv, []string{"foo", "baz"})
fmt.Printf("%v %v %v", len(result), result["foo"], result["baz"])
Output: 2 1 3
func PickByValues[K, V comparable, Map ~map[K]V](in Map, values []V) Map
PickByValues returns same map type filtered by given values. Play: https://go.dev/play/p/1zdzSvbfsJc
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := PickByValues(kv, []int{1, 3})
fmt.Printf("%v %v %v", len(result), result["foo"], result["baz"])
Output: 2 1 3
Product gets the product of the values in a collection. If collection is empty 1 is returned. Play: https://go.dev/play/p/2_kjM_smtAH
list := []int{1, 2, 3, 4, 5}
result := Product(list)
fmt.Printf("%v", result)
Output: 120
ProductBy summarizes the values in a collection using the given return value from the iteration function. If collection is empty 1 is returned. Play: https://go.dev/play/p/wadzrWr9Aer
list := []string{"foo", "bar"}
result := ProductBy(list, func(item string) int {
return len(item)
})
fmt.Printf("%v", result)
Output: 9
ProductByErr summarizes the values in a collection using the given return value from the iteration function. If the iteratee returns an error, iteration stops and the error is returned. If collection is empty 1 and nil error are returned.
list := []string{"foo", "bar", "baz"}
_, err := ProductByErr(list, func(item string) (int, error) {
if item == "bar" {
return 0, errors.New("bar is not allowed")
}
return len(item), nil
})
fmt.Printf("%v", err)
Output: bar is not allowed
Range creates a slice of numbers (positive and/or negative) with given length. Play: https://go.dev/play/p/0r6VimXAi9H
result1 := Range(4)
result2 := Range(-4)
result3 := RangeFrom(1, 5)
result4 := RangeFrom(1.0, 5)
result5 := RangeWithSteps(0, 20, 5)
result6 := RangeWithSteps[float32](-1.0, -4.0, -1.0)
result7 := RangeWithSteps(1, 4, -1)
result8 := Range(0)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
fmt.Printf("%v\n", result7)
fmt.Printf("%v\n", result8)
Output: [0 1 2 3] [0 -1 -2 -3] [1 2 3 4 5] [1 2 3 4 5] [0 5 10 15] [-1 -2 -3] [] []
RangeWithSteps creates a slice of numbers (positive and/or negative) progressing from start up to, but not including end. step set to zero will return an empty slice. Play: https://go.dev/play/p/0r6VimXAi9H
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
list := []int64{1, 2, 3, 4}
result := Reduce(list, func(agg, item int64, index int) int64 {
return agg + item
}, 0)
fmt.Printf("%v", result)
Output: 10
func ReduceErr[T, R any](collection []T, accumulator func(agg R, item T, index int) (R, error), initial R) (R, error)
ReduceErr 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. It returns the first error returned by the accumulator function.
list := []int64{1, 2, 3, 4}
_, err := ReduceErr(list, func(agg, item int64, index int) (int64, error) {
if item == 3 {
return 0, errors.New("number 3 is not allowed")
}
return agg + item, nil
}, 0)
fmt.Printf("%v", err)
Output: number 3 is not allowed
func ReduceRight[T, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R
ReduceRight is like Reduce except that it iterates over elements of collection from right to left. Play: https://go.dev/play/p/Fq3W70l7wXF
list := [][]int{{0, 1}, {2, 3}, {4, 5}}
result := ReduceRight(list, func(agg, item []int, index int) []int {
return append(agg, item...)
}, []int{})
fmt.Printf("%v", result)
Output: [4 5 2 3 0 1]
func ReduceRightErr[T, R any](collection []T, accumulator func(agg R, item T, index int) (R, error), initial R) (R, error)
ReduceRightErr is like ReduceRight except that the accumulator function can return an error. It returns the first error returned by the accumulator function.
list := [][]int{{0, 1}, {2, 3}, {4, 5}}
_, err := ReduceRightErr(list, func(agg, item []int, index int) ([]int, error) {
if index == 0 {
return nil, errors.New("index 0 is not allowed")
}
return append(agg, item...), nil
}, []int{})
fmt.Printf("%v", err)
Output: index 0 is not allowed
Reject is the opposite of Filter, this method returns the elements of collection that predicate does not return true for. Play: https://go.dev/play/p/pFCF5WVB225
list := []int{0, 1, 2, 3, 4, 5}
result := Reject(list, func(x, _ int) bool {
return x%2 == 0
})
fmt.Printf("%v", result)
Output: [1 3 5]
func RejectErr[T any, Slice ~[]T](collection Slice, predicate func(item T, index int) (bool, error)) (Slice, error)
RejectErr is the opposite of FilterErr, this method returns the elements of collection that predicate does not return true for. If the predicate returns an error, iteration stops immediately and returns the error. Play: https://go.dev/play/p/pFCF5WVB225
list := []int64{0, 1, 2, 3, 4, 5}
result, err := RejectErr(list, func(x int64, index int) (bool, error) {
if x == 3 {
return false, errors.New("number 3 is not allowed")
}
return x%2 == 0, nil
})
fmt.Printf("%v, %v\n", result, err)
result, err = RejectErr([]int64{0, 1, 2, 4, 6}, func(x int64, index int) (bool, error) {
return x%2 == 0, nil
})
fmt.Printf("%v, %v\n", result, err)
Output: [], number 3 is not allowed [1], <nil>
RejectMap is the opposite of FilterMap, this method returns a slice obtained after both filtering and mapping using the given callback function. The callback function should return two values:
- the result of the mapping operation and
- whether the result element should be included or not.
Repeat builds a slice with N copies of initial value. Play: https://go.dev/play/p/g3uHXbmc3b6
result := Repeat(2, foo{"a"})
fmt.Printf("%v", result)
Output: [{a} {a}]
RepeatBy builds a slice with values returned by N calls of callback. Play: https://go.dev/play/p/ozZLCtX_hNU
result := RepeatBy(5, func(i int) string {
return strconv.FormatInt(int64(math.Pow(float64(i), 2)), 10)
})
fmt.Printf("%v", result)
Output: [0 1 4 9 16]
RepeatByErr builds a slice with values returned by N calls of callback. It returns the first error returned by the callback function.
result, err := RepeatByErr(5, func(i int) (string, error) {
if i == 3 {
return "", errors.New("index 3 is not allowed")
}
return fmt.Sprintf("item-%d", i), nil
})
fmt.Printf("%d %v", len(result), err)
Output: 0 index 3 is not allowed
func Replace[T comparable, Slice ~[]T](collection Slice, old, nEw T, n 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
list := []int{0, 1, 0, 1, 2, 3, 0}
result := Replace(list, 0, 42, 1)
fmt.Printf("%v\n", result)
result = Replace(list, -1, 42, 1)
fmt.Printf("%v\n", result)
result = Replace(list, 0, 42, 2)
fmt.Printf("%v\n", result)
result = Replace(list, 0, 42, -1)
fmt.Printf("%v\n", result)
Output: [42 1 0 1 2 3 0] [0 1 0 1 2 3 0] [42 1 42 1 2 3 0] [42 1 42 1 2 3 42]
func ReplaceAll[T comparable, Slice ~[]T](collection Slice, old, nEw T) Slice
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 Reverse[T any, Slice ~[]T](collection Slice) Slice
Reverse reverses a slice so that the first element becomes the last, the second element becomes the second to last, and so on. Play: https://go.dev/play/p/iv2e9jslfBM
Deprecated: use mutable.Reverse() instead.
list := []int{0, 1, 2, 3, 4, 5}
result := Reverse(list)
fmt.Printf("%v", result)
Output: [5 4 3 2 1 0]
RuneLength is an alias to utf8.RuneCountInString which returns the number of runes in string. Play: https://go.dev/play/p/tuhgW_lWY8l
result1, chars1 := RuneLength("hellô"), len("hellô")
result2, chars2 := RuneLength("🤘"), len("🤘")
fmt.Printf("%v %v\n", result1, chars1)
fmt.Printf("%v %v\n", result2, chars2)
Output: 5 6 1 4
func SampleBy[T any](collection []T, randomIntGenerator randomIntGenerator) T
SampleBy returns a random item from collection, using randomIntGenerator as the random index generator. Play: https://go.dev/play/p/HDmKmMgq0XN
func SamplesBy[T any, Slice ~[]T](collection Slice, count int, randomIntGenerator randomIntGenerator) Slice
SamplesBy returns N random unique items from collection, using randomIntGenerator as the random index generator. Play: https://go.dev/play/p/HDmKmMgq0XN
func Shuffle[T any, Slice ~[]T](collection Slice) Slice
Shuffle returns a slice of shuffled values. Uses the Fisher-Yates shuffle algorithm. Play: https://go.dev/play/p/ZTGG7OUCdnp
Deprecated: use mutable.Shuffle() instead.
list := []int{0, 1, 2, 3, 4, 5}
result := Shuffle(list)
fmt.Printf("%v", result)
Slice returns 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
list := []int{0, 1, 2, 3, 4, 5}
result := Slice(list, 1, 4)
fmt.Printf("%v\n", result)
result = Slice(list, 4, 1)
fmt.Printf("%v\n", result)
result = Slice(list, 4, 5)
fmt.Printf("%v\n", result)
Output: [1 2 3] [] [4]
func SliceToMap[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V
SliceToMap returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs 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 slice. Alias of Associate(). Play: https://go.dev/play/p/WHa2CfMO3Lr
list := []string{"a", "aa", "aaa"}
result := SliceToMap(list, func(str string) (string, int) {
return str, len(str)
})
fmt.Printf("%v", result)
Output: map[a:1 aa:2 aaa:3]
func SliceToMapI[T any, K comparable, V any](collection []T, transform func(item T, index int) (K, V)) map[K]V
SliceToMapI returns a map containing key-value pairs provided by transform function applied to elements of the given slice. If any of two pairs 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 slice. Alias of AssociateI(). Play: https://go.dev/play/p/mMBm5GV3_eq
Sliding creates a slice of sliding windows of a given size with a given step. If step is equal to size, windows don't overlap (similar to Chunk). If step is less than size, windows overlap.
list := []int{1, 2, 3, 4, 5, 6, 7, 8}
// Overlapping windows (step < size)
result1 := Sliding(list, 3, 1)
fmt.Printf("Overlapping: %v\n", result1)
// Non-overlapping windows (step == size, like Chunk)
result2 := Sliding(list, 3, 3)
fmt.Printf("Non-overlapping: %v\n", result2)
// Step > size (skipping elements)
result3 := Sliding(list, 2, 3)
fmt.Printf("With step: %v\n", result3)
Output: Overlapping: [[1 2 3] [2 3 4] [3 4 5] [4 5 6] [5 6 7] [6 7 8]] Non-overlapping: [[1 2 3] [4 5 6]] With step: [[1 2] [4 5] [7 8]]
SomeBy returns true if the predicate returns true for any of the elements in the collection. If the collection is empty SomeBy returns false. Play: https://go.dev/play/p/DXF-TORBudx
Splice inserts multiple elements at index i. A negative index counts back from the end of the slice. The helper is protected against overflow errors. Play: https://go.dev/play/p/G5_GhkeSUBA
Subset returns a copy of a slice from `offset` up to `length` elements. Like `slice[start:start+length]`, but does not panic on overflow. Play: https://go.dev/play/p/tOQu1GhFcog
list := []int{0, 1, 2, 3, 4, 5}
result := Subset(list, 2, 3)
fmt.Printf("%v", result)
Output: [2 3 4]
Substring extracts a substring from a string with Unicode character (rune) awareness. offset - starting position of the substring (can be positive, negative, or zero) length - number of characters to extract With positive offset, counting starts from the beginning of the string With negative offset, counting starts from the end of the string Play: https://go.dev/play/p/TQlxQi82Lu1
result1 := Substring("hello", 2, 3)
result2 := Substring("hello", -4, 3)
result3 := Substring("hello", -2, math.MaxUint)
result4 := Substring("🏠🐶🐱", 0, 2)
result5 := Substring("你好,世界", 0, 3)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
Output: llo ell lo 🏠🐶 你好,
Sum sums the values in a collection. If collection is empty 0 is returned. Play: https://go.dev/play/p/upfeJVqs4Bt
list := []int{1, 2, 3, 4, 5}
sum := Sum(list)
fmt.Printf("%v", sum)
Output: 15
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
list := []string{"foo", "bar"}
result := SumBy(list, func(item string) int {
return len(item)
})
fmt.Printf("%v", result)
Output: 6
SumByErr summarizes the values in a collection using the given return value from the iteration function. If the iteratee returns an error, iteration stops and the error is returned. If collection is empty 0 and nil error are returned.
list := []string{"foo", "bar", "baz"}
_, err := SumByErr(list, func(item string) (int, error) {
if item == "bar" {
return 0, errors.New("bar is not allowed")
}
return len(item), nil
})
fmt.Printf("%v", err)
Output: bar is not allowed
Switch is a pure functional switch/case/default statement. Play: https://go.dev/play/p/TGbKUMAeRUd
result1 := Switch[int, string](1).
Case(1, "1").
Case(2, "2").
Default("3")
result2 := Switch[int, string](2).
Case(1, "1").
Case(2, "2").
Default("3")
result3 := Switch[int, string](42).
Case(1, "1").
Case(2, "2").
Default("3")
result4 := Switch[int, string](1).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result5 := Switch[int, string](2).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
result6 := Switch[int, string](42).
CaseF(1, func() string { return "1" }).
CaseF(2, func() string { return "2" }).
DefaultF(func() string { return "3" })
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
Output: 1 2 3 1 2 3
Take takes the first n elements from a slice.
list := []int{0, 1, 2, 3, 4, 5}
result := Take(list, 3)
fmt.Printf("%v", result)
Output: [0 1 2]
func TakeFilter[T any, Slice ~[]T](collection Slice, n int, predicate func(item T, index int) bool) Slice
TakeFilter filters elements and takes the first n elements that match the predicate. Equivalent to calling Take(Filter(...)), but more efficient as it stops after finding n matches.
list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
result := TakeFilter(
list, 3, func(val, index int) bool {
return val%2 == 0
},
)
fmt.Printf("%v", result)
Output: [2 4 6]
TakeWhile takes elements from the beginning of a slice while the predicate returns true.
list := []int{0, 1, 2, 3, 4, 5}
result := TakeWhile(list, func(val int) bool {
return val < 3
})
fmt.Printf("%v", result)
Output: [0 1 2]
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
result := Ternary(true, "a", "b")
fmt.Printf("%v", result)
Output: a
TernaryF is a single line if/else statement whose options are functions. Play: https://go.dev/play/p/AO4VW20JoqM
result := TernaryF(true, func() string { return "a" }, func() string { return "b" })
fmt.Printf("%v", result)
Output: a
Times invokes the iteratee n times, returning a slice of the results of each invocation. The iteratee is invoked with index as argument. Play: https://go.dev/play/p/vgQj3Glr6lT
result := Times(3, func(i int) string {
return strconv.FormatInt(int64(i), 10)
})
fmt.Printf("%v", result)
Output: [0 1 2]
func Trim[T comparable, Slice ~[]T](collection, cutset Slice) Slice
Trim removes all the leading and trailing cutset from the collection. Play: https://go.dev/play/p/1an9mxLdRG5
collection := []int{0, 1, 2, 0, 3, 0}
// Test with valid cutset
result := Trim(collection, []int{0})
fmt.Printf("Trim with cutset {0}: %v\n", result)
// Test with string collection
words := []string{" hello ", "world", " "}
result2 := Trim(words, []string{" "})
fmt.Printf("Trim with string cutset: %v\n", result2)
// Test with no cutset elements
result3 := Trim(collection, []int{5})
fmt.Printf("Trim with cutset {5} (not present): %v\n", result3)
Output: Trim with cutset {0}: [1 2 0 3] Trim with string cutset: [ hello world ] Trim with cutset {5} (not present): [0 1 2 0 3 0]
func TrimLeft[T comparable, Slice ~[]T](collection, cutset Slice) Slice
TrimLeft removes all the leading cutset from the collection. Play: https://go.dev/play/p/74aqfAYLmyi
collection := []int{0, 1, 2, 0, 3, 0}
// Test with valid cutset
result := TrimLeft(collection, []int{0})
fmt.Printf("TrimLeft with cutset {0}: %v\n", result)
// Test with string collection
words := []string{" hello ", "world", " "}
result2 := TrimLeft(words, []string{" "})
fmt.Printf("TrimLeft with string cutset: %v\n", result2)
// Test with no cutset elements
result3 := TrimLeft(collection, []int{5})
fmt.Printf("TrimLeft with cutset {5} (not present): %v\n", result3)
Output: TrimLeft with cutset {0}: [1 2 0 3 0] TrimLeft with string cutset: [ hello world ] TrimLeft with cutset {5} (not present): [0 1 2 0 3 0]
func TrimPrefix[T comparable, Slice ~[]T](collection, prefix Slice) Slice
TrimPrefix removes all the leading prefix from the collection. Play: https://go.dev/play/p/SHO6X-YegPg
collection := []int{1, 2, 1, 2, 3}
// Test with valid prefix
result := TrimPrefix(collection, []int{1, 2})
fmt.Printf("TrimPrefix with prefix {1,2}: %v\n", result)
// Test with string collection
words := []string{"hello", "hello", "world"}
result2 := TrimPrefix(words, []string{"hello"})
fmt.Printf("TrimPrefix with string prefix: %v\n", result2)
// Test with prefix not present
result3 := TrimPrefix(collection, []int{5, 6})
fmt.Printf("TrimPrefix with prefix {5,6} (not present): %v\n", result3)
Output: TrimPrefix with prefix {1,2}: [3] TrimPrefix with string prefix: [world] TrimPrefix with prefix {5,6} (not present): [1 2 1 2 3]
func TrimRight[T comparable, Slice ~[]T](collection, cutset Slice) Slice
TrimRight removes all the trailing cutset from the collection. Play: https://go.dev/play/p/MRpAfR6sf0g
collection := []int{0, 1, 2, 0, 3, 0}
// Test with valid cutset
result := TrimRight(collection, []int{0})
fmt.Printf("TrimRight with cutset {0}: %v\n", result)
// Test with string collection
words := []string{" hello ", "world", " "}
result2 := TrimRight(words, []string{" "})
fmt.Printf("TrimRight with string cutset: %v\n", result2)
// Test with no cutset elements
result3 := TrimRight(collection, []int{5})
fmt.Printf("TrimRight with cutset {5} (not present): %v\n", result3)
Output: TrimRight with cutset {0}: [0 1 2 0 3] TrimRight with string cutset: [ hello world ] TrimRight with cutset {5} (not present): [0 1 2 0 3 0]
func TrimSuffix[T comparable, Slice ~[]T](collection, suffix Slice) Slice
TrimSuffix removes all the trailing suffix from the collection. Play: https://go.dev/play/p/IjEUrV0iofq
collection := []int{1, 2, 1, 2, 3}
// Test with valid suffix
result := TrimSuffix(collection, []int{1, 2})
fmt.Printf("TrimSuffix with suffix {1,2}: %v\n", result)
// Test with string collection
words := []string{"hello", "world", "test"}
result2 := TrimSuffix(words, []string{"test"})
fmt.Printf("TrimSuffix with string suffix: %v\n", result2)
// Test with suffix not present
result3 := TrimSuffix(collection, []int{5, 6})
fmt.Printf("TrimSuffix with suffix {5,6} (not present): %v\n", result3)
Output: TrimSuffix with suffix {1,2}: [1 2 1 2 3] TrimSuffix with string suffix: [hello world] TrimSuffix with suffix {5,6} (not present): [1 2 1 2 3]
Try calls the function and return false in case of error.
ok1 := Try(func() error {
return nil
})
ok2 := Try(func() error {
return errors.New("my error")
})
ok3 := Try(func() error {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output: true false false
Try1 is an alias to Try. Play: https://go.dev/play/p/mTyyWUvn9u4
ok1 := Try1(func() error {
return nil
})
ok2 := Try1(func() error {
return errors.New("my error")
})
ok3 := Try1(func() error {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output: true false false
Try2 has the same behavior as Try, but callback returns 2 variables. Play: https://go.dev/play/p/mTyyWUvn9u4
ok1 := Try2(func() (int, error) {
return 42, nil
})
ok2 := Try2(func() (int, error) {
return 42, errors.New("my error")
})
ok3 := Try2(func() (int, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output: true false false
Try3 has the same behavior as Try, but callback returns 3 variables. Play: https://go.dev/play/p/mTyyWUvn9u4
ok1 := Try3(func() (int, string, error) {
return 42, "foobar", nil
})
ok2 := Try3(func() (int, string, error) {
return 42, "foobar", errors.New("my error")
})
ok3 := Try3(func() (int, string, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output: true false false
Try4 has the same behavior as Try, but callback returns 4 variables. Play: https://go.dev/play/p/mTyyWUvn9u4
ok1 := Try4(func() (int, string, float64, error) {
return 42, "foobar", 4.2, nil
})
ok2 := Try4(func() (int, string, float64, error) {
return 42, "foobar", 4.2, errors.New("my error")
})
ok3 := Try4(func() (int, string, float64, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output: true false false
Try5 has the same behavior as Try, but callback returns 5 variables. Play: https://go.dev/play/p/mTyyWUvn9u4
ok1 := Try5(func() (int, string, float64, bool, error) {
return 42, "foobar", 4.2, true, nil
})
ok2 := Try5(func() (int, string, float64, bool, error) {
return 42, "foobar", 4.2, true, errors.New("my error")
})
ok3 := Try5(func() (int, string, float64, bool, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output: true false false
Try6 has the same behavior as Try, but callback returns 6 variables. Play: https://go.dev/play/p/mTyyWUvn9u4
ok1 := Try6(func() (int, string, float64, bool, foo, error) {
return 42, "foobar", 4.2, true, foo{}, nil
})
ok2 := Try6(func() (int, string, float64, bool, foo, error) {
return 42, "foobar", 4.2, true, foo{}, errors.New("my error")
})
ok3 := Try6(func() (int, string, float64, bool, foo, error) {
panic("my error")
})
fmt.Printf("%v\n", ok1)
fmt.Printf("%v\n", ok2)
fmt.Printf("%v\n", ok3)
Output: true false false
TryCatchWithErrorValue has the same behavior as TryWithErrorValue, but calls the catch function in case of error. Play: https://go.dev/play/p/8Pc9gwX_GZO
TryCatchWithErrorValue(
func() error {
panic("trigger an error")
},
func(err any) {
fmt.Printf("catch: %s", err)
},
)
Output: catch: trigger an error
TryOr has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X
value1, ok1 := TryOr(func() (int, error) {
return 42, nil
}, 21)
value2, ok2 := TryOr(func() (int, error) {
return 42, errors.New("my error")
}, 21)
value3, ok3 := TryOr(func() (int, error) {
panic("my error")
}, 21)
fmt.Printf("%v %v\n", value1, ok1)
fmt.Printf("%v %v\n", value2, ok2)
fmt.Printf("%v %v\n", value3, ok3)
Output: 42 true 21 false 21 false
TryOr1 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X
value1, ok1 := TryOr1(func() (int, error) {
return 42, nil
}, 21)
value2, ok2 := TryOr1(func() (int, error) {
return 42, errors.New("my error")
}, 21)
value3, ok3 := TryOr1(func() (int, error) {
panic("my error")
}, 21)
fmt.Printf("%v %v\n", value1, ok1)
fmt.Printf("%v %v\n", value2, ok2)
fmt.Printf("%v %v\n", value3, ok3)
Output: 42 true 21 false 21 false
TryOr2 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X
value1, value2, ok3 := TryOr2(func() (int, string, error) {
panic("my error")
}, 21, "hello")
fmt.Printf("%v %v %v\n", value1, value2, ok3)
Output: 21 hello false
func TryOr3[A, B, C any](callback func() (A, B, C, error), fallbackA A, fallbackB B, fallbackC C) (A, B, C, bool)
TryOr3 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X
value1, value2, value3, ok3 := TryOr3(func() (int, string, bool, error) {
panic("my error")
}, 21, "hello", false)
fmt.Printf("%v %v %v %v\n", value1, value2, value3, ok3)
Output: 21 hello false false
func TryOr4[A, B, C, D any](callback func() (A, B, C, D, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D) (A, B, C, D, bool)
TryOr4 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X
value1, value2, value3, value4, ok3 := TryOr4(func() (int, string, bool, foo, error) {
panic("my error")
}, 21, "hello", false, foo{bar: "bar"})
fmt.Printf("%v %v %v %v %v\n", value1, value2, value3, value4, ok3)
Output: 21 hello false {bar} false
func TryOr5[A, B, C, D, 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)
TryOr5 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X
value1, value2, value3, value4, value5, ok3 := TryOr5(func() (int, string, bool, foo, float64, error) {
panic("my error")
}, 21, "hello", false, foo{bar: "bar"}, 4.2)
fmt.Printf("%v %v %v %v %v %v\n", value1, value2, value3, value4, value5, ok3)
Output: 21 hello false {bar} 4.2 false
func TryOr6[A, B, C, D, E, 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)
TryOr6 has the same behavior as Must, but returns a default value in case of error. Play: https://go.dev/play/p/B4F7Wg2Zh9X
value1, value2, value3, value4, value5, value6, ok3 := TryOr6(func() (int, string, bool, foo, float64, string, error) {
panic("my error")
}, 21, "hello", false, foo{bar: "bar"}, 4.2, "world")
fmt.Printf("%v %v %v %v %v %v %v\n", value1, value2, value3, value4, value5, value6, ok3)
Output: 21 hello false {bar} 4.2 world false
TryWithErrorValue has the same behavior as Try, but also returns value passed to panic. Play: https://go.dev/play/p/Kc7afQIT2Fs
err1, ok1 := TryWithErrorValue(func() error {
return nil
})
err2, ok2 := TryWithErrorValue(func() error {
return errors.New("my error")
})
err3, ok3 := TryWithErrorValue(func() error {
panic("my error")
})
fmt.Printf("%v %v\n", err1, ok1)
fmt.Printf("%v %v\n", err2, ok2)
fmt.Printf("%v %v\n", err3, ok3)
Output: <nil> true my error false my error false
func Union[T comparable, Slice ~[]T](lists ...Slice) Slice
Union returns all distinct elements from given collections. result returns will not change the order of elements relatively. Play: https://go.dev/play/p/DI9RVEB_qMK
func Uniq[T comparable, Slice ~[]T](collection Slice) Slice
Uniq returns a duplicate-free version of a slice, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the slice. Play: https://go.dev/play/p/DTzbeXZ6iEN
list := []int{1, 2, 2, 1}
result := Uniq(list)
fmt.Printf("%v", result)
Output: [1 2]
func UniqBy[T any, U comparable, Slice ~[]T](collection Slice, iteratee func(item T) U) Slice
UniqBy returns a duplicate-free version of a slice, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the slice. It accepts `iteratee` which is invoked for each element in the slice to generate the criterion by which uniqueness is computed. Play: https://go.dev/play/p/g42Z3QSb53u
list := []int{0, 1, 2, 3, 4, 5}
result := UniqBy(list, func(i int) int {
return i % 3
})
fmt.Printf("%v", result)
Output: [0 1 2]
UniqByErr returns a duplicate-free version of a slice, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the slice. It accepts `iteratee` which is invoked for each element in the slice to generate the criterion by which uniqueness is computed. It returns the first error returned by the iteratee function.
list := []int{0, 1, 2, 3, 4, 5}
_, err := UniqByErr(list, func(i int) (int, error) {
if i == 3 {
return 0, errors.New("number 3 is not allowed")
}
return i % 3, nil
})
fmt.Printf("%v", err)
Output: number 3 is not allowed
UniqKeys creates a slice of unique keys in the map. Play: https://go.dev/play/p/TPKAb6ILdHk
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"bar": 3}
result := UniqKeys(kv, kv2)
sort.Strings(result)
fmt.Printf("%v", result)
Output: [bar foo]
func UniqMap[T any, R comparable](collection []T, transform func(item T, index int) R) []R
UniqMap manipulates a slice and transforms it to a slice of another type with unique values. Play: https://go.dev/play/p/fygzLBhvUdB
type User struct {
Name string
Age int
}
users := []User{{Name: "Alex", Age: 10}, {Name: "Alex", Age: 12}, {Name: "Bob", Age: 11}, {Name: "Alice", Age: 20}}
result := UniqMap(users, func(u User, index int) string {
return u.Name
})
sort.Strings(result)
fmt.Printf("%v", result)
Output: [Alex Alice Bob]
func UniqValues[K, V comparable](in ...map[K]V) []V
UniqValues creates a slice of unique values in the map. Play: https://go.dev/play/p/nf6bXMh7rM3
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"baz": 2}
result := UniqValues(kv, kv2)
sort.Ints(result)
fmt.Printf("%v", result)
Output: [1 2]
Unpack2 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W
a, b := Unpack2(T2("hello", 2))
fmt.Printf("%v %v", a, b)
Output: hello 2
Unpack3 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W
a, b, c := Unpack3(T3("hello", 2, true))
fmt.Printf("%v %v %v", a, b, c)
Output: hello 2 true
Unpack4 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W
a, b, c, d := Unpack4(T4("hello", 2, true, foo{bar: "bar"}))
fmt.Printf("%v %v %v %v", a, b, c, d)
Output: hello 2 true {bar}
Unpack5 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W
a, b, c, d, e := Unpack5(T5("hello", 2, true, foo{bar: "bar"}, 4.2))
fmt.Printf("%v %v %v %v %v", a, b, c, d, e)
Output: hello 2 true {bar} 4.2
Unpack6 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W
a, b, c, d, e, f := Unpack6(T6("hello", 2, true, foo{bar: "bar"}, 4.2, "plop"))
fmt.Printf("%v %v %v %v %v %v", a, b, c, d, e, f)
Output: hello 2 true {bar} 4.2 plop
Unpack7 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W
a, b, c, d, e, f, g := Unpack7(T7("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false))
fmt.Printf("%v %v %v %v %v %v %v", a, b, c, d, e, f, g)
Output: hello 2 true {bar} 4.2 plop false
func Unpack8[A, B, C, D, E, F, G, H any](tuple Tuple8[A, B, C, D, E, F, G, H]) (A, B, C, D, E, F, G, H)
Unpack8 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W
a, b, c, d, e, f, g, h := Unpack8(T8("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42))
fmt.Printf("%v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h)
Output: hello 2 true {bar} 4.2 plop false 42
func Unpack9[A, B, C, D, E, F, G, H, I any](tuple Tuple9[A, B, C, D, E, F, G, H, I]) (A, B, C, D, E, F, G, H, I)
Unpack9 returns values contained in a tuple. Play: https://go.dev/play/p/xVP_k0kJ96W
a, b, c, d, e, f, g, h, i := Unpack9(T9("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42, "hello world"))
fmt.Printf("%v %v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h, i)
Output: hello 2 true {bar} 4.2 plop false 42 hello world
Unzip2 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW
a, b := Unzip2([]Tuple2[string, int]{T2("hello", 2)})
fmt.Printf("%v %v", a, b)
Output: [hello] [2]
Unzip3 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW
a, b, c := Unzip3([]Tuple3[string, int, bool]{T3("hello", 2, true)})
fmt.Printf("%v %v %v", a, b, c)
Output: [hello] [2] [true]
Unzip4 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW
a, b, c, d := Unzip4([]Tuple4[string, int, bool, foo]{T4("hello", 2, true, foo{bar: "bar"})})
fmt.Printf("%v %v %v %v", a, b, c, d)
Output: [hello] [2] [true] [{bar}]
Unzip5 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW
a, b, c, d, e := Unzip5([]Tuple5[string, int, bool, foo, float64]{T5("hello", 2, true, foo{bar: "bar"}, 4.2)})
fmt.Printf("%v %v %v %v %v", a, b, c, d, e)
Output: [hello] [2] [true] [{bar}] [4.2]
Unzip6 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW
a, b, c, d, e, f := Unzip6([]Tuple6[string, int, bool, foo, float64, string]{T6("hello", 2, true, foo{bar: "bar"}, 4.2, "plop")})
fmt.Printf("%v %v %v %v %v %v", a, b, c, d, e, f)
Output: [hello] [2] [true] [{bar}] [4.2] [plop]
func Unzip7[A, B, C, D, E, F, G any](tuples []Tuple7[A, B, C, D, E, F, G]) ([]A, []B, []C, []D, []E, []F, []G)
Unzip7 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW
a, b, c, d, e, f, g := Unzip7([]Tuple7[string, int, bool, foo, float64, string, bool]{T7("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false)})
fmt.Printf("%v %v %v %v %v %v %v", a, b, c, d, e, f, g)
Output: [hello] [2] [true] [{bar}] [4.2] [plop] [false]
func Unzip8[A, B, C, D, E, F, G, H any](tuples []Tuple8[A, B, C, D, E, F, G, H]) ([]A, []B, []C, []D, []E, []F, []G, []H)
Unzip8 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW
a, b, c, d, e, f, g, h := Unzip8([]Tuple8[string, int, bool, foo, float64, string, bool, int]{T8("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42)})
fmt.Printf("%v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h)
Output: [hello] [2] [true] [{bar}] [4.2] [plop] [false] [42]
func Unzip9[A, B, C, D, E, F, G, H, I any](tuples []Tuple9[A, B, C, D, E, F, G, H, I]) ([]A, []B, []C, []D, []E, []F, []G, []H, []I)
Unzip9 accepts a slice of grouped elements and creates a slice regrouping the elements to their pre-zip configuration. Play: https://go.dev/play/p/ciHugugvaAW
a, b, c, d, e, f, g, h, i := Unzip9([]Tuple9[string, int, bool, foo, float64, string, bool, int, string]{T9("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42, "hello world")})
fmt.Printf("%v %v %v %v %v %v %v %v %v", a, b, c, d, e, f, g, h, i)
Output: [hello] [2] [true] [{bar}] [4.2] [plop] [false] [42] [hello world]
func UnzipBy2[In, A, B any](items []In, iteratee func(In) (a A, b B)) ([]A, []B)
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 UnzipBy3[In, A, B, C any](items []In, iteratee func(In) (a A, b B, c C)) ([]A, []B, []C)
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 UnzipBy4[In, A, B, C, D any](items []In, iteratee func(In) (a A, b B, c C, d D)) ([]A, []B, []C, []D)
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 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)
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 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)
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 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)
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 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)
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 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)
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 UnzipByErr2[In, A, B any](items []In, iteratee func(In) (a A, b B, err error)) ([]A, []B, error)
UnzipByErr2 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. It returns the first error returned by the iteratee.
a, b, err := UnzipByErr2([]string{"hello", "error", "world"}, func(str string) (string, int, error) {
if str == "error" {
return "", 0, errors.New("error string not allowed")
}
return str, len(str), nil
})
fmt.Printf("%v %v %v", len(a), len(b), err)
Output: 0 0 error string not allowed
func UnzipByErr3[In, A, B, C any](items []In, iteratee func(In) (a A, b B, c C, err error)) ([]A, []B, []C, error)
UnzipByErr3 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. It returns the first error returned by the iteratee.
a, b, c, err := UnzipByErr3([]string{"hello", "error", "world"}, func(str string) (string, int, bool, error) {
if str == "error" {
return "", 0, false, errors.New("error string not allowed")
}
return str, len(str), len(str) > 4, nil
})
fmt.Printf("%v %v %v %v", len(a), len(b), len(c), err)
Output: 0 0 0 error string not allowed
func UnzipByErr4[In, A, B, C, D any](items []In, iteratee func(In) (a A, b B, c C, d D, err error)) ([]A, []B, []C, []D, error)
UnzipByErr4 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. It returns the first error returned by the iteratee.
a, b, c, d, err := UnzipByErr4([]string{"hello", "error", "world"}, func(str string) (string, int, bool, float32, error) {
if str == "error" {
return "", 0, false, 0, errors.New("error string not allowed")
}
return str, len(str), len(str) > 4, float32(len(str)), nil
})
fmt.Printf("%v %v %v %v %v", len(a), len(b), len(c), len(d), err)
Output: 0 0 0 0 error string not allowed
func UnzipByErr5[In, A, B, C, D, E any](items []In, iteratee func(In) (a A, b B, c C, d D, e E, err error)) ([]A, []B, []C, []D, []E, error)
UnzipByErr5 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. It returns the first error returned by the iteratee.
a, b, c, d, e, err := UnzipByErr5([]string{"hello", "error", "world"}, func(str string) (string, int, bool, float32, float64, error) {
if str == "error" {
return "", 0, false, 0, 0, errors.New("error string not allowed")
}
return str, len(str), len(str) > 4, float32(len(str)), float64(len(str)), nil
})
fmt.Printf("%v %v %v %v %v %v", len(a), len(b), len(c), len(d), len(e), err)
Output: 0 0 0 0 0 error string not allowed
func UnzipByErr6[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, err error)) ([]A, []B, []C, []D, []E, []F, error)
UnzipByErr6 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. It returns the first error returned by the iteratee.
a, b, c, d, e, f, err := UnzipByErr6([]string{"hello", "error", "world"}, func(str string) (string, int, bool, float32, float64, int8, error) {
if str == "error" {
return "", 0, false, 0, 0, 0, errors.New("error string not allowed")
}
return str, len(str), len(str) > 4, float32(len(str)), float64(len(str)), int8(len(str)), nil
})
fmt.Printf("%v %v %v %v %v %v %v", len(a), len(b), len(c), len(d), len(e), len(f), err)
Output: 0 0 0 0 0 0 error string not allowed
func UnzipByErr7[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, err error)) ([]A, []B, []C, []D, []E, []F, []G, error)
UnzipByErr7 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. It returns the first error returned by the iteratee.
a, b, c, d, e, f, g, err := UnzipByErr7([]string{"hello", "error", "world"}, func(str string) (string, int, bool, float32, float64, int8, int16, error) {
if str == "error" {
return "", 0, false, 0, 0, 0, 0, errors.New("error string not allowed")
}
return str, len(str), len(str) > 4, float32(len(str)), float64(len(str)), int8(len(str)), int16(len(str)), nil
})
fmt.Printf("%v %v %v %v %v %v %v %v", len(a), len(b), len(c), len(d), len(e), len(f), len(g), err)
Output: 0 0 0 0 0 0 0 error string not allowed
func UnzipByErr8[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, err error)) ([]A, []B, []C, []D, []E, []F, []G, []H, error)
UnzipByErr8 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. It returns the first error returned by the iteratee.
a, b, c, d, e, f, g, h, err := UnzipByErr8([]string{"hello", "error", "world"}, func(str string) (string, int, bool, float32, float64, int8, int16, int32, error) {
if str == "error" {
return "", 0, false, 0, 0, 0, 0, 0, errors.New("error string not allowed")
}
return str, len(str), len(str) > 4, float32(len(str)), float64(len(str)), int8(len(str)), int16(len(str)), int32(len(str)), nil
})
fmt.Printf("%v %v %v %v %v %v %v %v %v", len(a), len(b), len(c), len(d), len(e), len(f), len(g), len(h), err)
Output: 0 0 0 0 0 0 0 0 error string not allowed
func UnzipByErr9[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, err error)) ([]A, []B, []C, []D, []E, []F, []G, []H, []I, error)
UnzipByErr9 iterates over a collection and creates a slice regrouping the elements to their pre-zip configuration. It returns the first error returned by the iteratee.
a, b, c, d, e, f, g, h, i, err := UnzipByErr9([]string{"hello", "error", "world"}, func(str string) (string, int, bool, float32, float64, int8, int16, int32, int64, error) {
if str == "error" {
return "", 0, false, 0, 0, 0, 0, 0, 0, errors.New("error string not allowed")
}
return str, len(str), len(str) > 4, float32(len(str)), float64(len(str)), int8(len(str)), int16(len(str)), int32(len(str)), int64(len(str)), nil
})
fmt.Printf("%v %v %v %v %v %v %v %v %v %v", len(a), len(b), len(c), len(d), len(e), len(f), len(g), len(h), len(i), err)
Output: 0 0 0 0 0 0 0 0 0 error string not allowed
Validate is a helper that creates an error when a condition is not met. Play: https://go.dev/play/p/vPyh51XpCBt
i := 42
err1 := Validate(i < 0, "expected %d < 0", i)
err2 := Validate(i > 0, "expected %d > 0", i)
fmt.Printf("%v\n%v", err1, err2)
Output: expected 42 < 0 <nil>
func ValueOr[K comparable, V any](in map[K]V, key K, fallback V) V
ValueOr returns the value of the given key or the fallback value if the key is not present. Play: https://go.dev/play/p/bAq9mHErB4V
kv := map[string]int{"foo": 1, "bar": 2}
result1 := ValueOr(kv, "foo", 42)
result2 := ValueOr(kv, "baz", 42)
fmt.Printf("%v %v", result1, result2)
Output: 1 42
Values creates a slice of the map values. Play: https://go.dev/play/p/nnRTQkzQfF6
kv := map[string]int{"foo": 1, "bar": 2}
kv2 := map[string]int{"baz": 3}
result := Values(kv, kv2)
sort.Ints(result)
fmt.Printf("%v", result)
Output: [1 2 3]
Window creates a slice of sliding windows of a given size. Each window overlaps with the previous one by size-1 elements. This is equivalent to Sliding(collection, size, 1).
list := []int{1, 2, 3, 4, 5}
result := Window(list, 3)
for _, item := range result {
fmt.Printf("%v\n", item)
}
Output: [1 2 3] [2 3 4] [3 4 5]
func WithoutBy[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) K, exclude ...K) Slice
WithoutBy filters a slice by excluding elements whose extracted keys match any in the exclude list. Returns a new slice containing only the elements whose keys are not in the exclude list. Play: https://go.dev/play/p/VgWJOF01NbJ
type User struct {
ID int
Name string
}
// original users
users := []User{
{ID: 1, Name: "Alice"},
{ID: 2, Name: "Bob"},
{ID: 3, Name: "Charlie"},
}
// exclude users with IDs 2 and 3
excludedIDs := []int{2, 3}
// extract function to get the user ID
extractID := func(user User) int {
return user.ID
}
// filtering users
filteredUsers := WithoutBy(users, extractID, excludedIDs...)
// output the filtered users
fmt.Printf("%v", filteredUsers)
Output: [{1 Alice}]
func WithoutByErr[T any, K comparable, Slice ~[]T](collection Slice, iteratee func(item T) (K, error), exclude ...K) (Slice, error)
WithoutByErr filters a slice by excluding elements whose extracted keys match any in the exclude list. It returns the first error returned by the iteratee.
type User struct {
ID int
Name string
}
// original users
users := []User{
{ID: 1, Name: "Alice"},
{ID: 2, Name: "Bob"},
{ID: 3, Name: "Charlie"},
}
// exclude users with IDs 2 and 3
excludedIDs := []int{2, 3}
// extract function to get the user ID
extractID := func(user User) (int, error) {
if user.ID == 2 {
return 0, errors.New("user 2 extraction failed")
}
return user.ID, nil
}
// filtering users
filteredUsers, err := WithoutByErr(users, extractID, excludedIDs...)
// output the filtered users
fmt.Printf("%v %v", filteredUsers, err)
Output: [] user 2 extraction failed
func WithoutEmpty[T comparable, Slice ~[]T](collection Slice) Slice
WithoutEmpty returns a slice excluding zero values.
Deprecated: Use lo.Compact instead.
func ZipBy2[A, B, Out any](a []A, b []B, iteratee func(a A, b B) Out) []Out
ZipBy2 creates a slice of transformed elements, the first of which contains the first elements 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 ZipBy3[A, B, C, Out any](a []A, b []B, c []C, iteratee func(a A, b B, c C) Out) []Out
ZipBy3 creates a slice of transformed elements, the first of which contains the first elements 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 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
ZipBy4 creates a slice of transformed elements, the first of which contains the first elements 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 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
ZipBy5 creates a slice of transformed elements, the first of which contains the first elements 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 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
ZipBy6 creates a slice of transformed elements, the first of which contains the first elements 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 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
ZipBy7 creates a slice of transformed elements, the first of which contains the first elements 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 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
ZipBy8 creates a slice of transformed elements, the first of which contains the first elements 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 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
ZipBy9 creates a slice of transformed elements, the first of which contains the first elements 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
ZipByErr2 creates a slice of transformed elements, the first of which contains the first elements 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. It returns the first error returned by the iteratee.
result, err := ZipByErr2([]string{"a", "b", "c"}, []int{1, 2, 3}, func(a string, b int) (string, error) {
if a == "b" {
return "", errors.New("b is not allowed")
}
return fmt.Sprintf("%v-%v", a, b), nil
})
if err != nil {
fmt.Printf("error: %v\n", err)
return
}
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: error: b is not allowed
func ZipByErr3[A, B, C, Out any](a []A, b []B, c []C, iteratee func(a A, b B, c C) (Out, error)) ([]Out, error)
ZipByErr3 creates a slice of transformed elements, the first of which contains the first elements 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. It returns the first error returned by the iteratee.
func ZipByErr4[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, error)) ([]Out, error)
ZipByErr4 creates a slice of transformed elements, the first of which contains the first elements 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. It returns the first error returned by the iteratee.
func ZipByErr5[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, error)) ([]Out, error)
ZipByErr5 creates a slice of transformed elements, the first of which contains the first elements 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. It returns the first error returned by the iteratee.
func ZipByErr6[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, error)) ([]Out, error)
ZipByErr6 creates a slice of transformed elements, the first of which contains the first elements 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. It returns the first error returned by the iteratee.
func ZipByErr7[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, error)) ([]Out, error)
ZipByErr7 creates a slice of transformed elements, the first of which contains the first elements 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. It returns the first error returned by the iteratee.
func ZipByErr8[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, error)) ([]Out, error)
ZipByErr8 creates a slice of transformed elements, the first of which contains the first elements 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. It returns the first error returned by the iteratee.
func ZipByErr9[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, error)) ([]Out, error)
ZipByErr9 creates a slice of transformed elements, the first of which contains the first elements 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. It returns the first error returned by the iteratee.
type Clonable[T any] interface { Clone() T }
Clonable defines a constraint of types having Clone() T method.
DispatchingStrategy is a function that distributes messages to channels.
Entry defines a key/value pairs.
Entries transforms a map into a slice of key/value pairs. Play: https://go.dev/play/p/_t4Xe34-Nl5
kv := map[string]int{"foo": 1, "bar": 2, "baz": 3}
result := Entries(kv)
sort.Slice(result, func(i, j int) bool {
return strings.Compare(result[i].Key, result[j].Key) < 0
})
fmt.Printf("%v", result)
Output: [{bar 2} {baz 3} {foo 1}]
type Transaction[T any] struct { }
Transaction implements a Saga pattern.
transaction := NewTransaction[int]().
Then(
func(state int) (int, error) {
fmt.Println("step 1")
return state + 10, nil
},
func(state int) int {
fmt.Println("rollback 1")
return state - 10
},
).
Then(
func(state int) (int, error) {
fmt.Println("step 2")
return state + 15, nil
},
func(state int) int {
fmt.Println("rollback 2")
return state - 15
},
).
Then(
func(state int) (int, error) {
fmt.Println("step 3")
if true {
return state, errors.New("error")
}
return state + 42, nil
},
func(state int) int {
fmt.Println("rollback 3")
return state - 42
},
)
_, _ = transaction.Process(-5)
Output: step 1 step 2 step 3 rollback 2 rollback 1
transaction := NewTransaction[int]().
Then(
func(state int) (int, error) {
return state + 10, nil
},
func(state int) int {
return state - 10
},
).
Then(
func(state int) (int, error) {
return state, errors.New("error")
},
func(state int) int {
return state - 15
},
).
Then(
func(state int) (int, error) {
return state + 42, nil
},
func(state int) int {
return state - 42
},
)
state, err := transaction.Process(-5)
fmt.Println(state)
fmt.Println(err)
Output: -5 error
transaction := NewTransaction[int]().
Then(
func(state int) (int, error) {
return state + 10, nil
},
func(state int) int {
return state - 10
},
).
Then(
func(state int) (int, error) {
return state + 15, nil
},
func(state int) int {
return state - 15
},
).
Then(
func(state int) (int, error) {
return state + 42, nil
},
func(state int) int {
return state - 42
},
)
state, err := transaction.Process(-5)
fmt.Println(state)
fmt.Println(err)
Output: 62 <nil>
type Tuple2[A, B any] struct { A A B B }
Tuple2 is a group of 2 elements (pair).
CrossJoin2 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/3VFppyL9FDU
result := CrossJoin2([]string{"a", "b"}, []int{1, 2, 3, 4})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: {a 1} {a 2} {a 3} {a 4} {b 1} {b 2} {b 3} {b 4}
T2 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm
result := T2("hello", 2)
fmt.Printf("%v %v", result.A, result.B)
Output: hello 2
Zip2 creates a slice of grouped elements, the first of which contains the first elements 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/jujaA6GaJTp
result := Zip2([]string{"hello"}, []int{2})
fmt.Printf("%v", result)
Output: [{hello 2}]
type Tuple3[A, B, C any] struct { A A B B C C }
Tuple3 is a group of 3 elements.
CrossJoin3 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/2WGeHyJj4fK
result := CrossJoin3([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: {a 1 true} {a 1 false} {a 2 true} {a 2 false} {a 3 true} {a 3 false} {a 4 true} {a 4 false} {b 1 true} {b 1 false} {b 2 true} {b 2 false} {b 3 true} {b 3 false} {b 4 true} {b 4 false}
T3 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm
result := T3("hello", 2, true)
fmt.Printf("%v %v %v", result.A, result.B, result.C)
Output: hello 2 true
Zip3 creates a slice of grouped elements, the first of which contains the first elements 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/jujaA6GaJTp
result := Zip3([]string{"hello"}, []int{2}, []bool{true})
fmt.Printf("%v", result)
Output: [{hello 2 true}]
type Tuple4[A, B, C, D any] struct { A A B B C C D D }
Tuple4 is a group of 4 elements.
CrossJoin4 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/6XhKjLmMnNp
result := CrossJoin4([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: {a 1 true {bar}} {a 1 false {bar}} {a 2 true {bar}} {a 2 false {bar}} {a 3 true {bar}} {a 3 false {bar}} {a 4 true {bar}} {a 4 false {bar}} {b 1 true {bar}} {b 1 false {bar}} {b 2 true {bar}} {b 2 false {bar}} {b 3 true {bar}} {b 3 false {bar}} {b 4 true {bar}} {b 4 false {bar}}
T4 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm
result := T4("hello", 2, true, foo{bar: "bar"})
fmt.Printf("%v %v %v %v", result.A, result.B, result.C, result.D)
Output: hello 2 true {bar}
Zip4 creates a slice of grouped elements, the first of which contains the first elements 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/jujaA6GaJTp
result := Zip4([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}})
fmt.Printf("%v", result)
Output: [{hello 2 true {bar}}]
type Tuple5[A, B, C, D, E any] struct { A A B B C C D D E E }
Tuple5 is a group of 5 elements.
func CrossJoin5[A, B, C, D, E any](listA []A, listB []B, listC []C, listD []D, listE []E) []Tuple5[A, B, C, D, E]
CrossJoin5 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/7oPqRsTuVwX
result := CrossJoin5([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: {a 1 true {bar} 4.2} {a 1 false {bar} 4.2} {a 2 true {bar} 4.2} {a 2 false {bar} 4.2} {a 3 true {bar} 4.2} {a 3 false {bar} 4.2} {a 4 true {bar} 4.2} {a 4 false {bar} 4.2} {b 1 true {bar} 4.2} {b 1 false {bar} 4.2} {b 2 true {bar} 4.2} {b 2 false {bar} 4.2} {b 3 true {bar} 4.2} {b 3 false {bar} 4.2} {b 4 true {bar} 4.2} {b 4 false {bar} 4.2}
T5 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm
result := T5("hello", 2, true, foo{bar: "bar"}, 4.2)
fmt.Printf("%v %v %v %v %v", result.A, result.B, result.C, result.D, result.E)
Output: hello 2 true {bar} 4.2
Zip5 creates a slice of grouped elements, the first of which contains the first elements 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/jujaA6GaJTp
result := Zip5([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2})
fmt.Printf("%v", result)
Output: [{hello 2 true {bar} 4.2}]
type Tuple6[A, B, C, D, E, F any] struct { A A B B C C D D E E F F }
Tuple6 is a group of 6 elements.
func CrossJoin6[A, B, C, D, E, F any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F) []Tuple6[A, B, C, D, E, F]
CrossJoin6 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/8yZ1aB2cD3e
result := CrossJoin6([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: {a 1 true {bar} 4.2 plop} {a 1 false {bar} 4.2 plop} {a 2 true {bar} 4.2 plop} {a 2 false {bar} 4.2 plop} {a 3 true {bar} 4.2 plop} {a 3 false {bar} 4.2 plop} {a 4 true {bar} 4.2 plop} {a 4 false {bar} 4.2 plop} {b 1 true {bar} 4.2 plop} {b 1 false {bar} 4.2 plop} {b 2 true {bar} 4.2 plop} {b 2 false {bar} 4.2 plop} {b 3 true {bar} 4.2 plop} {b 3 false {bar} 4.2 plop} {b 4 true {bar} 4.2 plop} {b 4 false {bar} 4.2 plop}
T6 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm
result := T6("hello", 2, true, foo{bar: "bar"}, 4.2, "plop")
fmt.Printf("%v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F)
Output: hello 2 true {bar} 4.2 plop
func Zip6[A, B, C, D, E, F any](a []A, b []B, c []C, d []D, e []E, f []F) []Tuple6[A, B, C, D, E, F]
Zip6 creates a slice of grouped elements, the first of which contains the first elements 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/jujaA6GaJTp
result := Zip6([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"})
fmt.Printf("%v", result)
Output: [{hello 2 true {bar} 4.2 plop}]
func (t Tuple6[A, B, C, D, E, F]) Unpack() (A, B, C, D, E, F)
Unpack returns values contained in a tuple. Play: https://go.dev/play/p/7J4KrtgtK3M
type Tuple7[A, B, C, D, E, F, G any] struct { A A B B C C D D E E F F G G }
Tuple7 is a group of 7 elements.
func CrossJoin7[A, B, C, D, E, F, G any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G) []Tuple7[A, B, C, D, E, F, G]
CrossJoin7 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/9f4g5h6i7j8
result := CrossJoin7([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: {a 1 true {bar} 4.2 plop false} {a 1 false {bar} 4.2 plop false} {a 2 true {bar} 4.2 plop false} {a 2 false {bar} 4.2 plop false} {a 3 true {bar} 4.2 plop false} {a 3 false {bar} 4.2 plop false} {a 4 true {bar} 4.2 plop false} {a 4 false {bar} 4.2 plop false} {b 1 true {bar} 4.2 plop false} {b 1 false {bar} 4.2 plop false} {b 2 true {bar} 4.2 plop false} {b 2 false {bar} 4.2 plop false} {b 3 true {bar} 4.2 plop false} {b 3 false {bar} 4.2 plop false} {b 4 true {bar} 4.2 plop false} {b 4 false {bar} 4.2 plop false}
T7 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm
result := T7("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false)
fmt.Printf("%v %v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F, result.G)
Output: hello 2 true {bar} 4.2 plop false
func Zip7[A, B, C, D, E, F, G any](a []A, b []B, c []C, d []D, e []E, f []F, g []G) []Tuple7[A, B, C, D, E, F, G]
Zip7 creates a slice of grouped elements, the first of which contains the first elements 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/jujaA6GaJTp
result := Zip7([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false})
fmt.Printf("%v", result)
Output: [{hello 2 true {bar} 4.2 plop false}]
func (t Tuple7[A, B, C, D, E, F, G]) Unpack() (A, B, C, D, E, F, G)
Unpack returns values contained in a tuple. Play: https://go.dev/play/p/Ow9Zgf_zeiA
type Tuple8[A, B, C, D, E, F, G, H any] struct { A A B B C C D D E E F F G G H H }
Tuple8 is a group of 8 elements.
func CrossJoin8[A, B, C, D, E, F, G, H any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H) []Tuple8[A, B, C, D, E, F, G, H]
CrossJoin8 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/0k1l2m3n4o5
result := CrossJoin8([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: {a 1 true {bar} 4.2 plop false 42} {a 1 false {bar} 4.2 plop false 42} {a 2 true {bar} 4.2 plop false 42} {a 2 false {bar} 4.2 plop false 42} {a 3 true {bar} 4.2 plop false 42} {a 3 false {bar} 4.2 plop false 42} {a 4 true {bar} 4.2 plop false 42} {a 4 false {bar} 4.2 plop false 42} {b 1 true {bar} 4.2 plop false 42} {b 1 false {bar} 4.2 plop false 42} {b 2 true {bar} 4.2 plop false 42} {b 2 false {bar} 4.2 plop false 42} {b 3 true {bar} 4.2 plop false 42} {b 3 false {bar} 4.2 plop false 42} {b 4 true {bar} 4.2 plop false 42} {b 4 false {bar} 4.2 plop false 42}
func T8[A, B, C, D, E, F, G, H any](a A, b B, c C, d D, e E, f F, g G, h H) Tuple8[A, B, C, D, E, F, G, H]
T8 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm
result := T8("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42)
fmt.Printf("%v %v %v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F, result.G, result.H)
Output: hello 2 true {bar} 4.2 plop false 42
func Zip8[A, B, C, D, E, F, G, H any](a []A, b []B, c []C, d []D, e []E, f []F, g []G, h []H) []Tuple8[A, B, C, D, E, F, G, H]
Zip8 creates a slice of grouped elements, the first of which contains the first elements 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/jujaA6GaJTp
result := Zip8([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42})
fmt.Printf("%v", result)
Output: [{hello 2 true {bar} 4.2 plop false 42}]
func (t Tuple8[A, B, C, D, E, F, G, H]) Unpack() (A, B, C, D, E, F, G, H)
Unpack returns values contained in a tuple. Play: https://go.dev/play/p/Ow9Zgf_zeiA
type Tuple9[A, B, C, D, E, F, G, H, I any] struct { A A B B C C D D E E F F G G H H I I }
Tuple9 is a group of 9 elements.
func CrossJoin9[A, B, C, D, E, F, G, H, I any](listA []A, listB []B, listC []C, listD []D, listE []E, listF []F, listG []G, listH []H, listI []I) []Tuple9[A, B, C, D, E, F, G, H, I]
CrossJoin9 combines every item from one list with every item from others. It is the cartesian product of lists received as arguments. Returns an empty list if a list is empty. Play: https://go.dev/play/p/6p7q8r9s0t1
result := CrossJoin9([]string{"a", "b"}, []int{1, 2, 3, 4}, []bool{true, false}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, []string{"hello world"})
for _, r := range result {
fmt.Printf("%v\n", r)
}
Output: {a 1 true {bar} 4.2 plop false 42 hello world} {a 1 false {bar} 4.2 plop false 42 hello world} {a 2 true {bar} 4.2 plop false 42 hello world} {a 2 false {bar} 4.2 plop false 42 hello world} {a 3 true {bar} 4.2 plop false 42 hello world} {a 3 false {bar} 4.2 plop false 42 hello world} {a 4 true {bar} 4.2 plop false 42 hello world} {a 4 false {bar} 4.2 plop false 42 hello world} {b 1 true {bar} 4.2 plop false 42 hello world} {b 1 false {bar} 4.2 plop false 42 hello world} {b 2 true {bar} 4.2 plop false 42 hello world} {b 2 false {bar} 4.2 plop false 42 hello world} {b 3 true {bar} 4.2 plop false 42 hello world} {b 3 false {bar} 4.2 plop false 42 hello world} {b 4 true {bar} 4.2 plop false 42 hello world} {b 4 false {bar} 4.2 plop false 42 hello world}
func T9[A, B, C, D, E, F, G, H, 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]
T9 creates a tuple from a list of values. Play: https://go.dev/play/p/IllL3ZO4BQm
result := T9("hello", 2, true, foo{bar: "bar"}, 4.2, "plop", false, 42, "hello world")
fmt.Printf("%v %v %v %v %v %v %v %v %v", result.A, result.B, result.C, result.D, result.E, result.F, result.G, result.H, result.I)
Output: hello 2 true {bar} 4.2 plop false 42 hello world
func Zip9[A, B, C, D, E, F, G, H, 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]
Zip9 creates a slice of grouped elements, the first of which contains the first elements 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/jujaA6GaJTp
result := Zip9([]string{"hello"}, []int{2}, []bool{true}, []foo{{bar: "bar"}}, []float64{4.2}, []string{"plop"}, []bool{false}, []int{42}, []string{"hello world"})
fmt.Printf("%v", result)
Output: [{hello 2 true {bar} 4.2 plop false 42 hello world}]
func (t Tuple9[A, B, C, D, E, F, G, H, I]) Unpack() (A, B, C, D, E, F, G, H, I)
Unpack returns values contained in a tuple. Play: https://go.dev/play/p/Ow9Zgf_zeiA