slicex

May 31, 2026 · View on GitHub

sink-logo

slicex

slicex provides generic helpers for working with Go slices in a clear and reusable way.

It is designed to help you replace repetitive slice loops with small functions for:

  • filtering values
  • mapping slices to new types
  • searching for items
  • grouping and partitioning data
  • removing duplicates
  • combining slices into pairs
  • creating sliding windows
  • performing set-like operations
  • simple numeric aggregation

Overview

Use slicex when you want slice logic to be easier to read, reuse, and test.

It is especially useful when:

  • the same slice loop appears in multiple places
  • a helper makes the intent of the code clearer
  • you want type-safe generic utilities instead of custom one-off helpers

When to use it

Use slicex when:

  • you need a common slice operation expressed clearly
  • you want to avoid repeating small loops throughout the codebase
  • you prefer reusable generic helpers over ad hoc implementations

Prefer a simpler local loop when:

  • the operation is tiny and only used once
  • a helper would make the code less obvious
  • performance or allocation behavior needs a specialized implementation

API reference

Get the first matching or default value

FunctionPurpose
FirstOrReturns the first element of a slice or a fallback value
FirstOrEmptyReturns the first element of a slice or the zero value
FirstByReturns the first item satisfying a predicate, and a found boolean
FirstOrByReturns the first item satisfying a predicate, or a fallback value
FindReturns the first item in a slice equal to a given value
FindByReturns the first item in a slice that matches a predicate
FindOrReturns the first item equal to a given value, or a fallback
FindOrByReturns the first item matching a predicate, or a fallback value

Get the last matching or default value

FunctionPurpose
LastOrReturns the last element of a slice or a fallback value
LastOrEmptyReturns the last element of a slice or the zero value
LastByReturns the last item satisfying a predicate, and a found boolean
LastOrByReturns the last item satisfying a predicate, or a fallback value

Test for presence or match

FunctionPurpose
ContainsReturns true if the slice contains a given value
AnyReturns true if any item matches a given value
AnyByReturns true if any item matches a predicate
AllReturns true if all items match a given value
AllByReturns true if all items satisfy a predicate
NoneReturns true if no item equals a given value
NoneByReturns true if no item satisfies a predicate

Filter and select items

FunctionPurpose
FilterReturns only items that satisfy a predicate
FilterWithIndexReturns only items whose predicate receives both the index and value
CompactRemoves zero values from a slice
CompactByRemoves items for which a caller-supplied predicate returns true

Transform or reshape data

FunctionPurpose
MapTransforms each element into a new slice
MapWithIndexTransforms each element using a function that also receives the index
MapErrTransforms each element, stopping and returning the error if one occurs
FilterMapFilters and transforms elements in one pass
FilterMapWithIndexFilters and transforms elements in one pass, with index access
BindMaps each item to a slice and flattens the results (flatMap)
FlattenCollapses a slice of slices into a single flat slice
ReduceFolds slice values into a single accumulated result
ToMapConverts a slice into a map using a key selector
ApplyRuns a function on each item for side effects; does not return a new slice
ApplyWithIndexRuns a function on each item for side effects, also receiving the index

Remove duplicates or keep unique values

FunctionPurpose
UniqueRemoves duplicate values from a slice
UniqueByRemoves duplicates by a derived key
UniqueMapTransforms items and returns only unique results

Count elements

FunctionPurpose
CountReturns the number of elements equal to a given value
CountByReturns the number of elements satisfying a predicate

Reorder slices

FunctionPurpose
ReverseReturns a reversed copy of the slice
RotateReturns a copy with elements shifted left by n positions; negative n shifts right
SortReturns a sorted copy of the slice using natural ordering
SortByReturns a sorted copy of the slice using a comparison function

Group, split, or combine collections

FunctionPurpose
GroupByGroups items into a map by a computed key
PartitionSplits items into two slices based on a predicate
ChunkSplits a slice into sub-slices of at most size n
ZipCombines two slices into a slice of Pair values, pairing elements by index
WindowReturns overlapping sub-slices of a fixed size, advancing one position at a time

Find by index

FunctionPurpose
IndexOfReturns the index of the first element equal to a given value, or -1
IndexByReturns the index of the first element satisfying a predicate, or -1

Perform set-like operations

FunctionPurpose
IntersectReturns values common to both slices
UnionReturns all unique values from both slices
DifferenceReturns values present in one slice but not the other

Aggregate numeric values

FunctionPurpose
SumCalculates the sum of numeric values
SumByCalculates the sum using a value selector
ProductCalculates the product of numeric values
ProductByCalculates the product using a value selector
MeanCalculates the arithmetic mean
MeanByCalculates the mean using a value selector
MinReturns the minimum value
MinByReturns the minimum value using a comparison function
MaxReturns the maximum value
MaxByReturns the maximum value using a comparison function

Notes

  • Prefer the function that most clearly expresses your intent.
  • Prefer the simplest helper that matches the operation.
  • Check each function’s documentation for details such as ordering, stability, and zero-value behavior.
  • For very large workloads, consider whether a specialized implementation would be more appropriate.
  • FirstBy and FirstOrBy delegate to FindBy and FindOrBy respectively; they exist for naming symmetry with LastBy and LastOrBy.
  • Zip returns a Pair[A, B] value for each position; the result length equals the shorter of the two input slices.
  • Window returns an empty slice when size is less than 1 or greater than the length of the input slice.
  • Chunk panics if the chunk size is less than 1.
  • Flatten skips nil inner slices.
  • IndexOf and IndexBy return -1 when no match is found.
  • Rotate with a positive n shifts left; negative n shifts right. Values wrap around.

Examples