base

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 4 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 ToSet

func ToSet[T comparable](ts []T) map[T]bool

ToSet returns a map with each element as a key set to true. 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.

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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 EntryMapper

type EntryMapper[T any, K comparable, V any] struct {
	// contains filtered or unexported fields
}

EntryMapper wraps a map for cross-type transformation. T is first so K and V are inferred from the map argument.

func NewEntryMapper

func NewEntryMapper[T any, K comparable, V any](m map[K]V) EntryMapper[T, K, V]

NewEntryMapper creates an EntryMapper for transformation to type T.

func (EntryMapper[T, K, V]) Map

func (mt EntryMapper[T, K, V]) Map(fn func(K, V) T) Mapper[T]

Map transforms each key-value pair using fn and returns the results 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() float64

Max returns the largest element, or zero if the slice is empty.

func (Float64) Min

func (fs Float64) Min() float64

Min returns the smallest element, or zero if the slice is empty.

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() int

Max returns the largest element, or zero if the slice is empty.

func (Int) Min

func (is Int) Min() int

Min returns the smallest element, or zero 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 ParallelMap

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

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

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]) 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]) 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]) 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]) 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]) 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]) ParallelEach

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

ParallelEach applies fn to each member of m, using the specified number of worker goroutines. The fn must be safe for concurrent use.

func (Mapper[T]) ParallelKeepIf

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

ParallelKeepIf returns a new slice containing members for which fn returns true, using the specified number of worker goroutines. Order is preserved.

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]) 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 cmp (negative = a < b, zero = equal, positive = a > b). 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]) 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 MapperTo

type MapperTo[R, T any] []T

MapperTo is a fluent slice for filter→map chains where the cross-type map comes last. Prefer slice.Map(ts, fn) for most cross-type mapping — it infers all types and returns Mapper[R] for further chaining. Use MapTo[R] only when you need to filter or transform before the cross-type map: slice.MapTo[R](ts).KeepIf(pred).Map(fn).

func (MapperTo[R, T]) Clone

func (ts MapperTo[R, T]) Clone() MapperTo[R, T]

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

func (MapperTo[R, T]) Convert

func (ts MapperTo[R, T]) Convert(fn func(T) T) MapperTo[R, T]

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

func (MapperTo[R, T]) Each

func (ts MapperTo[R, T]) Each(fn func(T))

Each applies fn to each member of ts.

func (MapperTo[R, T]) First

func (ts MapperTo[R, T]) First() option.Option[T]

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

func (MapperTo[R, T]) FlatMap

func (ts MapperTo[R, T]) FlatMap(fn func(T) []R) Mapper[R]

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 (MapperTo[R, T]) KeepIf

func (ts MapperTo[R, T]) KeepIf(fn func(T) bool) MapperTo[R, T]

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

func (MapperTo[R, T]) Last added in v0.36.0

func (ts MapperTo[R, T]) Last() option.Option[T]

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

func (MapperTo[R, T]) Len

func (ts MapperTo[R, T]) Len() int

Len returns the length of the slice.

func (MapperTo[R, T]) Map

func (ts MapperTo[R, T]) Map(fn func(T) R) Mapper[R]

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

func (MapperTo[R, T]) ParallelEach

func (ts MapperTo[R, T]) ParallelEach(workers int, fn func(T))

ParallelEach applies fn to each member of ts, using the specified number of worker goroutines. The fn must be safe for concurrent use.

func (MapperTo[R, T]) ParallelKeepIf

func (ts MapperTo[R, T]) ParallelKeepIf(workers int, fn func(T) bool) MapperTo[R, T]

ParallelKeepIf returns a new slice containing members for which fn returns true, using the specified number of worker goroutines. Order is preserved.

func (MapperTo[R, T]) ParallelMap

func (ts MapperTo[R, T]) ParallelMap(workers int, fn func(T) R) Mapper[R]

ParallelMap returns the result of applying fn to each member of ts, using the specified number of worker goroutines. Order is preserved. The fn must be safe for concurrent use.

func (MapperTo[R, T]) RemoveIf

func (ts MapperTo[R, T]) RemoveIf(fn func(T) bool) MapperTo[R, T]

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

func (MapperTo[R, T]) Reverse

func (ts MapperTo[R, T]) Reverse() MapperTo[R, T]

Reverse returns a new slice with elements in reverse order.

func (MapperTo[R, T]) Single

func (ts MapperTo[R, 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 (MapperTo[R, T]) Sort

func (ts MapperTo[R, T]) Sort(cmp func(T, T) int) MapperTo[R, T]

Sort returns a sorted copy using cmp (negative = a < b, zero = equal, positive = a > b). Build comparators from key extractors using Asc or Desc.

func (MapperTo[R, T]) Take

func (ts MapperTo[R, T]) Take(n int) MapperTo[R, T]

Take returns the first n members of ts.

func (MapperTo[R, T]) TakeLast

func (ts MapperTo[R, T]) TakeLast(n int) MapperTo[R, T]

TakeLast returns the last n members of ts.

func (MapperTo[R, T]) ToAny

func (ts MapperTo[R, T]) ToAny(fn func(T) any) MapperTo[R, any]

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

func (MapperTo[R, T]) ToBool

func (ts MapperTo[R, T]) ToBool(fn func(T) bool) MapperTo[R, bool]

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

func (MapperTo[R, T]) ToByte

func (ts MapperTo[R, T]) ToByte(fn func(T) byte) MapperTo[R, byte]

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

func (MapperTo[R, T]) ToError

func (ts MapperTo[R, T]) ToError(fn func(T) error) MapperTo[R, error]

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

func (MapperTo[R, T]) ToFloat32

func (ts MapperTo[R, T]) ToFloat32(fn func(T) float32) MapperTo[R, float32]

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

func (MapperTo[R, T]) ToFloat64

func (ts MapperTo[R, T]) ToFloat64(fn func(T) float64) MapperTo[R, float64]

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

func (MapperTo[R, T]) ToInt

func (ts MapperTo[R, T]) ToInt(fn func(T) int) MapperTo[R, int]

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

func (MapperTo[R, T]) ToInt32

func (ts MapperTo[R, T]) ToInt32(fn func(T) int32) MapperTo[R, int32]

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

func (MapperTo[R, T]) ToInt64

func (ts MapperTo[R, T]) ToInt64(fn func(T) int64) MapperTo[R, int64]

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

func (MapperTo[R, T]) ToRune

func (ts MapperTo[R, T]) ToRune(fn func(T) rune) MapperTo[R, rune]

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

func (MapperTo[R, T]) ToString

func (ts MapperTo[R, T]) ToString(fn func(T) string) MapperTo[R, 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) 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 ss contains any element in filter. Returns true if filter is empty (no constraint).

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