base

package
v0.42.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindAs

func FindAs[R, T any](ts []T) option.Option[R]

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

func Partition[T any](ts []T, fn func(T) bool) ([]T, []T)

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

func (e Entries[K, V]) KeepIf(fn func(K, V) bool) Entries[K, V]

KeepIf returns a new Entries containing only the key-value pairs where fn returns true.

func (Entries[K, V]) Keys

func (e Entries[K, V]) Keys() Mapper[K]

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

func (e Entries[K, V]) RemoveIf(fn func(K, V) bool) Entries[K, V]

RemoveIf returns a new Entries containing only the key-value pairs where fn returns false.

func (Entries[K, V]) ToAny

func (e Entries[K, V]) ToAny(fn func(K, V) any) Mapper[any]

ToAny returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).

func (Entries[K, V]) ToBool

func (e Entries[K, V]) ToBool(fn func(K, V) bool) Mapper[bool]

ToBool returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).

func (Entries[K, V]) ToByte

func (e Entries[K, V]) ToByte(fn func(K, V) byte) Mapper[byte]

ToByte returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).

func (Entries[K, V]) ToError

func (e Entries[K, V]) ToError(fn func(K, V) error) Mapper[error]

ToError returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).

func (Entries[K, V]) ToFloat32

func (e Entries[K, V]) ToFloat32(fn func(K, V) float32) Mapper[float32]

ToFloat32 returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).

func (Entries[K, V]) ToFloat64

func (e Entries[K, V]) ToFloat64(fn func(K, V) float64) Float64

ToFloat64 returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).

func (Entries[K, V]) ToInt

func (e Entries[K, V]) ToInt(fn func(K, V) int) Int

ToInt returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).

func (Entries[K, V]) ToInt32

func (e Entries[K, V]) ToInt32(fn func(K, V) int32) Mapper[int32]

ToInt32 returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).

func (Entries[K, V]) ToInt64

func (e Entries[K, V]) ToInt64(fn func(K, V) int64) Mapper[int64]

ToInt64 returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).

func (Entries[K, V]) ToRune

func (e Entries[K, V]) ToRune(fn func(K, V) rune) Mapper[rune]

ToRune returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).

func (Entries[K, V]) ToString

func (e Entries[K, V]) ToString(fn func(K, V) string) String

ToString returns the result of applying fn to each key-value pair. Order is not guaranteed (map iteration order).

func (Entries[K, V]) Values

func (e Entries[K, V]) Values() Mapper[V]

Values extracts the values as a Mapper for further transformation. Order is not guaranteed (map iteration order).

type Float64

type Float64 []float64

func (Float64) Max

func (fs Float64) Max() option.Option[float64]

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

func (fs Float64) Min() option.Option[float64]

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.

func (Float64) Sum

func (fs Float64) Sum() float64

Sum returns the sum of all elements.

type Int

type Int []int

func (Int) Max

func (is Int) Max() option.Option[int]

Max returns the largest element, or not-ok if the slice is empty.

func (Int) Min

func (is Int) Min() option.Option[int]

Min returns the smallest element, or not-ok if the slice is empty.

func (Int) Sum

func (is Int) Sum() int

Sum returns the sum of all elements.

type Mapper

type Mapper[T any] []T

Mapper is a fluent slice usable anywhere a regular slice is, but provides additional fluent fp methods. Its underlying type is []T.

func PFlatMap added in v0.41.0

func PFlatMap[T, R any](m Mapper[T], workers int, fn func(T) []R) Mapper[R]

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

func PMap[T, R any](m Mapper[T], workers int, fn func(T) R) Mapper[R]

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

func (ts Mapper[T]) Any(fn func(T) bool) bool

Any returns true if fn returns true for any element.

func (Mapper[T]) Clone

func (ts Mapper[T]) Clone() Mapper[T]

Clone returns a shallow copy of the slice with independent backing array.

func (Mapper[T]) Convert

func (ts Mapper[T]) Convert(fn func(T) T) Mapper[T]

Convert returns the result of applying fn to each member of ts.

func (Mapper[T]) Drop added in v0.41.0

func (ts Mapper[T]) Drop(n int) Mapper[T]

Drop returns ts without the first n elements. Returns empty if n >= len(ts). Negative n is treated as zero.

func (Mapper[T]) DropLast added in v0.41.0

func (ts Mapper[T]) DropLast(n int) Mapper[T]

DropLast returns ts without the last n elements. Returns empty if n >= len(ts). Negative n is treated as zero.

func (Mapper[T]) DropLastWhile added in v0.41.0

func (ts Mapper[T]) DropLastWhile(fn func(T) bool) Mapper[T]

DropLastWhile returns the prefix of ts remaining after dropping the longest suffix of elements that satisfy fn.

func (Mapper[T]) DropWhile added in v0.41.0

func (ts Mapper[T]) DropWhile(fn func(T) bool) Mapper[T]

DropWhile returns the suffix of ts remaining after dropping the longest prefix of elements that satisfy fn.

func (Mapper[T]) Each

func (ts Mapper[T]) Each(fn func(T))

Each applies fn to each member of ts.

func (Mapper[T]) Every

func (ts Mapper[T]) Every(fn func(T) bool) bool

Every returns true if fn returns true for every element. Returns true for an empty slice (vacuous truth).

func (Mapper[T]) Find

func (ts Mapper[T]) Find(fn func(T) bool) option.Option[T]

Find returns the first element matching the predicate, or not-ok if none match.

func (Mapper[T]) FindLast added in v0.41.0

func (ts Mapper[T]) FindLast(fn func(T) bool) option.Option[T]

FindLast returns the last element matching the predicate, or not-ok if none match.

func (Mapper[T]) First

func (ts Mapper[T]) First() option.Option[T]

First returns the first element, or not-ok if the slice is empty.

func (Mapper[T]) FlatMap

func (ts Mapper[T]) FlatMap(fn func(T) []T) Mapper[T]

FlatMap applies fn to each element, concatenating the resulting slices in iteration order. Nil slices returned by fn are treated as empty. The result is always non-nil.

func (Mapper[T]) IndexWhere

func (ts Mapper[T]) IndexWhere(fn func(T) bool) option.Option[int]

IndexWhere returns the index of the first element matching the predicate, or not-ok if none match.

func (Mapper[T]) Intersperse added in v0.41.0

func (ts Mapper[T]) Intersperse(sep T) Mapper[T]

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

func (ts Mapper[T]) IsSorted(cmp func(T, T) int) bool

IsSorted reports whether ts is sorted according to cmp.

func (Mapper[T]) KeepIf

func (ts Mapper[T]) KeepIf(fn func(T) bool) Mapper[T]

KeepIf returns a new slice containing the members of ts for which fn returns true. It is the complement of RemoveIf.

func (Mapper[T]) KeyByInt added in v0.41.0

func (ts Mapper[T]) KeyByInt(fn func(T) int) map[int]T

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

func (ts Mapper[T]) KeyByString(fn func(T) string) map[string]T

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

func (ts Mapper[T]) Last() option.Option[T]

Last returns the last element, or not-ok if the slice is empty.

func (Mapper[T]) LastIndexWhere added in v0.41.0

func (ts Mapper[T]) LastIndexWhere(fn func(T) bool) option.Option[int]

LastIndexWhere returns the index of the last element matching the predicate, or not-ok if none match.

func (Mapper[T]) Len

func (ts Mapper[T]) Len() int

Len returns the length of the slice.

func (Mapper[T]) None

func (ts Mapper[T]) None(fn func(T) bool) bool

None returns true if fn returns false for every element. Returns true for an empty slice (no elements match).

func (Mapper[T]) PEach added in v0.41.0

func (m Mapper[T]) PEach(workers int, fn func(T))

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

func (m Mapper[T]) PKeepIf(workers int, fn func(T) bool) Mapper[T]

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

func (ts Mapper[T]) Partition(fn func(T) bool) (Mapper[T], Mapper[T])

Partition splits ts into two slices: elements where fn returns true, and elements where it returns false. Single pass. Both results are independent slices. For use in standalone form, see the Partition function in the slice package.

func (Mapper[T]) RemoveIf

func (ts Mapper[T]) RemoveIf(fn func(T) bool) Mapper[T]

RemoveIf returns a new slice containing members for which fn returns false. It is the complement of KeepIf.

func (Mapper[T]) Reverse

func (ts Mapper[T]) Reverse() Mapper[T]

Reverse returns a new slice with elements in reverse order.

func (Mapper[T]) Sample added in v0.41.0

func (ts Mapper[T]) Sample() option.Option[T]

Sample returns a random element from ts, or not-ok if ts is empty.

func (Mapper[T]) Samples added in v0.41.0

func (ts Mapper[T]) Samples(count int) Mapper[T]

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]) Shuffle added in v0.41.0

func (ts Mapper[T]) Shuffle() Mapper[T]

Shuffle returns a randomly reordered copy of ts.

func (Mapper[T]) Single

func (ts Mapper[T]) Single() either.Either[int, T]

Single returns Right(element) if exactly one element exists, or Left(count) if zero or more than one.

func (Mapper[T]) Sort

func (ts Mapper[T]) Sort(cmp func(T, T) int) Mapper[T]

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

func (ts Mapper[T]) Take(n int) Mapper[T]

Take returns the first n elements of ts.

func (Mapper[T]) TakeLast

func (ts Mapper[T]) TakeLast(n int) Mapper[T]

TakeLast returns the last n elements of ts.

func (Mapper[T]) TakeWhile added in v0.41.0

func (ts Mapper[T]) TakeWhile(fn func(T) bool) Mapper[T]

TakeWhile returns the longest prefix of elements that satisfy fn.

func (Mapper[T]) ToAny

func (ts Mapper[T]) ToAny(fn func(T) any) Mapper[any]

ToAny returns the result of applying fn to each member of ts.

func (Mapper[T]) ToBool

func (ts Mapper[T]) ToBool(fn func(T) bool) Mapper[bool]

ToBool returns the result of applying fn to each member of ts.

func (Mapper[T]) ToByte

func (ts Mapper[T]) ToByte(fn func(T) byte) Mapper[byte]

ToByte returns the result of applying fn to each member of ts.

func (Mapper[T]) ToError

func (ts Mapper[T]) ToError(fn func(T) error) Mapper[error]

ToError returns the result of applying fn to each member of ts.

func (Mapper[T]) ToFloat32

func (ts Mapper[T]) ToFloat32(fn func(T) float32) Mapper[float32]

ToFloat32 returns the result of applying fn to each member of ts.

func (Mapper[T]) ToFloat64

func (ts Mapper[T]) ToFloat64(fn func(T) float64) Float64

ToFloat64 returns the result of applying fn to each member of ts.

func (Mapper[T]) ToInt

func (ts Mapper[T]) ToInt(fn func(T) int) Int

ToInt returns the result of applying fn to each member of ts.

func (Mapper[T]) ToInt32

func (ts Mapper[T]) ToInt32(fn func(T) int32) Mapper[int32]

ToInt32 returns the result of applying fn to each member of ts.

func (Mapper[T]) ToInt64

func (ts Mapper[T]) ToInt64(fn func(T) int64) Mapper[int64]

ToInt64 returns the result of applying fn to each member of ts.

func (Mapper[T]) ToRune

func (ts Mapper[T]) ToRune(fn func(T) rune) Mapper[rune]

ToRune returns the result of applying fn to each member of ts.

func (Mapper[T]) ToString

func (ts Mapper[T]) ToString(fn func(T) string) String

ToString returns the result of applying fn to each member of ts.

type String

type String []string

func (String) Contains

func (ss String) Contains(target string) bool

Contains returns true if ss contains target.

func (String) ContainsAny

func (ss String) ContainsAny(targets []string) bool

ContainsAny returns true if ss contains any element in targets. Returns false if either slice is empty.

func (String) Each

func (ss String) Each(fn func(string))

Each calls fn for every element.

func (String) Join added in v0.41.0

func (ss String) Join(sep string) string

Join concatenates the elements with sep between each.

func (String) Len

func (ss String) Len() int

Len returns the length of the slice.

func (String) Matches

func (ss String) Matches(filter []string) bool

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).

func (String) NonEmpty added in v0.41.0

func (ss String) NonEmpty() String

NonEmpty removes empty strings.

func (String) ToSet

func (ss String) ToSet() map[string]bool

ToSet returns a map with each string as a key set to true.

func (String) Unique

func (ss String) Unique() String

Unique returns a new slice with duplicate strings removed, preserving order.

Jump to

Keyboard shortcuts

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