slice_utils

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 9 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

slice_utils

slice_utils is a Go library providing a collection of utility functions for working with slices and Go 1.23+ iterators (iter.Seq). It offers functional-style operations like filtering, mapping, grouping, and deduplication.

Installation

go get github.com/zauberhaus/slice_utils

Features

Slice Operations

Helper functions for common slice manipulations.

  • Filtering & Selection: Select, Delete, FilterStrings, RemoveStrings
  • Transformation: Convert, Change, ToAny
  • Aggregation: Count, Aggregate, Empty, Contains
  • Maps: ToMap, Remap, Group
  • Organization: SortFunc, Chunks, Pairs
  • Uniqueness: Duplicates, Deduplicate
Iterator Sequences (Go 1.23+)

Utilities for working with iter.Seq.

  • Filtering: FilterSeq, RemoveSeq, PatternSeq
  • Transformation: ConvertSeq, ReplaceFuncSeq, ReplaceSeq, AnySeq
  • Aggregation: CountSeq, SumSeq, SumFuncSeq, IsEmptySeq
  • Grouping & Hashing: GroupSeq, HashSeq
  • Uniqueness: DuplicateSeq, DeduplicationSeq

Usage

Slice Examples
import (
    "fmt"
    "github.com/zauberhaus/slice_utils"
)

func main() {
    // Filtering
    nums := []int{1, 2, 3, 4, 5}
    evens := slice_utils.Select(nums, func(n int) bool {
        return n%2 == 0
    })
    fmt.Println(evens) // [2 4]

    // Conversion
    strs := slice_utils.Convert(nums, func(n int) string {
        return fmt.Sprintf("val-%d", n)
    })
    fmt.Println(strs) // [val-1 val-2 val-3 val-4 val-5]

    // Grouping
    groups := slice_utils.Group(nums, func(n int) string {
        if n%2 == 0 {
            return "even"
        }
        return "odd"
    })
    fmt.Println(groups["even"]) // [2 4]
}
Sequence Examples
import (
    "fmt"
    "slices"
    "github.com/zauberhaus/slice_utils"
)

func main() {
    nums := []int{1, 2, 3, 4, 5}
    
    // Create a sequence
    seq := slices.Values(nums)
    
    // Filter sequence
    filtered := slice_utils.FilterSeq(seq, func(n int) bool {
        return n > 2
    })
    
    // Collect results
    result := slices.Collect(filtered)
    fmt.Println(result) // [3 4 5]
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Aggregate

func Aggregate[Slice ~[]V, V any, T cmp.Ordered](slice Slice, f func(val1 V) (T, error)) (T, error)

func AnySeq

func AnySeq[S any](s iter.Seq[S]) iter.Seq[any]

func Change

func Change[Slice ~[]V, V any](slice Slice, f func(val1 V) V) Slice

func Chunks

func Chunks[Slice ~[]V, V any](slice Slice, size int) []Slice

func Contains

func Contains[V any](slice []V, f func(val V) bool) bool

func Convert

func Convert[Slice ~[]V, V any, T any](slice Slice, f func(val1 V) T) []T

func ConvertSeq

func ConvertSeq[S any, T any](s iter.Seq[S], fn func(val S) T) iter.Seq[T]

func Count

func Count[Slice ~[]V, V any](slice Slice, f func(val V) bool) int

func CountSeq

func CountSeq[S any](s iter.Seq[S]) int

func Deduplicate

func Deduplicate[Slice ~[]V, V comparable](s Slice) Slice

func DeduplicationSeq

func DeduplicationSeq[V comparable](s iter.Seq[V]) iter.Seq[V]

func Delete

func Delete[Slice ~[]V, V comparable](slice Slice, vals ...V) Slice

Delete returns slice without the first element that equals any of vals. The input slice is never modified; a new slice is allocated on removal.

func DuplicateSeq

func DuplicateSeq[V comparable](s iter.Seq[V]) iter.Seq[V]

func Duplicates

func Duplicates[Slice ~[]V, V comparable](slice Slice) Slice

Duplicates returns each value that occurs more than once, in the order of its second occurrence.

func Empty

func Empty[Slice ~[]V, V any](slice Slice, f func(val V) bool) bool

func FilterSeq

func FilterSeq[S any](s iter.Seq[S], fn func(S) bool) iter.Seq[S]

func FilterStrings

func FilterStrings(s []string, p *regexp.Regexp) []string

func First added in v1.2.0

func First[Slice ~[]V, V any](s Slice, fn func(V) bool) (V, bool)

func Group

func Group[S ~[]E, E any, H cmp.Ordered](s S, f func(v E) H) map[H]S

func GroupSeq

func GroupSeq[S ~[]E, E any, H comparable](s iter.Seq[E], fn func(v E) H) iter.Seq[S]

GroupSeq yields the values of s grouped by fn, with groups in the order their keys first appear.

func Groups

func Groups[Slice ~[]V, V any, K cmp.Ordered](s Slice, f func(v V) K) []Slice

Groups returns the values of s grouped by f, ordered by ascending key.

func HashSeq

func HashSeq[E comparable](s iter.Seq[E]) iter.Seq2[uint64, E]

HashSeq yields each value with a deterministic FNV-1a hash of its %#v representation, so equal values hash the same across calls and processes. Pointer values hash by address.

func IsEmptySeq

func IsEmptySeq[S any](s iter.Seq[S]) bool

func Last added in v1.2.0

func Last[Slice ~[]V, V any](s Slice, fn func(V) bool) (V, bool)

func Pairs

func Pairs[T any](values ...T) [][2]T

func PatternSeq

func PatternSeq[S any](s iter.Seq[S], pattern *regexp.Regexp) iter.Seq[S]

PatternSeq yields the values whose text matches pattern. A nil pattern matches nothing.

func Remap

func Remap[Slice ~[]V, V any, K comparable, T any](slice Slice, f func(val V) (K, T, error)) (map[K]T, error)

func RemoveSeq

func RemoveSeq[S comparable](s iter.Seq[S], g iter.Seq[S]) iter.Seq[S]

RemoveSeq yields the values of s that are not in g. g is read once per iteration of the returned sequence, before s is consumed.

func RemoveStrings

func RemoveStrings(s []string, p *regexp.Regexp) []string

func ReplaceFuncSeq

func ReplaceFuncSeq[S any](s iter.Seq[S], fn func(val S) S) iter.Seq[S]

func ReplaceSeq

func ReplaceSeq[S comparable](s iter.Seq[S], g map[S]S) iter.Seq[S]

func Select

func Select[Slice ~[]V, V any](slice Slice, f func(val V) bool) Slice

func SortFunc

func SortFunc[Slice ~[]V, V any](slice Slice, f func(val1 V, val2 V) bool)

func StringPatternSeq

func StringPatternSeq[S any](s iter.Seq[S], pattern string) iter.Seq[S]

func SumFuncSeq

func SumFuncSeq[S any, T cmp.Ordered](s iter.Seq[S], fn func(S) (T, error)) (T, error)

func SumSeq

func SumSeq[S cmp.Ordered](s iter.Seq[S]) S

func To added in v1.1.1

func To[T any, V any, Slice ~[]V](slice Slice) []T

func ToAny

func ToAny[Slice ~[]V, V any](slice Slice) []any

func ToMap

func ToMap[Slice ~[]V, K comparable, V any](slice Slice, f func(val V) K) map[K]V

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址