Documentation
¶
Overview ¶
Package slice provides fluent slice types that can chain functional collection operations.
Types are defined in internal/base and aliased here. All methods are available through the aliases.
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.
Entries[K, V] is a fluent map type for chaining map operations.
Index ¶
- func Asc[T any, S cmp.Ordered](key func(T) S) func(T, T) int
- func Chunk[T any](ts []T, size int) [][]T
- func Contains[T comparable](ts []T, target T) bool
- func Desc[T any, S cmp.Ordered](key func(T) S) func(T, T) int
- func FindAs[R, T any](ts []T) option.Option[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 Entries
- type Error
- type Float32
- type Float64
- type Group
- type Int
- type Mapper
- func Compact[T comparable](ts []T) Mapper[T]
- func From[T any](ts []T) Mapper[T]
- func FromSet[T comparable](m map[T]bool) Mapper[T]
- func GroupBy[T any, K comparable](ts []T, fn func(T) K) Mapper[Group[K, T]]
- func Map[T, R any](ts []T, fn func(T) R) Mapper[R]
- 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]
- type MapperTo
- 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 Group ¶ added in v0.35.0
type Group[K comparable, T any] struct { Key K Items []T }
Group holds a grouping key and its collected items.
type Mapper ¶
Type aliases for types defined in internal/base. All methods defined on the base types are available through these aliases.
func Compact ¶ added in v0.28.0
func Compact[T comparable](ts []T) Mapper[T]
Compact removes zero-value elements from ts.
func FromSet ¶ added in v0.30.0
func FromSet[T comparable](m map[T]bool) Mapper[T]
FromSet extracts the members of a set (keys where value is true) as a Mapper. Order is not guaranteed (map iteration order).
func GroupBy ¶ added in v0.30.0
func GroupBy[T any, K comparable](ts []T, fn func(T) K) Mapper[Group[K, T]]
GroupBy groups elements by the key returned by fn. Returns Mapper[Group[K, T]] — groups preserve first-seen key order and chain directly.
func Map ¶ added in v0.30.0
Map returns the result of applying fn to each member of ts. It is a standalone function because Go methods cannot introduce new type parameters — the target type R must be inferred from the function argument rather than bound on the receiver.
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.
type MapperTo ¶
func MapTo ¶
MapTo creates a MapperTo 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).