slice

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: MIT Imports: 7 Imported by: 6

Documentation

Overview

Package slice provides generic functions for slice types

Index

Constants

This section is empty.

Variables

View Source
var ErrBreak = it.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 Clone added in v0.0.2

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

Clone makes new slice instance with copied elements

func Convert added in v0.0.5

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

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

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 ConvertFit added in v0.0.5

func ConvertFit[FS ~[]From, From, To any](elements FS, fit c.Predicate[From], by c.Converter[From, To]) []To

ConvertFit additionally filters 'From' elements

func ConvertFitIndexed added in v0.0.5

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

ConvertFitIndexed additionally filters 'From' elements

func ConvertIndexed added in v0.0.5

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

ConvertIndexed creates a slice consisting of the transformed elements using the converter 'by' 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 Filter added in v0.0.2

func Filter[TS ~[]T, T any](elements TS, filter c.Predicate[T]) []T

Filter creates a slice containing only the filtered elements

func First added in v0.0.3

func First[TS ~[]T, T any](elements TS, by c.Predicate[T]) (T, bool)

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

func Flatt added in v0.0.2

func Flatt[FS ~[]From, From, To any](elements FS, by c.Flatter[From, To]) []To

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

func FlattElemFit added in v0.0.3

func FlattElemFit[FS ~[]From, From, To any](elements FS, by c.Flatter[From, To], fit c.Predicate[To]) []To

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

func FlattFit added in v0.0.3

func FlattFit[FS ~[]From, From, To any](elements FS, fit c.Predicate[From], by c.Flatter[From, To]) []To

FlattFit additionally filters 'From' elements.

func FlattFitFit added in v0.0.3

func FlattFitFit[FS ~[]From, From, To any](elements FS, fitFrom c.Predicate[From], by c.Flatter[From, To], fitTo c.Predicate[To]) []To

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

func For

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

For applies walker to elements. To stop walking just return the ErrBreak

func ForEach

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

ForEach applies walker to elements without error checking

func ForEachRef

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

ForEachRef applies walker to references without error checking

func Generate added in v0.0.4

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

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, index int) (T, bool)

Get returns an element from the elements by index, otherwise, if the provided index is ouf of the elements, returns zero T and false in the second result

func Group

func Group[T any, K comparable, TS ~[]T](elements TS, by c.Converter[T, K]) map[K]TS

Group converts the slice into a map with keys computeable by the converter 'by'

func Last added in v0.0.3

func Last[TS ~[]T, T any](elements TS, by c.Predicate[T]) (T, bool)

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

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 sclice of integers in the range defined by from and to inclusive.

func Reduce

func Reduce[TS ~[]T, T any](elements TS, by c.Binary[T]) T

Reduce reduces elements to an one

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 c.Converter[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[T c.Summable, TS ~[]T](elements TS) T

Sum returns the sum of all elements

func ToString

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

ToString converts 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 a delimiter and a nil value representation

func ToStringf

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

ToStringf converts elements to a string representation defined by a custom element format and a delimiter

func Track

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

Track applies tracker to elements with error checking. To stop traking just return the ErrBreak

func TrackEach

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

TrackEach applies tracker to elements without error checking

Types

type Less added in v0.0.5

type Less[T any] func(e1, e2 T) bool

type Sorter added in v0.0.5

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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