slices

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepEquals

func DeepEquals[T comparable, A ~[]T](s1, s2 A) bool

DeepEquals checks to make sure that both slices contain the same elements. The ordering of the elements matter.

func ERange

func ERange[T constraints.Number](min, max T, step ...T) []T

ERange generates a slice of numbers from min up to, but not including, max.

func Filter

func Filter[T any, S ~[]T](s S, f func(T) bool) S

Filter returns a new slice containing only the elements of the input slice `s` for which the predicate function `f` returns true.

The original order of elements is preserved. The output slice is a newly allocated slice of the same type as the input.

Example:

evens := Filter([]int{1, 2, 3, 4}, func(n int) bool {
	return n%2 == 0
})
// evens == []int{2, 4}

func ForEach added in v0.3.0

func ForEach[T any, E ~[]T](s E, f func(int, T))

ForEach iterates over the elements of the provided slice `s`, calling the function `f` for each element with its index and value.

Example usage:

slices.ForEach([]string{"a", "b", "c"}, func(i int, v string) {
    fmt.Printf("Index %d: %s\n", i, v)
})

func IRange

func IRange[T constraints.Number](min, max T, step ...T) []T

IRange generates a slice of numbers from min to max, inclusive.

func Map

func Map[T any, R any, S ~[]T](s S, f func(T) R) []R

Map applies the given function f to each element of the input slice s, returning a new slice containing the results.

It preserves the order of the original slice and runs sequentially.

Example:

doubled := Map([]int{1, 2, 3}, func(n int) int {
    return n * 2
})
// doubled = []int{2, 4, 6}

func ParallelFilter

func ParallelFilter[T any, S ~[]T](s S, f func(T) bool, workers ...int) S

ParallelFilter evaluates the predicate function `f` in parallel on each element of the input slice `s` and returns a new slice containing only those elements for which `f` returns true.

The number of concurrent workers can be optionally specified via the `workers` variadic argument. If omitted, it defaults to `runtime.GOMAXPROCS(0)`.

The original order of elements is preserved. This function is particularly useful when the predicate function is expensive and you want to utilize multiple CPU cores.

Note: Although filtering is performed in parallel, the result is assembled sequentially, making this function most beneficial when `f` is significantly more expensive than a simple condition.

Example:

evens := ParallelFilter([]int{1, 2, 3, 4}, func(n int) bool {
    return n%2 == 0
})
// evens == []int{2, 4}

func ParallelForEach added in v0.3.0

func ParallelForEach[T any, E ~[]T](s E, f func(int, T), workers ...int)

ParallelForEach iterates over the elements of the provided slice `s` in parallel, using multiple worker goroutines. It calls the function `f` for each element with its index and value.

The optional `workers` argument allows you to specify the number of worker goroutines. If omitted or zero, it defaults to runtime.GOMAXPROCS(0).

Example usage:

slices.ParallelForEach([]int{1, 2, 3, 4}, func(i int, v int) {
    fmt.Printf("Index %d: %d\n", i, v)
})

slices.ParallelForEach([]int{1, 2, 3, 4}, func(i int, v int) {
    fmt.Printf("Index %d: %d\n", i, v)
}, 4) // use 4 workers

func ParallelMap

func ParallelMap[T any, R any, S ~[]T](s S, f func(T) R, workers ...int) []R

ParallelMap applies the function f to each element of the input slice s concurrently using a worker pool, and returns a new slice containing the results in the original order.

The number of concurrent workers can be controlled via the optional workers parameter. If omitted or set to a non-positive number, the number of logical CPUs (runtime.GOMAXPROCS(0)) is used by default.

Example:

squared := ParallelMap([]int{1, 2, 3, 4}, func(n int) int {
    return n * n
}, 8)
// squared = []int{1, 4, 9, 16}

Notes: - This function is safe for functions f that are side-effect free or thread-safe. - Use ParallelMap for CPU-bound or latency-sensitive transforms over large slices.

Panics if f panics; it does not recover from errors within goroutines.

func ShallowEquals

func ShallowEquals[T comparable, A ~[]T](s1, s2 A) bool

ShallowEquals checks to make sure that both slices contain the same elements. The ordering of the elements doesn't matter.

Types

This section is empty.

Jump to

Keyboard shortcuts

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