Documentation
¶
Overview ¶
Package base defines the core collection types and their methods. These types are re-exported via type aliases in the slice and kv packages.
Index ¶
- func FindAs[R, T any](ts []T) option.Option[R]
- func KeyBy[T any, K comparable](ts []T, fn func(T) K) map[K]T
- func Partition[T any](ts []T, fn func(T) bool) ([]T, []T)
- func ToSet[T comparable](ts []T) map[T]bool
- type Entries
- func (e Entries[K, V]) KeepIf(fn func(K, V) bool) Entries[K, V]
- func (e Entries[K, V]) Keys() Mapper[K]
- func (e Entries[K, V]) RemoveIf(fn func(K, V) bool) Entries[K, V]
- func (e Entries[K, V]) ToAny(fn func(K, V) any) Mapper[any]
- func (e Entries[K, V]) ToBool(fn func(K, V) bool) Mapper[bool]
- func (e Entries[K, V]) ToByte(fn func(K, V) byte) Mapper[byte]
- func (e Entries[K, V]) ToError(fn func(K, V) error) Mapper[error]
- func (e Entries[K, V]) ToFloat32(fn func(K, V) float32) Mapper[float32]
- func (e Entries[K, V]) ToFloat64(fn func(K, V) float64) Float64
- func (e Entries[K, V]) ToInt(fn func(K, V) int) Int
- func (e Entries[K, V]) ToInt32(fn func(K, V) int32) Mapper[int32]
- func (e Entries[K, V]) ToInt64(fn func(K, V) int64) Mapper[int64]
- func (e Entries[K, V]) ToRune(fn func(K, V) rune) Mapper[rune]
- func (e Entries[K, V]) ToString(fn func(K, V) string) String
- func (e Entries[K, V]) Values() Mapper[V]
- type Float64
- type Int
- type Mapper
- func (ts Mapper[T]) Any(fn func(T) bool) bool
- func (ts Mapper[T]) Clone() Mapper[T]
- func (ts Mapper[T]) Drop(n int) Mapper[T]
- func (ts Mapper[T]) DropLast(n int) Mapper[T]
- func (ts Mapper[T]) DropLastWhile(fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) DropWhile(fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) Each(fn func(T))
- func (ts Mapper[T]) EachIndexed(fn func(int, T))
- func (ts Mapper[T]) Every(fn func(T) bool) bool
- func (ts Mapper[T]) Find(fn func(T) bool) option.Option[T]
- func (ts Mapper[T]) FindLast(fn func(T) bool) option.Option[T]
- func (ts Mapper[T]) First() option.Option[T]
- func (ts Mapper[T]) FlatMap(fn func(T) []T) Mapper[T]
- func (ts Mapper[T]) IndexWhere(fn func(T) bool) option.Option[int]
- func (ts Mapper[T]) Intersperse(sep T) Mapper[T]
- func (ts Mapper[T]) IsSorted(cmp func(T, T) int) bool
- func (ts Mapper[T]) KeepIf(fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) KeepIfWhen(cond bool, fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) KeyByInt(fn func(T) int) map[int]T
- func (ts Mapper[T]) KeyByString(fn func(T) string) map[string]T
- func (ts Mapper[T]) Last() option.Option[T]
- func (ts Mapper[T]) LastIndexWhere(fn func(T) bool) option.Option[int]
- func (ts Mapper[T]) Len() int
- func (ts Mapper[T]) None(fn func(T) bool) bool
- func (m Mapper[T]) PEach(workers int, fn func(T))
- func (m Mapper[T]) PKeepIf(workers int, fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) Partition(fn func(T) bool) (Mapper[T], Mapper[T])
- func (ts Mapper[T]) RemoveIf(fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) RemoveIfWhen(cond bool, fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) Reverse() Mapper[T]
- func (ts Mapper[T]) Sample() option.Option[T]
- func (ts Mapper[T]) Samples(count int) Mapper[T]
- func (ts Mapper[T]) Shuffle() Mapper[T]
- func (ts Mapper[T]) Single() either.Either[int, T]
- func (ts Mapper[T]) Sort(cmp func(T, T) int) Mapper[T]
- func (ts Mapper[T]) Take(n int) Mapper[T]
- func (ts Mapper[T]) TakeLast(n int) Mapper[T]
- func (ts Mapper[T]) TakeWhile(fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) ToAny(fn func(T) any) Mapper[any]
- func (ts Mapper[T]) ToBool(fn func(T) bool) Mapper[bool]
- func (ts Mapper[T]) ToByte(fn func(T) byte) Mapper[byte]
- func (ts Mapper[T]) ToError(fn func(T) error) Mapper[error]
- func (ts Mapper[T]) ToFloat32(fn func(T) float32) Mapper[float32]
- func (ts Mapper[T]) ToFloat64(fn func(T) float64) Float64
- func (ts Mapper[T]) ToInt(fn func(T) int) Int
- func (ts Mapper[T]) ToInt32(fn func(T) int32) Mapper[int32]
- func (ts Mapper[T]) ToInt64(fn func(T) int64) Mapper[int64]
- func (ts Mapper[T]) ToRune(fn func(T) rune) Mapper[rune]
- func (ts Mapper[T]) ToString(fn func(T) string) String
- func (ts Mapper[T]) Transform(fn func(T) T) Mapper[T]
- type String
- func (ss String) Contains(target string) bool
- func (ss String) ContainsAny(targets []string) bool
- func (ss String) Each(fn func(string))
- func (ss String) Join(sep string) string
- func (ss String) Len() int
- func (ss String) Matches(filter []string) bool
- func (ss String) NonEmpty() String
- func (ss String) ToSet() map[string]bool
- func (ss String) Unique() String
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FindAs ¶
FindAs returns the first element that type-asserts to R, or not-ok if none match. Useful for finding a specific concrete type in a slice of interfaces.
func KeyBy ¶ added in v0.41.0
func KeyBy[T any, K comparable](ts []T, fn func(T) K) map[K]T
KeyBy indexes elements by a key derived from fn, returning a map from key to element. If multiple elements produce the same key, the last one wins.
func Partition ¶ added in v0.41.0
Partition splits ts into two slices: elements where fn returns true, and elements where it returns false. Single pass. Both results are independent slices.
func ToSet ¶
func ToSet[T comparable](ts []T) map[T]bool
ToSet returns a map with each element as a key set to true. Membership is by key presence (use comma-ok or range), not by value. Requires comparable elements.
Types ¶
type Entries ¶
type Entries[K comparable, V any] map[K]V
Entries is a defined type over map[K]V. Indexing, ranging, and len all work as with a plain map. The zero value is a nil map — safe for reads (len, range) but panics on write. From does not copy; the Entries and the original map share the same backing data.
All methods that produce slices from map data return results in nondeterministic order (Go map iteration order). Sort the result if stable ordering is needed.
func (Entries[K, V]) KeepIf ¶ added in v0.40.0
KeepIf returns a new Entries containing only the key-value pairs where fn returns true.
func (Entries[K, V]) Keys ¶
Keys extracts the keys as a Mapper for further transformation. Order is not guaranteed (map iteration order).
func (Entries[K, V]) RemoveIf ¶ added in v0.40.0
RemoveIf returns a new Entries containing only the key-value pairs where fn returns false.
func (Entries[K, V]) ToAny ¶
ToAny returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).
func (Entries[K, V]) ToBool ¶
ToBool returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).
func (Entries[K, V]) ToByte ¶
ToByte returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).
func (Entries[K, V]) ToError ¶
ToError returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).
func (Entries[K, V]) ToFloat32 ¶
ToFloat32 returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).
func (Entries[K, V]) ToFloat64 ¶
ToFloat64 returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).
func (Entries[K, V]) ToInt ¶
ToInt returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).
func (Entries[K, V]) ToInt32 ¶
ToInt32 returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).
func (Entries[K, V]) ToInt64 ¶
ToInt64 returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).
func (Entries[K, V]) ToRune ¶
ToRune returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).
type Float64 ¶
type Float64 []float64
func (Float64) Max ¶
Max returns the largest non-NaN element, or not-ok if the slice is empty or contains only NaN values. NaN elements are skipped. Signed zeros: -0.0 and +0.0 are equal per IEEE 754; the result depends on input order when both are present. Use math.Copysign if sign matters.
func (Float64) Min ¶
Min returns the smallest non-NaN element, or not-ok if the slice is empty or contains only NaN values. NaN elements are skipped. Signed zeros: -0.0 and +0.0 are equal per IEEE 754; the result depends on input order when both are present. Use math.Copysign if sign matters.
type Int ¶
type Int []int
type Mapper ¶
type Mapper[T any] []T
Mapper is a defined type over []T. It preserves indexing, range, and len while adding chainable functional methods. The zero value is a nil slice; methods treat it like an empty slice.
func PFlatMap ¶ added in v0.41.0
PFlatMap applies fn to each element of m concurrently using the specified number of worker goroutines, then flattens the results into a single slice. Order is preserved. The fn must be safe for concurrent use.
Panics in fn are recovered, converted to *rslt.PanicError with a stack captured during recovery, and re-panicked on the calling goroutine after all workers exit.
func PMap ¶ added in v0.41.0
PMap returns the result of applying fn to each member of m, using the specified number of worker goroutines. Order is preserved. The fn must be safe for concurrent use.
Panics in fn are recovered, converted to *rslt.PanicError with a stack captured during recovery, and re-panicked on the calling goroutine after all workers exit.
func (Mapper[T]) Any ¶
Any reports whether fn returns true for any element. Scans elements in index order and stops at the first match. fn must not be nil.
func (Mapper[T]) Drop ¶ added in v0.41.0
Drop returns ts without the first n elements. Returns empty if n >= len(ts). Negative n is treated as zero. The result aliases the input backing array.
func (Mapper[T]) DropLast ¶ added in v0.41.0
DropLast returns ts without the last n elements. Returns empty if n >= len(ts). Negative n is treated as zero. The result aliases the input backing array.
func (Mapper[T]) DropLastWhile ¶ added in v0.41.0
DropLastWhile returns the prefix of ts remaining after dropping the longest suffix of elements that satisfy fn. The result aliases the input backing array. fn must not be nil.
func (Mapper[T]) DropWhile ¶ added in v0.41.0
DropWhile returns the suffix of ts remaining after dropping the longest prefix of elements that satisfy fn. The result aliases the input backing array. fn must not be nil.
func (Mapper[T]) Each ¶
func (ts Mapper[T]) Each(fn func(T))
Each applies fn to each member of ts in index order. fn must not be nil.
func (Mapper[T]) EachIndexed ¶ added in v0.96.0
EachIndexed applies fn to each element's index and value in index order. fn must not be nil.
func (Mapper[T]) Every ¶
Every reports whether fn returns true for every element. Scans elements in index order and stops at the first mismatch. Returns true for an empty slice (vacuous truth). fn must not be nil.
func (Mapper[T]) Find ¶
Find returns the first element matching the predicate, or not-ok if none match. Scans elements in index order and stops at the first match. fn must not be nil.
func (Mapper[T]) FindLast ¶ added in v0.41.0
FindLast returns the last element matching the predicate, or not-ok if none match. Scans elements in reverse index order and stops at the first match. fn must not be nil.
func (Mapper[T]) FlatMap ¶
FlatMap applies fn to each element, concatenating the resulting slices in index order. Nil slices returned by fn are treated as empty. The result is always non-nil. fn must not be nil.
func (Mapper[T]) IndexWhere ¶
IndexWhere returns the index of the first element matching the predicate, or not-ok if none match. Scans elements in index order and stops at the first match. fn must not be nil.
func (Mapper[T]) Intersperse ¶ added in v0.41.0
Intersperse inserts sep between every adjacent pair of elements. Returns a new slice; the result does not alias the input.
func (Mapper[T]) IsSorted ¶ added in v0.41.0
IsSorted reports whether ts is sorted according to cmp.
func (Mapper[T]) KeepIf ¶
KeepIf returns a new slice containing the members of ts for which fn returns true. It is the complement of Mapper.RemoveIf. fn must not be nil.
func (Mapper[T]) KeepIfWhen ¶ added in v0.45.0
KeepIfWhen conditionally filters: when cond is true, behaves like Mapper.KeepIf; when cond is false, returns ts unchanged. This preserves method chaining when a filter should only apply based on an external condition (e.g., an optional query parameter). If cond is true, fn must not be nil.
func (Mapper[T]) KeyByInt ¶ added in v0.41.0
KeyByInt indexes elements by an int key derived from fn. If multiple elements produce the same key, the last one wins. For other key types, use the standalone KeyBy function.
func (Mapper[T]) KeyByString ¶ added in v0.41.0
KeyByString indexes elements by a string key derived from fn. If multiple elements produce the same key, the last one wins. For other key types, use the standalone KeyBy function.
func (Mapper[T]) Last ¶ added in v0.36.0
Last returns the last element, or not-ok if the slice is empty.
func (Mapper[T]) LastIndexWhere ¶ added in v0.41.0
LastIndexWhere returns the index of the last element matching the predicate, or not-ok if none match. Scans elements in reverse index order and stops at the first match. fn must not be nil.
func (Mapper[T]) None ¶
None reports whether fn returns false for every element. Scans elements in index order and stops at the first match. Returns true for an empty slice (no elements match). fn must not be nil.
func (Mapper[T]) PEach ¶ added in v0.41.0
PEach applies fn to each member of m, using the specified number of worker goroutines. The fn must be safe for concurrent use.
Panics in fn are recovered, converted to *rslt.PanicError with a stack captured during recovery, and re-panicked on the calling goroutine after all workers exit.
func (Mapper[T]) PKeepIf ¶ added in v0.41.0
PKeepIf returns a new slice containing members for which fn returns true, using the specified number of worker goroutines. Order is preserved.
Panics in fn are recovered, converted to *rslt.PanicError with a stack captured during recovery, and re-panicked on the calling goroutine after all workers exit.
func (Mapper[T]) Partition ¶ added in v0.41.0
Partition splits ts into two slices: elements where fn returns true, and elements where it returns false. Single pass. Both results are independent slices. fn must not be nil. For use in standalone form, see the Partition function in the slice package.
func (Mapper[T]) RemoveIf ¶
RemoveIf returns a new slice containing members for which fn returns false. It is the complement of Mapper.KeepIf. fn must not be nil.
func (Mapper[T]) RemoveIfWhen ¶ added in v0.45.0
RemoveIfWhen conditionally filters: when cond is true, behaves like Mapper.RemoveIf; when cond is false, returns ts unchanged. This preserves method chaining when a filter should only apply based on an external condition. If cond is true, fn must not be nil.
func (Mapper[T]) Sample ¶ added in v0.41.0
Sample returns a random element from ts, or not-ok if ts is empty.
func (Mapper[T]) Samples ¶ added in v0.41.0
Samples returns count random elements from ts without replacement. If count >= len(ts), returns all elements in random order. Returns empty for count <= 0 or empty ts.
func (Mapper[T]) Single ¶
Single returns Right(element) if exactly one element exists, or Left(count) if zero or more than one.
func (Mapper[T]) Sort ¶
Sort returns a sorted copy using an unstable sort; elements that compare equal may be reordered. cmp must define a strict weak ordering: negative means a < b, zero means equal, positive means a > b. It must be consistent and transitive. Build comparators from key extractors using Asc or Desc.
func (Mapper[T]) Take ¶
Take returns the first n elements of ts. Negative n is treated as zero; n greater than len(ts) is treated as len(ts). The result aliases the input backing array.
func (Mapper[T]) TakeLast ¶
TakeLast returns the last n elements of ts. Negative n is treated as zero; n greater than len(ts) returns the entire slice. The result aliases the input backing array.
func (Mapper[T]) TakeWhile ¶ added in v0.41.0
TakeWhile returns the longest prefix of elements that satisfy fn. The result aliases the input backing array. fn must not be nil.
func (Mapper[T]) ToAny ¶
ToAny returns the result of applying fn to each member of ts. fn must not be nil.
func (Mapper[T]) ToBool ¶
ToBool returns the result of applying fn to each member of ts. fn must not be nil.
func (Mapper[T]) ToByte ¶
ToByte returns the result of applying fn to each member of ts. fn must not be nil.
func (Mapper[T]) ToError ¶
ToError returns the result of applying fn to each member of ts. fn must not be nil.
func (Mapper[T]) ToFloat32 ¶
ToFloat32 returns the result of applying fn to each member of ts. fn must not be nil.
func (Mapper[T]) ToFloat64 ¶
ToFloat64 returns the result of applying fn to each member of ts. fn must not be nil.
func (Mapper[T]) ToInt ¶
ToInt returns the result of applying fn to each member of ts. fn must not be nil.
func (Mapper[T]) ToInt32 ¶
ToInt32 returns the result of applying fn to each member of ts. fn must not be nil.
func (Mapper[T]) ToInt64 ¶
ToInt64 returns the result of applying fn to each member of ts. fn must not be nil.
func (Mapper[T]) ToRune ¶
ToRune returns the result of applying fn to each member of ts. fn must not be nil.
type String ¶
type String []string
func (String) ContainsAny ¶
ContainsAny returns true if ss contains any element in targets. Returns false if either slice is empty.
func (String) Matches ¶
Matches returns true if filter is empty (no constraint) or if ss contains any element in filter. Complement of ContainsAny: empty filter means "everything matches" (allowlist semantics).