slicez

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: MIT Imports: 4 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Associate

func Associate[E any, K comparable, V any](slice []E, mapper func(e E) (key K, value V)) map[K]V

Associate will iterate over a slice turning each object into a key/value pair in a map. Alias SliceToMap

func Chunk

func Chunk[A any](slice []A, n int) [][]A

Chunk will make de-flatten a slice into chunks eg slicez.Chunk([]int{0, 1, 2, 3, 4, 5, 6}, 2) // [][]int{{0, 1}, {2, 3}, {4, 5}, {6}}

func Clone

func Clone[E any](s []E) []E

Clone will create a copy of the slice

func Compact

func Compact[A comparable](slice []A) []A

Compact will remove any duplicate elements following each other in a slice, eg

{1,1,2,1,2,2,2} => {1,2,1,2}

func CompactBy

func CompactBy[A any](slice []A, equal func(a, b A) bool) []A

CompactBy will remove any duplicate elements following each other determined by the equal func. eg removing duplicate whitespaces from a string might look like

CompactBy([]rune("a    b"), func(a, b rune) {
 	return a == ' ' && a == b
})

resulting in "a b"

func Compare

func Compare[E compare.Ordered](s1, s2 []E) int

Compare will compare two slices

func CompareBy

func CompareBy[E1, E2 any](s1 []E1, s2 []E2, cmp func(E1, E2) int) int

CompareBy will compare two slices using a compare function

func Complement

func Complement[A comparable](a, b []A) []A

Complement returns a slice containing all elements in "b" that is not present in "a"

func ComplementBy

func ComplementBy[A any, B comparable](by func(a A) B, a, b []A) []A

ComplementBy returns a slice containing all elements in "b" that is not present in "a" determined using the "by" function

func Concat

func Concat[A any](slices ...[]A) []A

Concat will concatenate supplied slices in the given order into a new slice

func Contains

func Contains[E comparable](s []E, needle E) bool

Contains returns true if the needle is present in the slice

func ContainsBy

func ContainsBy[E any](s []E, f func(e E) bool) bool

ContainsBy returns true if the passed in func returns true on any of the element in the slice

func Cut

func Cut[E comparable](s []E, needle E) (left, right []E, found bool)

Cut will cut a slice into a left and a right part at the first instance where the needle is found. The needle is not included

func CutBy

func CutBy[E any](s []E, on func(E) bool) (left, right []E, found bool)

CutBy will cut a slice into a left and a right part at the first instance where the on function returns true. The element that makes the "on" function return true will not be included.

func Difference

func Difference[A comparable](slices ...[]A) []A

Difference returns a slice containing the difference between passed in slices

func DifferenceBy

func DifferenceBy[A any, B comparable](by func(a A) B, slices ...[]A) []A

DifferenceBy returns a slice containing the difference between passed in slices determined by the "by" function

func Drop

func Drop[A any](slice []A, i int) []A

Drop will produce a new slice where the "i" first element of the passed in slice are removed

func DropRight

func DropRight[A any](slice []A, i int) []A

DropRight will produce a new slice where the "i" last element of the passed in slice are removed

func DropRightWhile

func DropRightWhile[A any](slice []A, drop func(a A) bool) []A

DropRightWhile will produce a new slice, where the right most elements are dropped until the first instance the "drop" function returns false

func DropWhile

func DropWhile[A any](slice []A, drop func(a A) bool) []A

DropWhile will produce a new slice, where the left most elements are dropped until the first instance the "drop" function returns false

func Equal

func Equal[A comparable](s1, s2 []A) bool

Equal takes two slices of that is of the interface comparable. It returns true if they are of equal length and each element in a[x] == b[x] for every element

func EqualBy

func EqualBy[E1, E2 any](s1 []E1, s2 []E2, eq func(E1, E2) bool) bool

EqualBy takes two slices and an equality check function. It returns true if they are of equal length and each element in eq(a[x], b[x]) == true for every element

func Every

func Every[A comparable](slice []A, needle A) bool

Every returns true if every element in the slice is equal to the needle

func EveryBy

func EveryBy[A any](slice []A, predicate func(A) bool) bool

EveryBy returns true if the predicate function returns true for every element in the slice

func Filter

func Filter[A any](slice []A, include func(a A) bool) []A

Filter will produce a new slice only containing elements where the "include" function returns true

func Find

func Find[E any](s []E, equal func(E) bool) (e E, found bool)

Find will find the first instance of an element in a slice where the equal func returns true

func FindLast

func FindLast[E any](s []E, equal func(E) bool) (e E, found bool)

FindLast will find the last instance of an element in a slice where the equal func returns true

func FlatMap

func FlatMap[A any, B any](slice []A, f func(a A) []B) []B

FlatMap will map entries in one slice to entries in another slice and then flatten the map

func Flatten

func Flatten[A any](slice [][]A) []A

Flatten will flatten a 2d slice into a 1d slice

func Fold

func Fold[I any, A any](slice []I, combined func(accumulator A, val I) A, init A) A

Fold will iterate through the slice, from the left, and execute the combine function on each element accumulating the result into a value

func FoldRight

func FoldRight[I any, A any](slice []I, combined func(accumulator A, val I) A, init A) A

FoldRight will iterate through the slice, from the right, and execute the combine function on each element accumulating the result into a value

func ForEach

func ForEach[A any](slice []A, apply func(a A))

ForEach will apply the "apply" func on each element of the slice

func ForEachRight

func ForEachRight[A any](slice []A, apply func(a A))

ForEachRight will apply the "apply" func on each element of the slice

func GroupBy

func GroupBy[A any, B comparable](slice []A, key func(a A) B) map[B][]A

GroupBy will iterate through the slice and create a map where entries are grouped into slices using the key function generates the key .

func Head[A any](slice []A) (A, error)

Head will return the first element of the slice, or an error if the length of the slice is 0

func Index

func Index[E comparable](s []E, needle E) int

Index finds the first index of an element in an array. It returns -1 if it is not present

func IndexBy

func IndexBy[E any](s []E, f func(E) bool) int

IndexBy finds the first index of an element where the passed in function returns true. It returns -1 if it is not present

func Initial

func Initial[A any](slice []A) []A

Initial gets all but the last element of the slice

func Interleave

func Interleave[A any](slices ...[]A) []A

Interleave Round-robin alternating input slices and sequentially appending value at index into result. interleaved := Interleave([]int{1}, []int{2, 5, 8}, []int{3, 6}, []int{4, 7, 9, 10}) []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

func Intersection

func Intersection[A comparable](slices ...[]A) []A

Intersection returns a slice containing the intersection between passed in slices

func IntersectionBy

func IntersectionBy[A any, B comparable](by func(a A) B, slices ...[]A) []A

IntersectionBy returns a slice containing the intersection between passed in slices determined by the "by" function

func Join

func Join[E any](slices [][]E, glue []E) []E

Join will join a two-dimensional slice into a one dimensional slice with the glue slice between them. Similar to strings.Join or bytes.Join

func KeyBy

func KeyBy[A any, B comparable](slice []A, key func(a A) B) map[B]A

KeyBy will iterate through the slice and create a map where the key function generates the key value pair. If multiple values generate the same key, it is the first value that is stored in the map

func Last

func Last[A any](slice []A) (A, error)

Last will return the last element of the slice, or an error if the length of the slice is 0

func LastIndex

func LastIndex[E comparable](s []E, needle E) int

LastIndex finds the last index of an element in an array. It returns -1 if it is not present

func LastIndexBy

func LastIndexBy[E any](s []E, f func(E) bool) int

LastIndexBy finds the last index of an element where the passed in function returns true. It returns -1 if it is not present

func Map

func Map[A any, B any](slice []A, f func(a A) B) []B

Map will map entries in one slice to entries in another slice

func Max

func Max[E compare.Ordered](slice ...E) E

Max returns the largest element of the slice

func Min

func Min[E compare.Ordered](slice ...E) E

Min returns the smallest element of the slice

func None

func None[A comparable](slice []A, needle A) bool

None returns true if there is no element in the slice that matches the needle

func NoneBy

func NoneBy[A any](slice []A, predicate func(A) bool) bool

NoneBy returns true if there are no element in the slice for which the predicate function returns true

func Nth

func Nth[A any](slice []A, i int) A

Nth will return the nth element in the slice. It returns the zero value if len(slice) == 0. Nth looks as the slice of a modul group and will wrap around from both ends. Eg Nth(-1) will return the last element and Nth(10) where len(slice) == 10 will return the first element

func Partition

func Partition[A any](slice []A, predicate func(a A) bool) (satisfied, notSatisfied []A)

Partition will partition a slice into to two slices. One where every element for which the predicate function returns true and where it returns false

func PartitionBy

func PartitionBy[A any, B comparable](slice []A, by func(a A) B) [][]A

PartitionBy will partition a slice into to a slice of slices. Returns an array of elements split into groups. The order of grouped values is determined by the order they occur in collection. The grouping is generated from the results of running each element of collection through iteratee.

func Reject

func Reject[A any](slice []A, exclude func(a A) bool) []A

Reject is the complement of Filter and will produce a new slice only containing elements where the "exclude" function returns false

func RepeatBy

func RepeatBy[A any](i int, by func(i int) A) []A

RepeatBy creates a slice of i length and assign each element with result of the by function

func Replace

func Replace[E comparable](haystack []E, needle E, replacement E, n int) []E

Replace replaces occurrences needle in haystack n times. It Replaces all for n < 0

func ReplaceAll

func ReplaceAll[E comparable](haystack []E, needle E, replacement E) []E

ReplaceAll replaces all occurrences needle in haystack.

func ReplaceFirst

func ReplaceFirst[E comparable](haystack []E, needle E, replacement E) []E

ReplaceFirst replaces first occurrences needle in haystack.

func Reverse

func Reverse[A any](slice []A) []A

Reverse will return a news slice, but reversed of the original one

func Sample

func Sample[A any](slice []A, n int) []A

Sample will return a slice containing "n" random elements from the original slice

func Search[A any](slice []A, f func(e A) bool) (index int, e A)

Search given a slice data sorted in ascending order, the call

Search[int](data, func(e int) bool { return e >= 23 })

returns the smallest index i and element e such that e >= 23.

func Set

func Set[E comparable](slice []E) map[E]bool

Set will create a Set in the form of map[E]bool, This can be used to lookup if a item was present in the slice or not

func Shuffle

func Shuffle[A any](slice []A) []A

Shuffle will return a new slice where the elements from the original slice is shuffled

func SliceToMap

func SliceToMap[E any, K comparable, V any](slice []E, mapper func(a E) (key K, value V)) map[K]V

SliceToMap will iterate over a slice turning each object into a key/value pair in a map

func Some

func Some[A comparable](slice []A, needle A) bool

Some returns true there exist an element in the slice that is equal to the needle, an alias for Contains

func SomeBy

func SomeBy[A any](slice []A, predicate func(A) bool) bool

SomeBy returns true if there is an element in the slice for which the predicate function returns true

func Sort

func Sort[A compare.Ordered](slice []A) []A

Sort will return a new slice that is sorted in the natural order

func SortBy

func SortBy[A any](slice []A, less func(a, b A) bool) []A

SortBy will return a new slice that is sorted using the supplied less function for natural ordering

func Tail

func Tail[A any](slice []A) []A

Tail will return a new slice with all but the first element

func Take

func Take[A any](slice []A, i int) []A

Take will produce a new slice containing the "i" first element of the passed in slice

func TakeRight

func TakeRight[A any](slice []A, i int) []A

TakeRight will produce a new slice containing the "i" last element of the passed in slice

func TakeRightWhile

func TakeRightWhile[A any](slice []A, take func(a A) bool) []A

TakeRightWhile will produce a new slice containing all elements from the right until the "take" func returns false

func TakeWhile

func TakeWhile[A any](slice []A, take func(a A) bool) []A

TakeWhile will produce a new slice containing all elements from the left until the "take" func returns false

func Union

func Union[A comparable](slices ...[]A) []A

Union will return the union of an arbitrary number of slices. This is equivalent to Uniq(Concat(sliceA, sliceB))

func UnionBy

func UnionBy[A any, B comparable](by func(a A) B, slices ...[]A) []A

UnionBy will return the union of an arbitrary number of slices where the by function is used to determine the key. This is equivalent to UniqBy(Concat(sliceA, sliceB), by)

func Uniq

func Uniq[A comparable](slice []A) []A

Uniq returns a slice with no duplicate entries

func UniqBy

func UniqBy[A any, B comparable](slice []A, by func(a A) B) []A

UniqBy returns a slice with no duplicate entries using the by function to determine the key

func Unzip

func Unzip[A any, B any, C any](cSlice []C, unzipper func(c C) (a A, b B)) ([]A, []B)

Unzip will unzip a slice slices, c, into two slices, a and b, using the supplied unziper function

func Unzip2

func Unzip2[A any, B any, C any, D any](dSlice []D, unzipper func(d D) (a A, b B, c C)) ([]A, []B, []C)

Unzip2 will unzip a slice slices, d, into three slices, a, b and c, using the supplied unziper function

func Unzip3

func Unzip3[A any, B any, C any, D any, E any](eSlice []E, unzipper func(e E) (a A, b B, c C, d D)) ([]A, []B, []C, []D)

Unzip3 will unzip a slice slices, d, into three slices, a, b and c, using the supplied unziper function

func Without

func Without[A comparable](slice []A, exclude ...A) []A

Without creates a new slice excluding all given values.

func XOR

func XOR[A comparable](slices ...[]A) []A

func XORBy

func XORBy[A any, B comparable](by func(A) B, slices ...[]A) []A

func Zip

func Zip[A any, B any, C any](aSlice []A, bSlice []B, zipper func(a A, b B) C) []C

Zip will zip two slices, a and b, into one slice, c, using the zip function to combined elements

func Zip2

func Zip2[A any, B any, C any, D any](aSlice []A, bSlice []B, cSlice []C, zipper func(a A, b B, c C) D) []D

Zip2 will zip three slices, a, b and c, into one slice, d, using the zip function to combined elements

func Zip3

func Zip3[A any, B any, C any, D any, E any](aSlice []A, bSlice []B, cSlice []C, dSlice []D, zipper func(a A, b B, c C, d D) E) []E

Zip3 will zip three slices, a, b and c, into one slice, d, using the zip function to combined elements

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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