xslice

package
v0.0.0-...-d69adda Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 1 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Coalesce

func Coalesce[T comparable](ts ...T) T

func Every

func Every[T any](in []T, f func(T, int) bool) bool

Every tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

func Find

func Find[T any](in []T, f func(T, int) bool) T

Find returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, the zero value of T is returned.

func FindIndex

func FindIndex[T any](in []T, f func(T, int) bool) int

FindIndex returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

func ForEach

func ForEach[T any](in []T, f func(T, int))

ForEach executes a provided function once for each array element.

func Includes

func Includes[T comparable](in []T, a T) bool

Includes determines whether an array includes a certain value among its entries, returning true or false as appropriate.

func IndexOf

func IndexOf[T comparable](in []T, a T) int

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

func Join

func Join[T any](in []T, separator string) string

Join creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

func LastIndexOf

func LastIndexOf[T comparable](in []T, a T, from int) int

LastIndexOf returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

func Ptr

func Ptr[T any](t T) *T

func Reduce

func Reduce[T1, T2 any](in []T1, f func(T2, T1, int) T2, initial T2) T2

Reduce executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

The first time that the callback is run there is no "return value of the previous calculation". If supplied, an initial value may be used in its place. Otherwise the array element at index 0 is used as the initial value and iteration starts from the next element (index 1 instead of index 0).

func ReduceRight

func ReduceRight[T1, T2 any](in []T1, f func(T2, T1, int) T2, initial T2) T2

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

func Some

func Some[T any](in []T, f func(T, int) bool) bool

Some tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

func Sort

func Sort[T any](in []T, f func(a, b T) int) []T

Sort creates a new array by sorting the elements of the given array and returns the sorted array. The sort order is ascending.

func Unique

func Unique[T comparable](in []T) []T

Unique creates a new array with only unique elements from the given array.

Types

type AnySlice

type AnySlice[T any] []T

AnySlice is an array of any and has methods that mimic a subset of the JavaScript array functions.

func Filter

func Filter[T any](in []T, f func(T, int) bool) AnySlice[T]

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

func Map

func Map[T1, T2 any](in []T1, f func(T1, int) T2) AnySlice[T2]

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

func Reverse

func Reverse[T any](in []T) AnySlice[T]

Reverse creates a new array by reversing the given array. The first array element becomes the last, and the last array element becomes the first.

func Slice

func Slice[T any](in []T, start, end int) AnySlice[T]

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

If start<0, it is treated as distance from the end of the array. If end<=0, it is treated as distance from the end of the array.

func (AnySlice[T]) Every

func (a AnySlice[T]) Every(f func(T, int) bool) bool

Every tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

func (AnySlice[T]) Filter

func (a AnySlice[T]) Filter(f func(T, int) bool) AnySlice[T]

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

func (AnySlice[T]) Find

func (a AnySlice[T]) Find(f func(T, int) bool) T

Find returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, the zero value of T is returned.

func (AnySlice[T]) FindIndex

func (a AnySlice[T]) FindIndex(f func(T, int) bool) int

FindIndex returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.

func (AnySlice[T]) ForEach

func (a AnySlice[T]) ForEach(f func(T, int))

ForEach executes a provided function once for each array element.

func (AnySlice[T]) Reverse

func (a AnySlice[T]) Reverse() AnySlice[T]

Reverse creates a new array by reversing the given array. The first array element becomes the last, and the last array element becomes the first.

func (AnySlice[T]) Slice

func (a AnySlice[T]) Slice(start, end int) AnySlice[T]

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

If start<0, it is treated as distance from the end of the array. If end<=0, it is treated as distance from the end of the array.

func (AnySlice[T]) Some

func (a AnySlice[T]) Some(f func(T, int) bool) bool

Some tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

type ComparableSlice

type ComparableSlice[T comparable] AnySlice[T]

ComparableSlice is an array of comparable and has methods that mimic a subset of the JavaScript array functions that are unique to comparables.

func (ComparableSlice[T]) Filter

func (a ComparableSlice[T]) Filter(f func(T, int) bool) ComparableSlice[T]

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

func (ComparableSlice[T]) Includes

func (a ComparableSlice[T]) Includes(b T) bool

Includes determines whether an array includes a certain value among its entries, returning true or false as appropriate.

func (ComparableSlice[T]) IndexOf

func (a ComparableSlice[T]) IndexOf(b T) int

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

func (ComparableSlice[T]) LastIndexOf

func (a ComparableSlice[T]) LastIndexOf(b T, from int) int

LastIndexOf returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

func (ComparableSlice[T]) Reverse

func (a ComparableSlice[T]) Reverse() ComparableSlice[T]

Reverse creates a new array by reversing the given array. The first array element becomes the last, and the last array element becomes the first.

func (ComparableSlice[T]) Slice

func (a ComparableSlice[T]) Slice(start, end int) ComparableSlice[T]

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

If start<0, it is treated as distance from the end of the array. If end<=0, it is treated as distance from the end of the array.

func (ComparableSlice[T]) Unique

func (a ComparableSlice[T]) Unique() ComparableSlice[T]

Unique creates a new array with only unique elements from the given array.

type ReducibleSlice

type ReducibleSlice[T1, T2 any] AnySlice[T1]

ReducibleSlice is an array of any and has methods that mimic the JavaScript array reduce functions.

T1 represents the type of the array, while T2 represents the type of the array intended to be mapped to.

func (ReducibleSlice[T1, T2]) Filter

func (a ReducibleSlice[T1, T2]) Filter(f func(T1, int) bool) ReducibleSlice[T1, T2]

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

func (ReducibleSlice[T1, T2]) Map

func (a ReducibleSlice[T1, T2]) Map(f func(T1, int) T2) AnySlice[T2]

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

func (ReducibleSlice[T1, T2]) Reduce

func (a ReducibleSlice[T1, T2]) Reduce(f func(T2, T1, int) T2, initial T2) T2

Reduce executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

The first time that the callback is run there is no "return value of the previous calculation". If supplied, an initial value may be used in its place. Otherwise the array element at index 0 is used as the initial value and iteration starts from the next element (index 1 instead of index 0).

func (ReducibleSlice[T1, T2]) ReduceRight

func (a ReducibleSlice[T1, T2]) ReduceRight(f func(T2, T1, int) T2, initial T2) T2

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

func (ReducibleSlice[T1, T2]) Reverse

func (a ReducibleSlice[T1, T2]) Reverse() ReducibleSlice[T1, T2]

Reverse creates a new array by reversing the given array. The first array element becomes the last, and the last array element becomes the first.

func (ReducibleSlice[T1, T2]) Slice

func (a ReducibleSlice[T1, T2]) Slice(start, end int) ReducibleSlice[T1, T2]

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

If start<0, it is treated as distance from the end of the array. If end<=0, it is treated as distance from the end of the array.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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