Documentation
¶
Overview ¶
Package collections provides a fluent, chainable collection type for Go, inspired by Laravel Collections.
Index ¶
- func Avg[T Number](c *Collection[T]) (float64, bool)
- func GroupBy[T any, K comparable](c *Collection[T], fn func(T) K) map[K]*Collection[T]
- func IndexOf[T comparable](c *Collection[T], value T) (int, bool)
- func KeyBy[T any, K comparable](c *Collection[T], fn func(T) K) map[K]T
- func MapToMap[T any, K comparable, V any](c *Collection[T], keyFn func(T) K, valFn func(T) V) map[K]V
- func Max[T Number](c *Collection[T]) (T, bool)
- func MaxBy[T any, K Number](c *Collection[T], fn func(T) K) (T, bool)
- func Min[T Number](c *Collection[T]) (T, bool)
- func MinBy[T any, K Number](c *Collection[T], fn func(T) K) (T, bool)
- func Partition[T any](c *Collection[T], fn func(T) bool) (*Collection[T], *Collection[T])
- func Reduce[T, U any](c *Collection[T], initial U, fn func(U, T) U) U
- func SetExitFn(fn func())
- func Sum[T Number](c *Collection[T]) T
- type Collection
- func Collect[T any](items []T) *Collection[T]
- func Diff[T any](c *Collection[T], other *Collection[T], key func(T) any) *Collection[T]
- func FlatMap[T, U any](c *Collection[T], fn func(T) []U) *Collection[U]
- func Flatten[T any](c *Collection[[]T]) *Collection[T]
- func Intersect[T any](c *Collection[T], other *Collection[T], key func(T) any) *Collection[T]
- func Map[T, U any](c *Collection[T], fn func(T) U) *Collection[U]
- func New[T any](items ...T) *Collection[T]
- func Pluck[T, U any](c *Collection[T], fn func(T) U) *Collection[U]
- func SortBy[T any, K Number](c *Collection[T], fn func(T) K) *Collection[T]
- func SortByDesc[T any, K Number](c *Collection[T], fn func(T) K) *Collection[T]
- func SortByString[T any](c *Collection[T], fn func(T) string) *Collection[T]
- func UniqueComparable[T comparable](c *Collection[T]) *Collection[T]
- func Zip[T, U any](c *Collection[T], other *Collection[U]) *Collection[[2]any]
- func (c *Collection[T]) All() []T
- func (c *Collection[T]) Chunk(size int) [][]T
- func (c *Collection[T]) Clone() *Collection[T]
- func (c *Collection[T]) Contains(fn func(T) bool) bool
- func (c *Collection[T]) Count() int
- func (c *Collection[T]) Dd()
- func (c *Collection[T]) Dump() *Collection[T]
- func (c *Collection[T]) DumpStr() string
- func (c *Collection[T]) Each(fn func(T)) *Collection[T]
- func (c *Collection[T]) EachWithIndex(fn func(int, T)) *Collection[T]
- func (c *Collection[T]) Every(fn func(T) bool) bool
- func (c *Collection[T]) Filter(fn func(T) bool) *Collection[T]
- func (c *Collection[T]) First() (T, bool)
- func (c *Collection[T]) FirstWhere(fn func(T) bool) (T, bool)
- func (c *Collection[T]) IsEmpty() bool
- func (c *Collection[T]) IsNotEmpty() bool
- func (c *Collection[T]) Last() (T, bool)
- func (c *Collection[T]) LastWhere(fn func(T) bool) (T, bool)
- func (c *Collection[T]) Merge(other *Collection[T]) *Collection[T]
- func (c *Collection[T]) Nth(n int) (T, bool)
- func (c *Collection[T]) Pop() (T, bool)
- func (c *Collection[T]) Prepend(items ...T) *Collection[T]
- func (c *Collection[T]) Push(items ...T) *Collection[T]
- func (c *Collection[T]) Reject(fn func(T) bool) *Collection[T]
- func (c *Collection[T]) Reverse() *Collection[T]
- func (c *Collection[T]) Search(fn func(T) bool) (int, bool)
- func (c *Collection[T]) Shift() (T, bool)
- func (c *Collection[T]) Shuffle(randIntn func(int) int) *Collection[T]
- func (c *Collection[T]) Skip(n int) *Collection[T]
- func (c *Collection[T]) SkipUntil(fn func(T) bool) *Collection[T]
- func (c *Collection[T]) Slice(offset, length int) *Collection[T]
- func (c *Collection[T]) Sort(less func(a, b T) bool) *Collection[T]
- func (c *Collection[T]) String() string
- func (c *Collection[T]) Take(n int) *Collection[T]
- func (c *Collection[T]) TakeLast(n int) *Collection[T]
- func (c *Collection[T]) TakeUntil(fn func(T) bool) *Collection[T]
- func (c *Collection[T]) Tap(fn func(*Collection[T])) *Collection[T]
- func (c *Collection[T]) ToJSON() ([]byte, error)
- func (c *Collection[T]) Unique(key func(T) any) *Collection[T]
- type Number
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Avg ¶
func Avg[T Number](c *Collection[T]) (float64, bool)
Avg returns the average of a numeric collection as float64. Returns 0 and false if the collection is empty.
func GroupBy ¶
func GroupBy[T any, K comparable](c *Collection[T], fn func(T) K) map[K]*Collection[T]
GroupBy groups items by a key derived from fn, returning a map of collections.
byStatus := collections.GroupBy(orders, func(o Order) string { return o.Status })
func IndexOf ¶
func IndexOf[T comparable](c *Collection[T], value T) (int, bool)
IndexOf returns the index of the first occurrence of value in the collection. Prefer this over Collection.Search for comparable types — avoids closure overhead.
func KeyBy ¶
func KeyBy[T any, K comparable](c *Collection[T], fn func(T) K) map[K]T
KeyBy returns a map keyed by the result of fn. Later items overwrite earlier ones with the same key.
byID := collections.KeyBy(users, func(u User) int { return u.ID })
func MapToMap ¶
func MapToMap[T any, K comparable, V any](c *Collection[T], keyFn func(T) K, valFn func(T) V) map[K]V
MapToMap converts a collection to a plain map using key and value extractor functions.
func Max ¶
func Max[T Number](c *Collection[T]) (T, bool)
Max returns the maximum value in a numeric collection. Returns zero and false if the collection is empty.
func MaxBy ¶
func MaxBy[T any, K Number](c *Collection[T], fn func(T) K) (T, bool)
MaxBy returns the item with the largest key, as derived by fn.
func Min ¶
func Min[T Number](c *Collection[T]) (T, bool)
Min returns the minimum value in a numeric collection. Returns zero and false if the collection is empty.
func MinBy ¶
func MinBy[T any, K Number](c *Collection[T], fn func(T) K) (T, bool)
MinBy returns the item with the smallest key, as derived by fn.
func Partition ¶
func Partition[T any](c *Collection[T], fn func(T) bool) (*Collection[T], *Collection[T])
Partition splits the collection into two: items that pass and items that fail the predicate.
pass, fail := collections.Partition(scores, func(s int) bool { return s >= 50 })
func Reduce ¶
func Reduce[T, U any](c *Collection[T], initial U, fn func(U, T) U) U
Reduce reduces the collection to a single value.
total := collections.Reduce(prices, 0.0, func(acc float64, p float64) float64 { return acc + p })
func SetExitFn ¶
func SetExitFn(fn func())
SetExitFn overrides the function called by Dd. Pass nil to restore the default os.Exit(1). Intended for use in tests only.
func Sum ¶
func Sum[T Number](c *Collection[T]) T
Sum returns the sum of all items in a numeric collection.
Types ¶
type Collection ¶
type Collection[T any] struct { // contains filtered or unexported fields }
Collection is a generic, ordered list of items with a fluent interface.
func Collect ¶
func Collect[T any](items []T) *Collection[T]
Collect creates a Collection from a slice.
func Diff ¶
func Diff[T any](c *Collection[T], other *Collection[T], key func(T) any) *Collection[T]
Diff returns items in c that are not present in other, using the key function for comparison.
func FlatMap ¶
func FlatMap[T, U any](c *Collection[T], fn func(T) []U) *Collection[U]
FlatMap transforms each item into a slice and flattens the results.
func Flatten ¶
func Flatten[T any](c *Collection[[]T]) *Collection[T]
Flatten flattens one level of nested slices.
flat := collections.Flatten(collections.Collect([][]int{{1, 2}, {3, 4}}))
func Intersect ¶
func Intersect[T any](c *Collection[T], other *Collection[T], key func(T) any) *Collection[T]
Intersect returns items in c that are also present in other, using the key function.
func Map ¶
func Map[T, U any](c *Collection[T], fn func(T) U) *Collection[U]
Map transforms each item in the collection using fn, returning a new collection.
names := collections.Map(users, func(u User) string { return u.Name })
func Pluck ¶
func Pluck[T, U any](c *Collection[T], fn func(T) U) *Collection[U]
Pluck extracts a single field from each item using fn.
ids := collections.Pluck(users, func(u User) int { return u.ID })
func SortBy ¶
func SortBy[T any, K Number](c *Collection[T], fn func(T) K) *Collection[T]
SortBy returns a new collection sorted by the numeric key derived from fn (ascending).
func SortByDesc ¶
func SortByDesc[T any, K Number](c *Collection[T], fn func(T) K) *Collection[T]
SortByDesc returns a new collection sorted by the numeric key (descending).
func SortByString ¶
func SortByString[T any](c *Collection[T], fn func(T) string) *Collection[T]
SortByString returns a new collection sorted by a string key.
func UniqueComparable ¶
func UniqueComparable[T comparable](c *Collection[T]) *Collection[T]
UniqueComparable removes duplicate items from a collection of comparable types. Prefer this over Collection.Unique when T is comparable — it uses map[T]struct{} instead of map[any]struct{}, avoiding per-element interface boxing.
nums := collections.UniqueComparable(collections.New(1, 2, 2, 3))
func Zip ¶
func Zip[T, U any](c *Collection[T], other *Collection[U]) *Collection[[2]any]
Zip pairs each item in c with the corresponding item in other. The resulting collection stops at the shorter length.
func (*Collection[T]) Chunk ¶
func (c *Collection[T]) Chunk(size int) [][]T
Chunk splits the collection into slices of the given size.
func (*Collection[T]) Clone ¶
func (c *Collection[T]) Clone() *Collection[T]
ToJSON serialises the collection to JSON bytes. Clone returns a new collection with an independent copy of the backing slice.
func (*Collection[T]) Contains ¶
func (c *Collection[T]) Contains(fn func(T) bool) bool
Contains returns true if any item satisfies the predicate.
func (*Collection[T]) Count ¶
func (c *Collection[T]) Count() int
Count returns the number of items.
func (*Collection[T]) Dd ¶
func (c *Collection[T]) Dd()
Dd dumps the collection to stdout then exits the process (dump and die). Mirrors Laravel's dd() — for debugging only, never use in production.
func (*Collection[T]) Dump ¶
func (c *Collection[T]) Dump() *Collection[T]
Dump prints the collection items as indented JSON to stdout and returns the collection unchanged, so it can be inserted anywhere in a chain.
collections.New(1, 2, 3).Filter(fn).Dump().Map(fn2)
func (*Collection[T]) DumpStr ¶
func (c *Collection[T]) DumpStr() string
DumpStr returns the collection as an indented JSON string without printing. Useful for logging and snapshot tests.
func (*Collection[T]) Each ¶
func (c *Collection[T]) Each(fn func(T)) *Collection[T]
Each calls fn for every item and returns the collection for chaining.
func (*Collection[T]) EachWithIndex ¶
func (c *Collection[T]) EachWithIndex(fn func(int, T)) *Collection[T]
EachWithIndex calls fn with the index and item for every item.
func (*Collection[T]) Every ¶
func (c *Collection[T]) Every(fn func(T) bool) bool
Every returns true if all items satisfy the predicate.
func (*Collection[T]) Filter ¶
func (c *Collection[T]) Filter(fn func(T) bool) *Collection[T]
Filter returns a new collection with items that satisfy the predicate.
func (*Collection[T]) First ¶
func (c *Collection[T]) First() (T, bool)
First returns the first item. Returns zero value and false if empty.
func (*Collection[T]) FirstWhere ¶
func (c *Collection[T]) FirstWhere(fn func(T) bool) (T, bool)
FirstWhere returns the first item that satisfies fn.
func (*Collection[T]) IsEmpty ¶
func (c *Collection[T]) IsEmpty() bool
IsEmpty returns true if the collection has no items.
func (*Collection[T]) IsNotEmpty ¶
func (c *Collection[T]) IsNotEmpty() bool
IsNotEmpty returns true if the collection has at least one item.
func (*Collection[T]) Last ¶
func (c *Collection[T]) Last() (T, bool)
Last returns the last item. Returns zero value and false if empty.
func (*Collection[T]) LastWhere ¶
func (c *Collection[T]) LastWhere(fn func(T) bool) (T, bool)
LastWhere returns the last item that satisfies fn.
func (*Collection[T]) Merge ¶
func (c *Collection[T]) Merge(other *Collection[T]) *Collection[T]
Merge returns a new collection combining the current items with another collection.
func (*Collection[T]) Nth ¶
func (c *Collection[T]) Nth(n int) (T, bool)
Nth returns the item at index n (0-based). Returns zero value and false if out of range.
func (*Collection[T]) Pop ¶
func (c *Collection[T]) Pop() (T, bool)
Pop removes and returns the last item.
func (*Collection[T]) Prepend ¶
func (c *Collection[T]) Prepend(items ...T) *Collection[T]
Prepend inserts items at the beginning and returns a new collection.
func (*Collection[T]) Push ¶
func (c *Collection[T]) Push(items ...T) *Collection[T]
Push appends items to the end and returns a new collection.
func (*Collection[T]) Reject ¶
func (c *Collection[T]) Reject(fn func(T) bool) *Collection[T]
Reject returns a new collection with items that do NOT satisfy the predicate.
func (*Collection[T]) Reverse ¶
func (c *Collection[T]) Reverse() *Collection[T]
Reverse returns a new collection with items in reverse order.
func (*Collection[T]) Search ¶
func (c *Collection[T]) Search(fn func(T) bool) (int, bool)
Search returns the index of the first item that satisfies the predicate.
func (*Collection[T]) Shift ¶
func (c *Collection[T]) Shift() (T, bool)
Shift removes and returns the first item.
func (*Collection[T]) Shuffle ¶
func (c *Collection[T]) Shuffle(randIntn func(int) int) *Collection[T]
Shuffle returns a new collection with items in random order. Pass a seeded rand func, e.g. rand.Intn, for reproducibility.
func (*Collection[T]) Skip ¶
func (c *Collection[T]) Skip(n int) *Collection[T]
Skip returns a new collection skipping the first n items.
func (*Collection[T]) SkipUntil ¶
func (c *Collection[T]) SkipUntil(fn func(T) bool) *Collection[T]
SkipUntil skips items until the predicate is true, then returns the rest.
func (*Collection[T]) Slice ¶
func (c *Collection[T]) Slice(offset, length int) *Collection[T]
Slice returns a new collection starting at offset with at most length items.
func (*Collection[T]) Sort ¶
func (c *Collection[T]) Sort(less func(a, b T) bool) *Collection[T]
Sort returns a new collection sorted by the given less function.
func (*Collection[T]) String ¶
func (c *Collection[T]) String() string
String returns the JSON representation of the collection.
func (*Collection[T]) Take ¶
func (c *Collection[T]) Take(n int) *Collection[T]
Take returns a new collection with only the first n items.
func (*Collection[T]) TakeLast ¶
func (c *Collection[T]) TakeLast(n int) *Collection[T]
TakeLast returns a new collection with only the last n items.
func (*Collection[T]) TakeUntil ¶
func (c *Collection[T]) TakeUntil(fn func(T) bool) *Collection[T]
TakeUntil returns items until the predicate becomes true.
func (*Collection[T]) Tap ¶
func (c *Collection[T]) Tap(fn func(*Collection[T])) *Collection[T]
Tap calls fn with the collection (for side effects / debugging) and returns the collection.
func (*Collection[T]) ToJSON ¶
func (c *Collection[T]) ToJSON() ([]byte, error)
func (*Collection[T]) Unique ¶
func (c *Collection[T]) Unique(key func(T) any) *Collection[T]
Unique returns a new collection with duplicate items removed, using a key function.