Documentation
¶
Overview ¶
Package fn provides fluent, functional-style operations for slices in Go. It offers a chainable API for filtering, mapping, and transforming collections.
Basic usage:
result := fn.Of(products).
Filter(func(p Product) bool { return p.Active }).
Take(10).
Collect()
For type-changing operations (Map, FlatMap, Reduce, etc.), use the package-level functions:
names := fn.Map(
fn.Of(products).Filter(func(p Product) bool { return p.Active }),
func(p Product) string { return p.Name },
).Collect()
Index ¶
- func Contains[T comparable](s Slice[T], element T) bool
- func CountBy[T any, K comparable](s Slice[T], keyFn func(T) K) map[K]int
- func GroupBy[T any, K comparable](s Slice[T], keyFn func(T) K) map[K][]T
- func IndexBy[T any, K comparable](s Slice[T], keyFn func(T) K) map[K]T
- func KeyBy[T any, K comparable](s Slice[T], keyFn func(T) K) map[K]T
- func Max[T cmp.Ordered](s Slice[T]) (T, bool)
- func MaxBy[T any, K cmp.Ordered](s Slice[T], keyFn func(T) K) (T, bool)
- func Min[T cmp.Ordered](s Slice[T]) (T, bool)
- func MinBy[T any, K cmp.Ordered](s Slice[T], keyFn func(T) K) (T, bool)
- func Reduce[T, U any](s Slice[T], initial U, fn func(U, T) U) U
- func ReduceRight[T, U any](s Slice[T], initial U, fn func(U, T) U) U
- func Sum[T cmp.Ordered](s Slice[T]) T
- func SumBy[T any, K cmp.Ordered](s Slice[T], keyFn func(T) K) K
- func ToMap[T any, K comparable, V any](s Slice[T], keyFn func(T) K, valueFn func(T) V) map[K]V
- func ToSet[T comparable](s Slice[T]) map[T]struct{}
- type Entry
- type Slice
- func Compact[T comparable](s Slice[T]) Slice[T]
- func Difference[T comparable](s Slice[T], others ...[]T) Slice[T]
- func FlatMap[T, U any](s Slice[T], fn func(T) []U) Slice[U]
- func Flatten[T any](s Slice[[]T]) Slice[T]
- func FromMap[K comparable, V any](m map[K]V) Slice[V]
- func FromMapEntries[K comparable, V any](m map[K]V) Slice[Entry[K, V]]
- func FromMapKeys[K comparable, V any](m map[K]V) Slice[K]
- func Intersection[T comparable](s Slice[T], others ...[]T) Slice[T]
- func Map[T, U any](s Slice[T], fn func(T) U) Slice[U]
- func Of[T any](data []T) Slice[T]
- func Range(start, end int) Slice[int]
- func SortBy[T any, K cmp.Ordered](s Slice[T], keyFn func(T) K) Slice[T]
- func SortByDesc[T any, K cmp.Ordered](s Slice[T], keyFn func(T) K) Slice[T]
- func SortFunc[T any](s Slice[T], cmpFn func(a, b T) int) Slice[T]
- func Union[T comparable](s Slice[T], others ...[]T) Slice[T]
- func Unique[T comparable](s Slice[T]) Slice[T]
- func UniqueBy[T any, K comparable](s Slice[T], keyFn func(T) K) Slice[T]
- func (s Slice[T]) Append(elements ...T) Slice[T]
- func (s Slice[T]) Chunk(size int) [][]T
- func (s Slice[T]) Clone() Slice[T]
- func (s Slice[T]) Collect() []T
- func (s Slice[T]) Concat(others ...[]T) Slice[T]
- func (s Slice[T]) Count(predicate func(T) bool) int
- func (s Slice[T]) Drop(n int) Slice[T]
- func (s Slice[T]) DropRight(n int) Slice[T]
- func (s Slice[T]) DropWhile(predicate func(T) bool) Slice[T]
- func (s Slice[T]) Every(predicate func(T) bool) bool
- func (s Slice[T]) Filter(predicate func(T) bool) Slice[T]
- func (s Slice[T]) Find(predicate func(T) bool) (T, bool)
- func (s Slice[T]) FindIndex(predicate func(T) bool) int
- func (s Slice[T]) FindLast(predicate func(T) bool) (T, bool)
- func (s Slice[T]) FindLastIndex(predicate func(T) bool) int
- func (s Slice[T]) First() (T, bool)
- func (s Slice[T]) ForEach(fn func(T)) Slice[T]
- func (s Slice[T]) ForEachIndexed(fn func(int, T)) Slice[T]
- func (s Slice[T]) IsEmpty() bool
- func (s Slice[T]) Last() (T, bool)
- func (s Slice[T]) Len() int
- func (s Slice[T]) None(predicate func(T) bool) bool
- func (s Slice[T]) Nth(n int) (T, bool)
- func (s Slice[T]) Partition(predicate func(T) bool) (Slice[T], Slice[T])
- func (s Slice[T]) Prepend(elements ...T) Slice[T]
- func (s Slice[T]) Reject(predicate func(T) bool) Slice[T]
- func (s Slice[T]) Reverse() Slice[T]
- func (s Slice[T]) Some(predicate func(T) bool) bool
- func (s Slice[T]) Take(n int) Slice[T]
- func (s Slice[T]) TakeRight(n int) Slice[T]
- func (s Slice[T]) TakeWhile(predicate func(T) bool) Slice[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](s Slice[T], element T) bool
Contains returns true if the slice contains the specified element.
func CountBy ¶
func CountBy[T any, K comparable](s Slice[T], keyFn func(T) K) map[K]int
CountBy counts elements by a key function. Returns a map of counts per key.
Example:
counts := fn.CountBy(fn.Of(products), func(p Product) string {
return p.Category
})
// Result: map[string]int{"shoes": 5, "shirts": 10}
func GroupBy ¶
func GroupBy[T any, K comparable](s Slice[T], keyFn func(T) K) map[K][]T
GroupBy groups elements by a key function. Returns a map where keys are the result of keyFn and values are slices of elements.
Example:
byCategory := fn.GroupBy(fn.Of(products), func(p Product) string {
return p.Category
})
// Result: map[string][]Product{"shoes": [...], "shirts": [...]}
func IndexBy ¶
func IndexBy[T any, K comparable](s Slice[T], keyFn func(T) K) map[K]T
IndexBy is an alias for KeyBy.
func KeyBy ¶
func KeyBy[T any, K comparable](s Slice[T], keyFn func(T) K) map[K]T
KeyBy creates a map keyed by the result of keyFn. If multiple elements have the same key, the last one wins.
Example:
byID := fn.KeyBy(fn.Of(users), func(u User) int { return u.ID })
// Result: map[int]User{1: user1, 2: user2, ...}
func MaxBy ¶
MaxBy returns the element with the maximum value of the key function. Returns zero value and false if slice is empty.
func MinBy ¶
MinBy returns the element with the minimum value of the key function. Returns zero value and false if slice is empty.
func Reduce ¶
Reduce reduces the slice to a single value using an accumulator function.
Example:
sum := fn.Reduce(fn.Of(numbers), 0, func(acc, n int) int { return acc + n })
func ReduceRight ¶
ReduceRight reduces the slice from right to left.
func ToMap ¶
func ToMap[T any, K comparable, V any](s Slice[T], keyFn func(T) K, valueFn func(T) V) map[K]V
ToMap converts a slice to a map using key and value functions.
Example:
userMap := fn.ToMap(fn.Of(users),
func(u User) int { return u.ID },
func(u User) string { return u.Name },
)
// Result: map[int]string{1: "Alice", 2: "Bob"}
func ToSet ¶
func ToSet[T comparable](s Slice[T]) map[T]struct{}
ToSet converts a slice to a set (map[T]struct{}). Useful for O(1) membership checks.
Example:
idSet := fn.ToSet(fn.Of([]int{1, 2, 3}))
_, exists := idSet[2] // true
Types ¶
type Entry ¶
type Entry[K comparable, V any] struct { Key K Value V }
FromMapEntries creates a Slice of key-value pairs from a map.
type Slice ¶
type Slice[T any] struct { // contains filtered or unexported fields }
Slice wraps a slice to provide fluent, chainable operations. All methods return new slices and never mutate the original.
func Compact ¶
func Compact[T comparable](s Slice[T]) Slice[T]
Compact removes zero values from the slice.
Example:
compact := fn.Compact(fn.Of([]string{"", "a", "", "b"})).Collect()
// Result: []string{"a", "b"}
func Difference ¶
func Difference[T comparable](s Slice[T], others ...[]T) Slice[T]
Difference returns elements in s that are not in others.
Example:
diff := fn.Difference(fn.Of([]int{1, 2, 3}), []int{2, 3, 4}).Collect()
// Result: []int{1}
func FlatMap ¶
FlatMap transforms each element into a slice and flattens the result. Essential for drilling into nested slices.
Example:
// Get all variants from all products
variants := fn.FlatMap(fn.Of(products), func(p Product) []Variant {
return p.Variants
}).Filter(func(v Variant) bool { return v.InStock }).Collect()
func Flatten ¶
Flatten flattens a slice of slices into a single slice.
Example:
flattened := fn.Flatten(fn.Of([][]int{{1, 2}, {3, 4}})).Collect()
// Result: []int{1, 2, 3, 4}
func FromMap ¶
func FromMap[K comparable, V any](m map[K]V) Slice[V]
FromMap creates a Slice from map values.
Example:
users := fn.FromMap(userMap).Filter(...).Collect()
func FromMapEntries ¶
func FromMapEntries[K comparable, V any](m map[K]V) Slice[Entry[K, V]]
func FromMapKeys ¶
func FromMapKeys[K comparable, V any](m map[K]V) Slice[K]
FromMapKeys creates a Slice from map keys.
func Intersection ¶
func Intersection[T comparable](s Slice[T], others ...[]T) Slice[T]
Intersection returns elements that exist in both s and all others.
Example:
common := fn.Intersection(fn.Of([]int{1, 2, 3}), []int{2, 3, 4}).Collect()
// Result: []int{2, 3}
func Map ¶
Map transforms each element using the provided function. Returns a new Slice of the transformed type.
Example:
names := fn.Map(fn.Of(users), func(u User) string { return u.Name }).Collect()
func Of ¶
Of creates a new Slice wrapper from an existing slice. This is the entry point for all fluent operations.
func Range ¶
Range creates a Slice of integers from start to end (exclusive).
Example:
nums := fn.Range(0, 5).Collect() // []int{0, 1, 2, 3, 4}
func SortBy ¶
SortBy returns a new Slice sorted by a comparable key.
Example:
sorted := fn.SortBy(fn.Of(users), func(u User) string {
return u.Name
}).Collect()
func SortByDesc ¶
SortByDesc returns a new Slice sorted by a comparable key in descending order.
func SortFunc ¶
SortFunc returns a new Slice sorted using a custom comparison function. The comparison function should return a negative number when a < b, zero when a == b, and a positive number when a > b.
func Union ¶
func Union[T comparable](s Slice[T], others ...[]T) Slice[T]
Union returns all unique elements from s and others combined.
Example:
all := fn.Union(fn.Of([]int{1, 2}), []int{2, 3}).Collect()
// Result: []int{1, 2, 3}
func Unique ¶
func Unique[T comparable](s Slice[T]) Slice[T]
Unique returns a new Slice with duplicate elements removed. Uses a map for O(1) lookups, so T must be comparable.
Example:
unique := fn.Unique(fn.Of([]int{1, 2, 2, 3, 3, 3})).Collect()
// Result: []int{1, 2, 3}
func UniqueBy ¶
func UniqueBy[T any, K comparable](s Slice[T], keyFn func(T) K) Slice[T]
UniqueBy returns a new Slice with duplicates removed based on a key function.
Example:
// Unique users by email
unique := fn.UniqueBy(fn.Of(users), func(u User) string {
return u.Email
}).Collect()
func (Slice[T]) Chunk ¶
Chunk splits the slice into groups of the specified size. The last chunk may have fewer elements.
func (Slice[T]) Collect ¶
func (s Slice[T]) Collect() []T
Collect returns the underlying slice. Use this to extract the final result after chaining operations.
func (Slice[T]) Drop ¶
Drop returns a new Slice without the first n elements. If n >= len, returns empty slice. If n <= 0, returns all elements.
func (Slice[T]) DropWhile ¶
DropWhile returns elements after dropping from the beginning while predicate returns true.
func (Slice[T]) Every ¶
Every returns true if all elements satisfy the predicate. Returns true for empty slices.
func (Slice[T]) Filter ¶
Filter returns a new Slice containing only elements that satisfy the predicate.
func (Slice[T]) Find ¶
Find returns the first element that satisfies the predicate and true, or the zero value and false if no element matches.
func (Slice[T]) FindIndex ¶
FindIndex returns the index of the first element that satisfies the predicate, or -1 if no element matches.
func (Slice[T]) FindLast ¶
FindLast returns the last element that satisfies the predicate and true, or the zero value and false if no element matches.
func (Slice[T]) FindLastIndex ¶
FindLastIndex returns the index of the last element that satisfies the predicate, or -1 if no element matches.
func (Slice[T]) ForEach ¶
ForEach executes a function for each element. Returns the same Slice for chaining.
func (Slice[T]) ForEachIndexed ¶
ForEachIndexed executes a function for each element with its index. Returns the same Slice for chaining.
func (Slice[T]) None ¶
None returns true if no elements satisfy the predicate. Returns true for empty slices.
func (Slice[T]) Nth ¶
Nth returns the element at index n and true, or zero value and false if out of bounds. Negative indices count from the end (-1 is the last element).
func (Slice[T]) Partition ¶
Partition splits elements into two slices based on the predicate. First slice contains elements where predicate returns true, second slice contains elements where predicate returns false.
func (Slice[T]) Reject ¶
Reject returns a new Slice containing only elements that do NOT satisfy the predicate. Opposite of Filter.
func (Slice[T]) Take ¶
Take returns a new Slice with the first n elements. If n > len, returns all elements. If n <= 0, returns empty slice.