Documentation
¶
Overview ¶
Package slice provides fluent slice types that can chain functional collection operations.
Mapper[T] is a fluent slice that can chain operations like ToString (map), KeepIf (filter), etc.
MapperTo[T, R] is a fluent slice with one additional method, MapTo, for mapping to a specified type R. If you don't need to map to an arbitrary type, use Mapper instead.
Index ¶
- func Fold[T, R any](ts []T, initial R, fn func(R, T) R) R
- func Unzip2[T, A, B any](ts []T, fa func(T) A, fb func(T) B) (Mapper[A], Mapper[B])
- func Unzip3[T, A, B, C any](ts []T, fa func(T) A, fb func(T) B, fc func(T) C) (Mapper[A], Mapper[B], Mapper[C])
- func Unzip4[T, A, B, C, D any](ts []T, fa func(T) A, fb func(T) B, fc func(T) C, fd func(T) D) (Mapper[A], Mapper[B], Mapper[C], Mapper[D])
- type Any
- type Bool
- type Byte
- type Error
- type Float32
- type Float64
- type Int
- type Mapper
- func (ts Mapper[T]) Convert(fn func(T) T) Mapper[T]
- func (ts Mapper[T]) Each(fn func(T))
- func (ts Mapper[T]) KeepIf(fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) Len() int
- func (ts Mapper[T]) RemoveIf(fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) TakeFirst(n int) 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) Mapper[float64]
- func (ts Mapper[T]) ToInt(fn func(T) int) Mapper[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) Mapper[string]
- type MapperTo
- func (ts MapperTo[R, T]) Convert(fn func(T) T) MapperTo[R, T]
- func (ts MapperTo[R, T]) Each(fn func(T))
- func (ts MapperTo[R, T]) KeepIf(fn func(T) bool) MapperTo[R, T]
- func (ts MapperTo[T, R]) Len() int
- func (ts MapperTo[R, T]) RemoveIf(fn func(T) bool) MapperTo[R, T]
- func (ts MapperTo[R, T]) TakeFirst(n int) MapperTo[R, T]
- func (ts MapperTo[R, T]) To(fn func(T) R) Mapper[R]
- func (ts MapperTo[R, T]) ToAny(fn func(T) any) MapperTo[R, any]
- func (ts MapperTo[R, T]) ToBool(fn func(T) bool) MapperTo[R, bool]
- func (ts MapperTo[R, T]) ToByte(fn func(T) byte) MapperTo[R, byte]
- func (ts MapperTo[R, T]) ToError(fn func(T) error) MapperTo[R, error]
- func (ts MapperTo[R, T]) ToFloat32(fn func(T) float32) MapperTo[R, float32]
- func (ts MapperTo[R, T]) ToFloat64(fn func(T) float64) MapperTo[R, float64]
- func (ts MapperTo[R, T]) ToInt(fn func(T) int) MapperTo[R, int]
- func (ts MapperTo[R, T]) ToInt32(fn func(T) int32) MapperTo[R, int32]
- func (ts MapperTo[R, T]) ToInt64(fn func(T) int64) MapperTo[R, int64]
- func (ts MapperTo[R, T]) ToRune(fn func(T) rune) MapperTo[R, rune]
- func (ts MapperTo[R, T]) ToString(fn func(T) string) MapperTo[R, string]
- type Rune
- type String
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fold ¶ added in v0.6.0
func Fold[T, R any](ts []T, initial R, fn func(R, T) R) R
Fold reduces a slice to a single value by applying fn to each element. It starts with initial and applies fn(accumulator, element) for each element from left to right. Returns initial if the slice is empty.
func Unzip2 ¶ added in v0.6.0
Unzip2 extracts two slices from ts in a single pass by applying the extraction functions. This is more efficient than calling two separate mapping operations when you need multiple fields.
Types ¶
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 (Mapper[T]) KeepIf ¶
KeepIf returns a new slice containing the members of ts for which fn returns true. It is the complement of RemoveIf.
func (Mapper[T]) RemoveIf ¶
RemoveIf returns a new slice containing members for which fn returns false. It is the complement of KeepIf.
func (Mapper[T]) ToInt32 ¶ added in v0.8.0
ToInt32 returns the result of applying fn to each member of ts.
func (Mapper[T]) ToInt64 ¶ added in v0.8.0
ToInt64 returns the result of applying fn to each member of ts.
type MapperTo ¶
type MapperTo[R, T any] []T
MapperTo is a fluent slice with one additional method, MapTo, for mapping to a specified type R. If you don't need to map to an arbitrary type, use Mapper instead.
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]) KeepIf ¶
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]) RemoveIf ¶
RemoveIf returns a new slice containing members for which fn returns false. It is the complement of KeepIf.
func (MapperTo[R, T]) ToInt32 ¶ added in v0.8.0
ToInt32 returns the result of applying fn to each member of ts.
func (MapperTo[R, T]) ToInt64 ¶ added in v0.8.0
ToInt64 returns the result of applying fn to each member of ts.