functional

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](input []T, predicate func(T) bool) bool

All reports whether every element satisfies the predicate. Returns true for an empty slice.

func Any

func Any[T any](input []T, predicate func(T) bool) bool

Any reports whether at least one element satisfies the predicate.

func Associate

func Associate[T any, K comparable, V any](input []T, transform func(T) (K, V)) map[K]V

Associate builds a map from the input slice using a function that returns a key-value pair for each element.

func Chunk

func Chunk[T any](input []T, size int) [][]T

Chunk splits the input slice into sub-slices of the given size. The last chunk may have fewer elements.

func Contains

func Contains[T comparable](input []T, value T) bool

Contains reports whether the slice contains the given value.

func Count

func Count[T any](input []T, predicate func(T) bool) int

Count returns the number of elements satisfying the predicate.

func Distinct

func Distinct[T comparable](input []T) []T

Distinct returns a new slice with duplicate elements removed. Preserves the order of first occurrence.

func DistinctBy

func DistinctBy[T any, K comparable](input []T, keySelector func(T) K) []T

DistinctBy returns a new slice with duplicates removed based on a key selector. Preserves the order of first occurrence.

func Drop

func Drop[T any](input []T, n int) []T

Drop returns the input slice with the first n elements removed.

func DropWhile

func DropWhile[T any](input []T, predicate func(T) bool) []T

DropWhile skips elements while the predicate holds, then returns the rest.

func Filter

func Filter[T any](input []T, predicate func(T) bool) []T

Filter returns a new slice containing only elements that satisfy the predicate.

func Find

func Find[T any](input []T, predicate func(T) bool) (T, bool)

Find returns the first element satisfying the predicate and true, or the zero value and false if none is found.

func FindIndex

func FindIndex[T any](input []T, predicate func(T) bool) int

FindIndex returns the index of the first element satisfying the predicate, or -1 if none is found.

func FlatMap

func FlatMap[T any, U any](input []T, mapper func(T) []U) []U

FlatMap applies a mapper that returns a slice for each element, then flattens the results into a single slice.

func Flatten

func Flatten[T any](input [][]T) []T

Flatten concatenates a slice of slices into a single flat slice.

func ForEach

func ForEach[T any](input []T, action func(T))

ForEach invokes the action function for each element.

func ForEachIndexed

func ForEachIndexed[T any](input []T, action func(int, T))

ForEachIndexed invokes the action function for each element with its index.

func GroupBy

func GroupBy[T any, K comparable](input []T, keySelector func(T) K) map[K][]T

GroupBy groups elements by a key, returning a map of key → slice of elements.

func IndexOf

func IndexOf[T comparable](input []T, value T) int

IndexOf returns the index of the first occurrence of value, or -1 if not found.

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys returns all keys of a map as a slice (order is not guaranteed).

func Map

func Map[T any, U any](input []T, mapper func(T) U) []U

Map applies a mapper function to each element of the input slice, returning a new slice of the mapped results.

func Max

func Max[T cmp.Ordered](input []T) (T, bool)

Max returns the maximum element of an ordered slice. Returns the zero value and false if the slice is empty.

func MaxBy

func MaxBy[T any](input []T, less func(a, b T) int) (T, bool)

MaxBy returns the maximum element according to the comparison function. Returns the zero value and false if the slice is empty.

func Min

func Min[T cmp.Ordered](input []T) (T, bool)

Min returns the minimum element of an ordered slice. Returns the zero value and false if the slice is empty.

func MinBy

func MinBy[T any](input []T, less func(a, b T) int) (T, bool)

MinBy returns the minimum element according to the comparison function. Returns the zero value and false if the slice is empty.

func None

func None[T any](input []T, predicate func(T) bool) bool

None reports whether no elements satisfy the predicate. Returns true for an empty slice.

func Partition

func Partition[T any](input []T, predicate func(T) bool) (truePart []T, falsePart []T)

Partition splits the input into two slices: elements that satisfy the predicate and elements that do not.

func Reduce

func Reduce[T any, U any](input []T, reducer func(U, T) U, initial U) U

Reduce folds the input slice into a single value using the reducer function.

func Reverse

func Reverse[T any](input []T) []T

Reverse returns a new slice with elements in reverse order.

func SortBy

func SortBy[T any](input []T, cmpFn func(a, b T) int) []T

SortBy returns a new sorted slice using the provided comparison function. The cmp function should return a negative number when a < b, zero when a == b, and a positive number when a > b.

func Sum

func Sum[T Numeric](input []T) T

Sum returns the sum of all elements in a numeric slice.

func SumBy

func SumBy[T any, N Numeric](input []T, selector func(T) N) N

SumBy returns the sum of the values extracted by the selector function.

func Take

func Take[T any](input []T, n int) []T

Take returns the first n elements (or all if n > len).

func TakeWhile

func TakeWhile[T any](input []T, predicate func(T) bool) []T

TakeWhile returns the longest prefix of elements satisfying the predicate.

func ToMap

func ToMap[T any, K comparable](input []T, keySelector func(T) K) map[K]T

ToMap builds a map by extracting keys from each element; the values are the elements themselves.

func Unzip

func Unzip[T any, U any](input []Pair[T, U]) ([]T, []U)

Unzip splits a slice of Pair into two separate slices.

func Values

func Values[K comparable, V any](m map[K]V) []V

Values returns all values of a map as a slice (order is not guaranteed).

func Zip

func Zip[T any, U any](a []T, b []U) [][2]any

Zip pairs elements from two slices, truncated to the shorter length.

func ZipWith

func ZipWith[T any, U any, R any](a []T, b []U, combiner func(T, U) R) []R

ZipWith combines elements from two slices using a combiner function, truncated to the shorter length. This is the type-safe alternative to Zip.

Types

type Numeric

type Numeric interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64
}

Numeric is a constraint for types that support arithmetic operations.

type Pair

type Pair[T any, U any] struct {
	First  T
	Second U
}

Pair is a generic pair type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL