slice

package
v0.0.7 Latest Latest
Warning

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

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

Documentation

Overview

Package slice provides generic functions for slice types

Index

Constants

View Source
const IterNoStarted = -1

IterNoStarted is the head Iterator position

Variables

View Source
var ErrBreak = loop.ErrBreak

ErrBreak is the 'break' statement of the For, Track methods

Functions

func BehaveAsStrings added in v0.0.4

func BehaveAsStrings[T ~string, TS ~[]T](elements TS) []string

BehaveAsStrings draws a string inherited type slice as the slice of strings

func CanIterateByRange added in v0.0.7

func CanIterateByRange(first, last, current int) bool

CanIterateByRange checks if an iterator can go further or stop

func Clone added in v0.0.2

func Clone[TS ~[]T, T any](elements TS) TS

Clone makes new slice instance with copied elements

func Contains added in v0.0.7

func Contains[TS ~[]T, T comparable](elements TS, example T) bool

Contains checks is the 'elements' slice contains the example

func Conv added in v0.0.7

func Conv[FS ~[]From, From, To any](elements FS, by func(From) (To, error)) ([]To, error)

Conv creates a slice consisting of the transformed elements using the converter 'by'

func Convert added in v0.0.5

func Convert[FS ~[]From, From, To any](elements FS, by func(From) To) []To

Convert creates a slice consisting of the transformed elements using the converter 'by'

func ConvertAndFilter added in v0.0.7

func ConvertAndFilter[FS ~[]From, From, To any](elements FS, by func(From) To, filter func(To) bool) []To

ConvertAndFilter additionally filters 'To' elements

func ConvertCheck added in v0.0.5

func ConvertCheck[FS ~[]From, From, To any](elements FS, by func(from From) (To, bool)) []To

ConvertCheck is similar to ConvertFit, but it checks and transforms elements together

func ConvertCheckIndexed added in v0.0.5

func ConvertCheckIndexed[FS ~[]From, From, To any](elements FS, by func(index int, from From) (To, bool)) []To

ConvertCheckIndexed additionally filters 'From' elements

func ConvertIndexed added in v0.0.5

func ConvertIndexed[FS ~[]From, From, To any](elements FS, converter func(index int, from From) To) []To

ConvertIndexed creates a slice consisting of the transformed elements using the 'converter' function which additionally applies the index of the element being converted

func DeepClone added in v0.0.5

func DeepClone[TS ~[]T, T any](elements TS, copier func(T) T) TS

DeepClone copies slice elements using a copier function and returns them as a new slice

func Delete

func Delete[TS ~[]T, T any](index int, elements TS) TS

Delete removes an element by index from the slice 'elements'

func Downcast added in v0.0.7

func Downcast[TS ~[]T, T any](elements []T) TS

Downcast transforms a slice type to an user-defined type slice based on that type

func DowncastRef added in v0.0.7

func DowncastRef[TS ~[]T, T any](elements *[]T) *TS

DowncastRef transforms a slice typeref to an user-defined type ref slice based on that type

func Empty added in v0.0.7

func Empty[TS ~[]T, T any](elements TS) bool

Empty checks whether the specified slice is empty

func Filled added in v0.0.7

func Filled[TS ~[]T, T any](elements TS, ifEmpty []T) TS

Filled returns the 'ifEmpty' if the 'elements' slise is empty

func Filt added in v0.0.7

func Filt[TS ~[]T, T any](elements TS, filter func(T) (bool, error)) ([]T, error)

Filt creates a slice containing only the filtered elements

func Filter added in v0.0.2

func Filter[TS ~[]T, T any](elements TS, filter func(T) bool) []T

Filter creates a slice containing only the filtered elements

func FilterAndConvert added in v0.0.7

func FilterAndConvert[FS ~[]From, From, To any](elements FS, filter func(From) bool, by func(From) To) []To

FilterAndConvert returns a stream that filters source elements and converts them

func FilterAndConvertIndexed added in v0.0.7

func FilterAndConvertIndexed[FS ~[]From, From, To any](elements FS, filter func(index int, from From) bool, converter func(index int, from From) To) []To

FilterAndConvertIndexed additionally filters 'From' elements

func FilterAndFlatt added in v0.0.7

func FilterAndFlatt[FS ~[]From, From, To any](elements FS, filter func(From) bool, flattener func(From) []To) []To

FilterAndFlatt filters source elements and extracts slices of 'To' by the 'flattener' function

func FilterConvertFilter added in v0.0.7

func FilterConvertFilter[FS ~[]From, From, To any](elements FS, filter func(From) bool, by func(From) To, filterConverted func(To) bool) []To

FilterConvertFilter filters source, converts, and filters converted elements

func FilterFlattFilter added in v0.0.7

func FilterFlattFilter[FS ~[]From, From, To any](elements FS, filterFrom func(From) bool, by func(From) []To, filterTo func(To) bool) []To

FilterFlattFilter unfolds the n-dimensional slice 'elements' into a n-1 dimensional slice with additinal filtering of 'From' and 'To' elements.

func First added in v0.0.3

func First[TS ~[]T, T any](elements TS, by func(T) bool) (no T, ok bool)

First returns the first element that satisfies requirements of the predicate 'by'

func Firstt added in v0.0.7

func Firstt[TS ~[]T, T any](elements TS, by func(T) (bool, error)) (no T, ok bool, err error)

Firstt returns the first element that satisfies the condition of the 'by' function

func Flat added in v0.0.7

func Flat[FS ~[]From, From, To any](elements FS, flattener func(From) ([]To, error)) ([]To, error)

Flat unfolds the n-dimensional slice into a n-1 dimensional slice

func Flatt added in v0.0.2

func Flatt[FS ~[]From, From, To any](elements FS, flattener func(From) []To) []To

Flatt unfolds the n-dimensional slice into a n-1 dimensional slice

func FlattAndConvert added in v0.0.7

func FlattAndConvert[FS ~[]From, From, I, To any](elements FS, flattener func(From) []I, convert func(I) To) []To

FlattAndConvert unfolds the n-dimensional slice into a n-1 dimensional slice and converts the elements

func FlattAndFiler added in v0.0.7

func FlattAndFiler[FS ~[]From, From, To any](elements FS, by func(From) []To, filter func(To) bool) []To

FlattAndFiler unfolds the n-dimensional slice into a n-1 dimensional slice with additinal filtering of 'To' elements.

func FlattKeys added in v0.0.7

func FlattKeys[TS ~[]T, T, K any](elements TS, keysExtractor func(T) []K) []c.KV[K, T]

FlattKeys transforms iterable elements to key/value iterator based on applying key, value extractor to the elements

func FlattValues added in v0.0.7

func FlattValues[TS ~[]T, T, V any](elements TS, valsExtractor func(T) []V) []c.KV[T, V]

FlattValues transforms iterable elements to key/value iterator based on applying key, value extractor to the elements

func For

func For[TS ~[]T, T any](elements TS, walker func(T) error) error

For applies the 'walker' function for the elements. Return the c.ErrBreak to stop

func ForEach

func ForEach[TS ~[]T, T any](elements TS, walker func(T))

ForEach applies the 'walker' function for the elements

func ForEachRef

func ForEachRef[T any, TS ~[]*T](references TS, walker func(T))

ForEachRef applies the 'walker' function for the references

func Generate added in v0.0.4

func Generate[T any](next func() (T, bool)) []T

Generate builds a slice by an generator function. The generator returns an element, or false if the generation is over, or an error.

func Get

func Get[TS ~[]T, T any](elements TS, current int) T

Get safely returns an element of the 'elements' slice by the 'current' index or return zero value of T if the index is more than size-1 or less 0

func GetFilled added in v0.0.7

func GetFilled[TS ~[]T, T any](elementsFactory func() TS, ifEmpty []T) TS

GetFilled returns the 'notEmpty' if the 'elementsFactory' return an empty slice

func GetValues added in v0.0.7

func GetValues[TS ~[]*T, T any](elements TS) []T

GetValues returns values referenced by the pointers. All nil pointers are excluded from the final result.

func Gett added in v0.0.7

func Gett[TS ~[]T, T any](elements TS, current int) (element T, ok bool)

Gett safely returns an element of the 'elements' slice by the 'current' index or return zero value of T if the index is more than size-1 or less 0 ok == true if success

func Group

func Group[TS ~[]T, T any, K comparable, V any](elements TS, keyExtractor func(T) K, valExtractor func(T) V) map[K][]V

Group converts the 'elements' slice into a map, extracting a key for each element applying the converter 'keyExtractor'. The keyExtractor converts an element to a key. The valExtractor converts an element to an value.

func GroupInMultiple added in v0.0.7

func GroupInMultiple[TS ~[]T, T any, K comparable, V any](elements TS, keysExtractor func(T) []K, valsExtractor func(T) []V) map[K][]V

GroupInMultiple converts the 'elements' slice into a map, extracting multiple keys, values per each element applying the 'keysExtractor' and 'valsExtractor' functions. The keysExtractor retrieves one or more keys per element. The valsExtractor retrieves one or more values per element.

func GroupInMultipleKeys added in v0.0.7

func GroupInMultipleKeys[TS ~[]T, T any, K comparable, V any](elements TS, keysExtractor func(T) []K, valExtractor func(T) V) map[K][]V

GroupInMultipleKeys converts the 'elements' slice into a map, extracting multiple keys, one value per each element applying the 'keysExtractor' and 'valExtractor' functions. The keysExtractor retrieves one or more keys per element. The valExtractor converts an element to a value.

func GroupInMultipleValues added in v0.0.7

func GroupInMultipleValues[TS ~[]T, T any, K comparable, V any](elements TS, keyExtractor func(T) K, valsExtractor func(T) []V) map[K][]V

GroupInMultipleValues converts the 'elements' slice into a map, extracting one key, multiple values per each element applying the 'keyExtractor' and 'valsExtractor' functions. The keyExtractor converts an element to a key. The valsExtractor retrieves one or more values per element.

func Has added in v0.0.7

func Has[TS ~[]T, T any](elements TS, condition func(T) bool) bool

Has checks is the 'elements' slice contains a value that satisfies the specified condition

func HasAny added in v0.0.7

func HasAny[TS ~[]T, T any](elements TS, predicate func(T) bool) bool

HasAny tests if the 'elements' slice contains an element that satisfies the "predicate" condition

func HasNext added in v0.0.7

func HasNext[T any](elements []T, current int) bool

HasNext checks if an iterator can go forward

func HasNextBySize added in v0.0.7

func HasNextBySize(size int, current int) bool

HasNextBySize checks if an iterator can go forward

func HasPrev added in v0.0.7

func HasPrev[T any](elements []T, current int) bool

HasPrev checks if an iterator can go backwards

func HasPrevBySize added in v0.0.7

func HasPrevBySize(size, current int) bool

HasPrevBySize checks if an iterator can go backwards

func IsValidIndex added in v0.0.7

func IsValidIndex(size, index int) bool

IsValidIndex checks if index is out of range

func Last added in v0.0.3

func Last[TS ~[]T, T any](elements TS, by func(T) bool) (no T, ok bool)

Last returns the latest element that satisfies requirements of the predicate 'filter'

func Lastt added in v0.0.7

func Lastt[TS ~[]T, T any](elements TS, by func(T) (bool, error)) (no T, ok bool, err error)

Lastt returns the latest element that satisfies requirements of the predicate 'filter'

func Len added in v0.0.7

func Len[TS ~[]T, T any](elements TS) int

Len return the length of the 'elements' slice

func NotEmpty added in v0.0.7

func NotEmpty[TS ~[]T, T any](elements TS) bool

NotEmpty checks whether the specified slice is not empty

func NotNil added in v0.0.7

func NotNil[TS ~[]*T, T any](elements TS) TS

NotNil returns only not nil elements

func Of

func Of[T any](elements ...T) []T

Of is generic slice constructor

func OfLoop added in v0.0.4

func OfLoop[S, T any](source S, hasNext func(S) bool, getNext func(S) (T, error)) ([]T, error)

OfLoop builds a slice by iterating elements of a source. The hasNext specifies a predicate that tests existing of a next element in the source. The getNext extracts the element.

func Range

func Range[T constraints.Integer](from T, to T) []T

Range generates a slice of integers in the range defined by from and to inclusive.

func Reduce

func Reduce[TS ~[]T, T any](elements TS, merge func(T, T) T) T

Reduce reduces the elements into an one using the 'merge' function

func Reverse

func Reverse[TS ~[]T, T any](elements TS) []T

Reverse inverts elements order

func Sort

func Sort[TS ~[]T, T any](elements TS, sorter Sorter, less Less[T]) TS

Sort sorts elements in place using a function that checks if an element is smaller than the others

func SortByOrdered

func SortByOrdered[T any, o constraints.Ordered, TS ~[]T](elements TS, sorter Sorter, by func(T) o) TS

SortByOrdered sorts elements in place by converting them to constraints.Ordered values and applying the operator <

func StringsBehaveAs added in v0.0.4

func StringsBehaveAs[TS ~[]T, T ~string](elements []string) TS

StringsBehaveAs draws a string slice as the slice of a string inherited type

func Sum added in v0.0.3

func Sum[TS ~[]T, T c.Summable](elements TS) T

Sum returns the sum of all elements

func ToKV added in v0.0.7

func ToKV[TS ~[]T, T, K, V any](elements TS, keyExtractor func(T) K, valExtractor func(T) V) []c.KV[K, V]

ToKV transforms slice elements to key/value pairs slice. One pair per one element

func ToKVs added in v0.0.7

func ToKVs[TS ~[]T, T, K, V any](elements TS, keysExtractor func(T) []K, valsExtractor func(T) []V) []c.KV[K, V]

ToKVs transforms slice elements to key/value pairs slice. Multiple pairs per one element

func ToMapResolv added in v0.0.7

func ToMapResolv[TS ~[]T, T any, K comparable, V, VR any](elements TS, keyExtractor func(T) K, valExtractor func(T) V, resolver func(bool, K, VR, V) VR) map[K]VR

ToMapResolv collects key\value elements to a map by iterating over the elements with resolving of duplicated key values

func ToString

func ToString[TS ~[]T, T any](elements TS) string

ToString converts the elements to their default string representation

func ToStringRefs

func ToStringRefs[T any, TS ~[]*T](references TS) string

ToStringRefs converts references to the default string representation

func ToStringRefsf

func ToStringRefsf[T any, TS ~[]*T](references TS, elementFormat, nilValue, delimeter string) string

ToStringRefsf converts references to a string representation defined by the delimiter and the nilValue representation

func ToStringf

func ToStringf[TS ~[]T, T any](elements TS, elementFormat, delimeter string) string

ToStringf converts the elements to a string representation defined by the elementFormat and a delimiter

func ToValues added in v0.0.7

func ToValues[TS ~[]*T, T any](pointers TS) []T

ToValues returns values referenced by the pointers. If a pointer is nil then it is replaced by the zero value.

func Track

func Track[TS ~[]T, T any](elements TS, tracker func(int, T) error) error

Track applies the 'tracker' function to the elements. Return the c.ErrBreak to stop tracking

func TrackEach

func TrackEach[TS ~[]T, T any](elements TS, tracker func(int, T))

TrackEach applies the 'tracker' function to the elements

func Upcast added in v0.0.7

func Upcast[TS ~[]T, T any](elements TS) []T

Upcast transforms a user-defined type slice to a underlying slice type

func UpcastRef added in v0.0.7

func UpcastRef[TS ~[]T, T any](elements *TS) *[]T

UpcastRef transforms a user-defined reference type slice to a underlying slice reference type

Types

type Iter added in v0.0.7

type Iter[T any] struct {
	// contains filtered or unexported fields
}

Iter is the Iterator implementation.

func NewHead added in v0.0.7

func NewHead[TS ~[]T, T any](elements TS) Iter[T]

NewHead instantiates Iter based on elements slice

func NewHeadS added in v0.0.7

func NewHeadS[TS ~[]T, T any](elements TS, elementSize uintptr) Iter[T]

NewHeadS instantiates Iter based on elements slice with predefined element size

func NewIter added in v0.0.7

func NewIter[TS ~[]T, T any](elements TS) *Iter[T]

NewIter instantiates an iterator based on the 'elements' slice

func NewTail added in v0.0.7

func NewTail[T any](elements []T) Iter[T]

NewTail instantiates Iter based on elements slice for reverse iterating

func NewTailS added in v0.0.7

func NewTailS[T any](elements []T, elementSize uintptr) Iter[T]

NewTailS instantiates Iter based on elements slice with predefined element size for reverse iterating

func (*Iter[T]) Cap added in v0.0.7

func (i *Iter[T]) Cap() int

Cap returns the iterator capacity

func (*Iter[T]) For added in v0.0.7

func (i *Iter[T]) For(walker func(element T) error) error

For takes elements retrieved by the iterator. Can be interrupt by returning ErrBreak

func (*Iter[T]) ForEach added in v0.0.7

func (i *Iter[T]) ForEach(walker func(element T))

ForEach takes all elements retrieved by the iterator.

func (*Iter[T]) Get added in v0.0.7

func (i *Iter[T]) Get() (v T, ok bool)

Get returns the current element. The ok result indicates whether the element was returned by the iterator. If ok == false, then the iteration must be completed.

func (*Iter[T]) GetNext added in v0.0.7

func (i *Iter[T]) GetNext() T

GetNext returns the next element

func (*Iter[T]) GetPrev added in v0.0.7

func (i *Iter[T]) GetPrev() T

GetPrev returns the previous element

func (*Iter[T]) HasNext added in v0.0.7

func (i *Iter[T]) HasNext() bool

HasNext checks the next element existing

func (*Iter[T]) HasPrev added in v0.0.7

func (i *Iter[T]) HasPrev() bool

HasPrev checks the previous element existing

func (*Iter[T]) Next added in v0.0.7

func (i *Iter[T]) Next() (v T, ok bool)

Next returns the next element. The ok result indicates whether the element was returned by the iterator. If ok == false, then the iteration must be completed.

func (*Iter[T]) Prev added in v0.0.7

func (i *Iter[T]) Prev() (v T, ok bool)

Prev returns the previos element. The ok result indicates whether the element was returned by the iterator. If ok == false, then the iteration must be completed.

type Less added in v0.0.5

type Less[T any] func(first, second T) bool

Less less element qualifier alias. Is a function that must return true it the first element is less the second

type Sorter added in v0.0.5

type Sorter func(x any, less func(i, j int) bool)

Sorter is alias for sort.Slice or SliceStable functions

Directories

Path Synopsis
Package clone provides slice clone aliases
Package clone provides slice clone aliases
reverse
Package reverse provides shor aliases for cloning slices with the elements in reverse order
Package reverse provides shor aliases for cloning slices with the elements in reverse order
sort
Package sort provides sorting of cloned slice elements
Package sort provides sorting of cloned slice elements
stablesort
Package stablesort provides stable sorting of cloned slice elements
Package stablesort provides stable sorting of cloned slice elements
Package convert provides slice converation helpers
Package convert provides slice converation helpers
Package filter provides aliases for slice filtering helpers
Package filter provides aliases for slice filtering helpers
Package first provides short aliases for slice functions for retrieving a first element
Package first provides short aliases for slice functions for retrieving a first element
Package flatt provides short aliases for slice functions
Package flatt provides short aliases for slice functions
Package group provides short aliases for grouping functions
Package group provides short aliases for grouping functions
Package iter provides implementations of slice based itarators
Package iter provides implementations of slice based itarators
Package last provides helpers for retrieving the last element of a slice
Package last provides helpers for retrieving the last element of a slice
Package range_ provides alias for the slice.Range function
Package range_ provides alias for the slice.Range function
Package reverse provides shor aliases for reversng order of a slice
Package reverse provides shor aliases for reversng order of a slice
Package sort provides sorting in place slice elements
Package sort provides sorting in place slice elements
Package stablesort provides stable sorting in place slice elements
Package stablesort provides stable sorting in place slice elements
Package stream provides helper functions for transforming a slice to a stream
Package stream provides helper functions for transforming a slice to a stream
Package sum provides sum.Of alias
Package sum provides sum.Of alias

Jump to

Keyboard shortcuts

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