vec

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: MIT Imports: 2 Imported by: 2

Documentation

Overview

Package vec is a package of generic-type functions for slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Concat

func Concat[T any](s ...[]T) []T

Concat is used to merge two or more slices. This method does not change the existing slices, but instead returns a new slice.

func Copy

func Copy[T any](s []T) []T

Copy creates a copy of the slice.

func CopyWithin

func CopyWithin[T any](s []T, target, start int, end ...int)

CopyWithin copies part of a slice to another location in the current slice. @target

Zero-based index at which to copy the sequence to. If negative, target will be counted from the end.

@start

Zero-based index at which to start copying elements from. If negative, start will be counted from the end.

@end

Zero-based index at which to end copying elements from. CopyWithin copies up to but not including end.
If negative, end will be counted from the end.
If end is omitted, CopyWithin will copy until the last index (default to len(s)).

func Dict added in v1.4.3

func Dict[T any, K comparable, V any](s []T, set func(m map[K]V, k int, v T)) map[K]V

Dict generates a map through the set function.

func Distinct

func Distinct[T comparable](s *[]T, changeSlice bool) (distinctCount map[T]int)

Distinct calculates the count of each different element, and only saves these different elements in place if changeSlice is true.

func DistinctBy added in v1.4.0

func DistinctBy[T any, U comparable](s *[]T, toComparable func(k int, v T) U, whoToKeep ...func(a, b T) T)

DistinctBy deduplication in place according to the mapping function

func DistinctMap added in v1.4.0

func DistinctMap[T any, U comparable](s []T, mapping func(k int, v T) U) []U

DistinctMap returns the unique elements after mapping.

func Every

func Every[T any](s []T, fn func(k int, v T) bool) bool

Every tests whether all elements in the slice pass the test implemented by the provided function. NOTE:

Calling this method on an empty slice will return true for any condition!

func Fill

func Fill[T any](s []T, value T, start int, end ...int)

Fill changes all elements in the current slice to a value, from a start index to an end index. @value

Zero-based index at which to copy the sequence to. If negative, target will be counted from the end.

@start

Zero-based index at which to start copying elements from. If negative, start will be counted from the end.

@end

Zero-based index at which to end copying elements from. CopyWithin copies up to but not including end.
If negative, end will be counted from the end.
If end is omitted, CopyWithin will copy until the last index (default to len(s)).

func Filter

func Filter[T any](s []T, fn func(k int, v T) bool) []T

Filter creates a new slice with all elements that pass the test implemented by the provided function.

func FilterMap added in v1.4.0

func FilterMap[T any, U any](s []T, fn func(k int, v T) gust.Option[U]) []U

FilterMap returns a filtered and mapped slice of new elements.

func Find

func Find[T any](s []T, fn func(k int, v T) bool) gust.Option[gust.VecEntry[T]]

Find returns the key-value of the first element in the provided slice that satisfies the provided testing function.

func FlatMap added in v1.4.3

func FlatMap[T any, U any](s [][]T, mapping func(T) U) []U

FlatMap returns a new type slice that contains all elements in the nested slice.

func Flatten added in v1.4.3

func Flatten[T any](s [][]T) []T

Flatten returns a new slice that contains all elements in the nested slice.

func ForEachSegment added in v1.5.1

func ForEachSegment[T any](s []T, maxSegmentLength int, callback func(slice []T) error, useClone ...bool) error

ForEachSegment execute the callback for each slice.

func Get added in v1.5.0

func Get[T any](s []T, index int) gust.Option[T]

Get returns the gust.Option[T] at the specified index.

func Includes

func Includes[T comparable](s []T, valueToFind T, fromIndex ...int) bool

Includes determines whether a slice includes a certain value among its entries. @fromIndex

The index to start the search at. Defaults to 0.

func IndexOf

func IndexOf[T comparable](s []T, searchElement T, fromIndex ...int) int

IndexOf returns the first index at which a given element can be found in the slice, or -1 if it is not present. @fromIndex

The index to start the search at. Defaults to 0.

func Intersect

func Intersect[T comparable](s ...[]T) (intersectCount map[T]int)

Intersect calculates intersection of two or more slices, and returns the count of each element.

func LastIndexOf

func LastIndexOf[T comparable](s []T, searchElement T, fromIndex ...int) int

LastIndexOf returns the last index at which a given element can be found in the slice, or -1 if it is not present. @fromIndex

The index to start the search at. Defaults to 0.

func Map

func Map[T any, U any](s []T, mapping func(k int, v T) U) []U

Map creates a new slice populated with the results of calling a provided function on every element in the calling slice.

func MapAlone added in v1.4.3

func MapAlone[T any, U any](s []T, mapping func(v T) U) []U

MapAlone creates a new slice populated with the results of calling a provided function on every element in the calling slice.

func One

func One[T any](s []T) T

One try to return the first element, otherwise return zero value.

func Pop

func Pop[T any](s *[]T) gust.Option[T]

Pop removes the last element from a slice and returns that element. This method changes the length of the slice.

func Push

func Push[T any](s *[]T, element ...T) int

Push adds one or more elements to the end of a slice and returns the new length of the slice.

func PushDistinct

func PushDistinct[T comparable](s []T, element ...T) []T

PushDistinct adds one or more new elements that do not exist in the current slice at the end.

func PushDistinctBy added in v1.5.1

func PushDistinctBy[T any](s []T, isSame func(a, b T) bool, element ...T) []T

PushDistinctBy adds one or more new elements that do not exist in the current slice at the end.

func Reduce

func Reduce[T any](s []T, fn func(k int, v, accumulator T) T, initialValue ...T) T

Reduce executes a reducer function (that you provide) on each element of the slice, resulting in a single output value. @accumulator

The accumulator accumulates callback's return values.
It is the accumulated value previously returned to the last invocation of the callback—or initialValue,
if it was supplied (see below).

@initialValue

A value to use as the first argument to the first call of the callback.
If no initialValue is supplied, the first element in the slice will be used and skipped.

func ReduceRight

func ReduceRight[T any](s []T, fn func(k int, v, accumulator T) T, initialValue ...T) T

ReduceRight applies a function against an accumulator and each value of the slice (from right-to-left) to reduce it to a single value. @accumulator

The accumulator accumulates callback's return values.
It is the accumulated value previously returned to the last invocation of the callback—or initialValue,
if it was supplied (see below).

@initialValue

A value to use as the first argument to the first call of the callback.
If no initialValue is supplied, the first element in the slice will be used and skipped.

func RemoveEvery

func RemoveEvery[T comparable](p *[]T, elements ...T) int

RemoveEvery removes all the elements from the slice, and returns the new length of the slice.

func RemoveFirst

func RemoveFirst[T comparable](p *[]T, elements ...T) int

RemoveFirst removes the first matched elements from the slice, and returns the new length of the slice.

func Reverse

func Reverse[T any](s []T)

Reverse reverses a slice in place.

func SetsDifference

func SetsDifference[T comparable](set1, set2 []T, others ...[]T) []T

SetsDifference calculates between multiple collections: set1 - set2 - others... This method does not change the existing slices, but instead returns a new slice.

func SetsIntersect

func SetsIntersect[T comparable](set1, set2 []T, others ...[]T) []T

SetsIntersect calculates between multiple collections: set1 ∩ set2 ∩ others... This method does not change the existing slices, but instead returns a new slice.

func SetsUnion

func SetsUnion[T comparable](set1, set2 []T, others ...[]T) []T

SetsUnion calculates between multiple collections: set1 ∪ set2 ∪ others... This method does not change the existing slices, but instead returns a new slice.

func Shift

func Shift[T any](s *[]T) gust.Option[T]

Shift removes the first element from a slice and returns that removed element. This method changes the length of the slice.

func Slice

func Slice[T any](s []T, begin int, end ...int) []T

Slice returns a copy of a portion of a slice into a new slice object selected from begin to end (end not included) where begin and end represent the index of items in that slice. The original slice will not be modified.

func SliceSegment added in v1.5.1

func SliceSegment[T any](s []T, maxSegmentLength int, useClone ...bool) [][]T

SliceSegment cut s into multiple segments according to the specified maximum segment length.

func Some

func Some[T any](s []T, fn func(k int, v T) bool) bool

Some tests whether at least one element in the slice passes the test implemented by the provided function. NOTE:

Calling this method on an empty slice returns false for any condition!

func Splice

func Splice[T any](s *[]T, start, deleteCount int, items ...T)

Splice changes the contents of a slice by removing or replacing existing elements and/or adding new elements in place.

func Unshift

func Unshift[T any](s *[]T, element ...T) int

Unshift adds one or more elements to the beginning of a slice and returns the new length of the slice.

func UnshiftDistinct

func UnshiftDistinct[T comparable](s *[]T, element ...T) int

UnshiftDistinct adds one or more new elements that do not exist in the current slice to the beginning and returns the new length of the slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

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