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 Chunk[T any](ts []T, size int) [][]T
- func Contains[T comparable](ts []T, target T) bool
- func FindAs[R, T any](ts []T) option.Basic[R]
- func Fold[T, R any](ts []T, initial R, fn func(R, T) R) R
- func ToSet[T comparable](ts []T) map[T]bool
- func ToSetBy[T any, K comparable](ts []T, fn func(T) K) map[K]bool
- 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 Compact[T comparable](ts []T) Mapper[T]
- func From[T any](ts []T) Mapper[T]
- func MapAccum[T, R, S any](ts []T, init S, fn func(S, T) (S, R)) (S, Mapper[R])
- func ParallelMap[T, R any](m Mapper[T], workers int, fn func(T) R) Mapper[R]
- func SortBy[T any, K cmp.Ordered](ts []T, fn func(T) K) Mapper[T]
- func SortByDesc[T any, K cmp.Ordered](ts []T, fn func(T) K) Mapper[T]
- func UniqueBy[T any, K comparable](ts []T, fn func(T) K) Mapper[T]
- func (ts Mapper[T]) Any(fn func(T) bool) bool
- func (ts Mapper[T]) Clone() Mapper[T]
- func (ts Mapper[T]) Convert(fn func(T) T) Mapper[T]
- func (ts Mapper[T]) Each(fn func(T))
- func (ts Mapper[T]) Every(fn func(T) bool) bool
- func (ts Mapper[T]) Find(fn func(T) bool) option.Basic[T]
- func (ts Mapper[T]) First() option.Basic[T]
- func (ts Mapper[T]) FlatMap(fn func(T) []T) Mapper[T]
- func (ts Mapper[T]) IndexWhere(fn func(T) bool) option.Basic[int]
- func (ts Mapper[T]) KeepIf(fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) Len() int
- func (ts Mapper[T]) None(fn func(T) bool) bool
- func (m Mapper[T]) ParallelEach(workers int, fn func(T))
- func (m Mapper[T]) ParallelKeepIf(workers int, fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) RemoveIf(fn func(T) bool) Mapper[T]
- func (ts Mapper[T]) Reverse() Mapper[T]
- func (ts Mapper[T]) Single() either.Either[int, T]
- func (ts Mapper[T]) Take(n int) Mapper[T]
- func (ts Mapper[T]) TakeLast(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) 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
- type MapperTo
- func (ts MapperTo[R, T]) Clone() MapperTo[R, T]
- 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]) First() option.Basic[T]
- func (ts MapperTo[R, T]) FlatMap(fn func(T) []R) Mapper[R]
- func (ts MapperTo[R, T]) KeepIf(fn func(T) bool) MapperTo[R, T]
- func (ts MapperTo[R, T]) Len() int
- func (ts MapperTo[R, T]) Map(fn func(T) R) Mapper[R]
- func (ts MapperTo[R, T]) ParallelEach(workers int, fn func(T))
- func (ts MapperTo[R, T]) ParallelKeepIf(workers int, fn func(T) bool) MapperTo[R, T]
- func (ts MapperTo[R, T]) ParallelMap(workers int, fn func(T) R) Mapper[R]
- func (ts MapperTo[R, T]) RemoveIf(fn func(T) bool) MapperTo[R, T]
- func (ts MapperTo[R, T]) Reverse() MapperTo[R, T]
- func (ts MapperTo[R, T]) Single() either.Either[int, T]
- func (ts MapperTo[R, T]) Take(n int) MapperTo[R, T]
- func (ts MapperTo[R, T]) TakeLast(n int) MapperTo[R, T]
- 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
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chunk ¶ added in v0.28.0
Chunk splits ts into sub-slices of at most size elements. The last chunk may have fewer than size elements. Panics if size <= 0.
func Contains ¶ added in v0.28.0
func Contains[T comparable](ts []T, target T) bool
Contains returns true if ts contains target.
func FindAs ¶ added in v0.21.0
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 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 ToSet ¶ added in v0.23.0
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.
func ToSetBy ¶ added in v0.28.0
func ToSetBy[T any, K comparable](ts []T, fn func(T) K) map[K]bool
ToSetBy returns a map with each element's key (extracted by fn) set to true. Useful when elements aren't directly comparable but have a comparable key field.
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 Float64 ¶
type Float64 []float64
func (Float64) Max ¶ added in v0.23.0
Max returns the largest element, or zero if the slice is empty.
type Int ¶
type Int []int
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 Compact ¶ added in v0.28.0
func Compact[T comparable](ts []T) Mapper[T]
Compact removes zero-value elements from ts.
func MapAccum ¶ added in v0.25.0
MapAccum threads state through a slice, producing both a final state and a mapped output. fn receives the accumulated state and current element, returning new state and an output value. Returns init and an empty slice if ts is empty.
func ParallelMap ¶ added in v0.23.0
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.
Example ¶
package main
import (
"fmt"
"runtime"
"github.com/binaryphile/fluentfp/slice"
)
func main() {
double := func(n int) int { return n * 2 }
result := slice.ParallelMap(slice.From([]int{1, 2, 3, 4, 5}), runtime.GOMAXPROCS(0), double)
fmt.Println([]int(result))
}
Output: [2 4 6 8 10]
func SortBy ¶ added in v0.23.0
SortBy returns a sorted copy of ts, ordered ascending by the key extracted via fn.
func SortByDesc ¶ added in v0.23.0
SortByDesc returns a sorted copy of ts, ordered descending by the key extracted via fn.
func UniqueBy ¶ added in v0.28.0
func UniqueBy[T any, K comparable](ts []T, fn func(T) K) Mapper[T]
UniqueBy returns a new slice with duplicate elements removed, where duplicates are determined by the key returned by fn. Preserves first occurrence and maintains order.
func (Mapper[T]) Clone ¶ added in v0.23.0
Clone returns a shallow copy of the slice with independent backing array.
func (Mapper[T]) Every ¶ added in v0.28.0
Every returns true if fn returns true for every element. Returns true for an empty slice (vacuous truth).
func (Mapper[T]) Find ¶ added in v0.17.0
Find returns the first element matching the predicate, or not-ok if none match.
func (Mapper[T]) First ¶ added in v0.20.0
First returns the first element, or not-ok if the slice is empty.
func (Mapper[T]) FlatMap ¶ added in v0.26.0
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 ¶ added in v0.23.0
IndexWhere returns the index of the first element matching the predicate, or not-ok if none match.
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]) None ¶ added in v0.28.0
None returns true if fn returns false for every element. Returns true for an empty slice (no elements match).
func (Mapper[T]) ParallelEach ¶ added in v0.23.0
ParallelEach applies fn to each member of m, using the specified number of worker goroutines. The fn must be safe for concurrent use.
Example ¶
package main
import (
"github.com/binaryphile/fluentfp/slice"
)
func main() {
slice.From([]string{"a", "b", "c"}).ParallelEach(2, func(s string) {
// process each element concurrently
_ = s
})
}
func (Mapper[T]) ParallelKeepIf ¶ added in v0.23.0
ParallelKeepIf returns a new slice containing members for which fn returns true, using the specified number of worker goroutines. Order is preserved.
Example ¶
package main
import (
"fmt"
"github.com/binaryphile/fluentfp/slice"
)
func main() {
isEven := func(n int) bool { return n%2 == 0 }
result := slice.From([]int{1, 2, 3, 4, 5, 6}).ParallelKeepIf(4, isEven)
fmt.Println([]int(result))
}
Output: [2 4 6]
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]) Reverse ¶ added in v0.28.0
Reverse returns a new slice with elements in reverse order.
func (Mapper[T]) Single ¶ added in v0.23.0
Single returns Right(element) if exactly one element exists, or Left(count) if zero or more than one.
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]) Clone ¶ added in v0.23.0
Clone returns a shallow copy of the slice with independent backing array.
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 ¶ added in v0.20.0
First returns the first element, or not-ok if the slice is empty.
func (MapperTo[R, T]) FlatMap ¶ added in v0.26.0
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 ¶
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]) Map ¶ added in v0.12.0
Map returns the result of applying fn to each member of ts.
func (MapperTo[R, T]) ParallelEach ¶ added in v0.23.0
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 ¶ added in v0.23.0
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 ¶ added in v0.23.0
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 ¶
RemoveIf returns a new slice containing members for which fn returns false. It is the complement of KeepIf.
func (MapperTo[R, T]) Reverse ¶ added in v0.28.0
Reverse returns a new slice with elements in reverse order.
func (MapperTo[R, T]) Single ¶ added in v0.23.0
Single returns Right(element) if exactly one element exists, or Left(count) if zero or more than one.
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.
type String ¶
type String []string
func (String) ContainsAny ¶ added in v0.23.0
ContainsAny returns true if ss contains any element in targets. Returns false if either slice is empty.
func (String) Matches ¶ added in v0.23.0
Matches returns true if ss contains any element in filter. Returns true if filter is empty (no constraint).