gslice

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[V any](s []V, f func(V, int) bool) bool

All returns true if all elements in the given slice pass the given predicate function.

func Any

func Any[V any](s []V, f func(V, int) bool) bool

Any returns true if any element in the given slice passes the given predicate function.

func Filter

func Filter[V any](s []V, f func(V, int) bool) []V

Filter returns a new slice containing only the elements that pass the given predicate function. If the given slice is nil, nil is returned.

func FilterMap added in v0.2.0

func FilterMap[V1, V2 any](s []V1, f func(V1, int) (V2, bool)) []V2

FilterMap returns a new slice containing the results of applying the given function to each element. If the given slice is nil, nil is returned.

func FilterMapV added in v0.2.0

func FilterMapV[V1, V2 any](s []V1, f func(V1) (V2, bool)) []V2

FilterMapV is a simplified version of FilterMap. If we don't need the index, we can use FilterMapV instead of FilterMap.

func FilterV added in v0.2.0

func FilterV[V any](s []V, f func(V) bool) []V

FilterV is a simplified version of Filter. If we don't need the index, we can use FilterV instead of Filter.

func Head[V any](s []V, n int) []V

Head returns the first n elements of the given slice. If n is negative, panic. If n is greater than or equal to the length of the slice, the whole slice is returned.

func In

func In[V comparable](s []V, v V) bool

In returns true if the given value is in the given slice.

func Index added in v0.2.0

func Index[V comparable](s []V, v V) int

Index return the index of the given value in the given slice. If the given value is not in the given slice, -1 is returned.

func Map

func Map[V1, V2 any](s []V1, f func(V1, int) V2) []V2

Map returns a new slice containing the results of applying the given function to each element. If the given slice is nil, nil is returned.

func MapV added in v0.2.0

func MapV[V1, V2 any](s []V1, f func(V1) V2) []V2

MapV is a simplified version of Map. If we don't need the index, we can use MapV instead of Map.

func Range added in v0.2.0

func Range[T constraints.Real](start, end, step T) []T

Range returns a slice of integers from start to end, excluding end. If step is positive, the slice is in increasing order. If step is negative, the slice is in decreasing order. If step is zero, empty slice is returned.

func Reduce added in v0.2.0

func Reduce[V, T any](s []V, f func(T, V, int) T, initial ...T) T

Reduce apply the given function cumulatively to each element of the given slice, from left to right, to reduce the slice to a single value. The initial value, if given, is used as the initial value. If the initial value is not given, the zero value of the type of the result is used. If the given slice is nil, the initial value is returned.

func ReduceV added in v0.2.0

func ReduceV[V, T any](s []V, f func(T, V) T, initial ...T) T

ReduceV is a simplified version of Reduce. If we don't need the index, we can use ReduceV instead of Reduce.

func Slice added in v0.2.0

func Slice[V any](s []V, start, end int) []V

Slice returns a slice of the given slice from start to end, excluding end. Negative index is supported. If start is greater than or equal to end, empty slice is returned.

func Sort added in v0.1.0

func Sort[V constraints.Ordered](s []V)

Sort sorts the given slice in increasing order.

func Tail added in v0.2.0

func Tail[V any](s []V, n int) []V

Tail returns the last n elements of the given slice. If n is negative, panic. If n is greater than or equal to the length of the slice, the whole slice is returned.

func ToMap

func ToMap[V1, V2 any, K comparable](s []V1, f func(V1, int) (K, V2)) map[K]V2

ToMap returns a new map containing the results of applying the given function to each element. If the given slice is nil, nil is returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

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