slicesx

package
v0.0.0-...-113f59a Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: BSD-3-Clause Imports: 1 Imported by: 0

Documentation

Overview

Package slicesx contains some helpful generic slice functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EqualSameNil

func EqualSameNil[S ~[]E, E comparable](s1, s2 S) bool

EqualSameNil reports whether two slices are equal: the same length, same nilness (notably when length zero), and all elements equal. If the lengths are different or their nilness differs, Equal returns false. Otherwise, the elements are compared in increasing index order, and the comparison stops at the first unequal pair. Floating point NaNs are not considered equal.

It is identical to the standard library's slices.Equal but adds the matching nilness check.

func Filter

func Filter[S ~[]T, T any](dst, src S, fn func(T) bool) S

Filter calls fn with each element of the provided src slice, and appends the element to dst if fn returns true.

dst can be nil to allocate a new slice, or set to src[:0] to filter in-place without allocating.

func Interleave

func Interleave[S ~[]T, T any](a, b S) S

Interleave combines two slices of the form [a, b, c] and [x, y, z] into a slice with elements interleaved; i.e. [a, x, b, y, c, z].

func Partition

func Partition[S ~[]T, T any](s S, cb func(T) bool) (trues, falses S)

Partition returns two slices, the first containing the elements of the input slice for which the callback evaluates to true, the second containing the rest.

This function does not mutate s.

func Shuffle

func Shuffle[S ~[]T, T any](s S)

Shuffle randomly shuffles a slice in-place, similar to rand.Shuffle.

Types

This section is empty.

Jump to

Keyboard shortcuts

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