slice

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxIterations = 100000
	DefaultStep          = 1
)

Variables

This section is empty.

Functions

func Filter

func Filter[T any](data []T, f func(T) bool) []T

Filter returns a new slice containing only the elements of the slice that satisfy the predicate.

func FindFunc added in v0.9.3

func FindFunc[T any](data []T, f func(T) bool) (T, bool)

FindFunc returns the first element in the slice that satisfies the predicate.

It returns the default value for the type and false if no element satisfies the predicate.

func FindIFunc added in v0.9.3

func FindIFunc[T any](data []T, f func(T) bool) (int, bool)

FindIFunc returns the index of the first element in the slice that satisfies the predicate.

It returns -1 and false if no element satisfies the predicate

func Fold added in v0.12.0

func Fold[T, S any](acc T, data []S, f func(T, S) T) T

Fold applies a function to each element of the slice. storing the result in an accumulator. It applies the function from left to right.

func FoldR added in v0.12.0

func FoldR[T, S any](acc T, data []S, f func(T, S) T) T

FoldR applies a function to each element of the slice. storing the result in an accumulator. It applies the function from right to left.

func Intersect

func Intersect[T comparable](a, b []T) []T

Intersect returns the intersection of two comparable slices.

func Map

func Map[T, U any](data []T, f func(T) U) []U

Map applies a function to each element of a slice and returns a new slice with the results.

func Unfold added in v0.12.0

func Unfold[T any](acc T, f func(T) T, p func(T) bool, opts ...UnfoldOption) []T

Unfold generates a slice by repeatedly applying a function to an accumulator. It includes the accumulator in the result as the first value. If the predicate is always false, it returns nil. It stops when the predicate returns false.

func UnfoldI added in v0.12.0

func UnfoldI[T any](acc T, f func(T) T, n int, opts ...UnfoldOption) []T

UnfoldI generates a slice by repeatedly applying a function to an accumulator. It includes the accumulator in the result as the first value. The length of the result is equal to i + 1. If i is negative, it returns nil. It stops after i iterations.

Types

type UnfoldConfig added in v0.12.0

type UnfoldConfig struct {
	Max  int
	Step int
}

type UnfoldOption added in v0.12.0

type UnfoldOption func(*UnfoldConfig)

func WithMax added in v0.12.0

func WithMax(n int) UnfoldOption

WithStep sets the step of the unfold. This does not include the first step. If n is 5, the length of the result is <= 6.

func WithStep added in v0.12.0

func WithStep(step int) UnfoldOption

WithStep sets the step of the unfold. This does not include the first step. If step is 2, the result will include every second value.

Jump to

Keyboard shortcuts

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