slices

package
v0.0.0-...-68847cf Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package slices provides the processing of types slices based on generics and higher-order functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append[V any](ivss ...[]V) []V

Append appends the values of all slices to one new slice.

func ContainsAll

func ContainsAll[V any](ivs []V, pred func(v V) bool) bool

ContainsAll returns true if the function pred() returns true for all values of the slice.

func ContainsAny

func ContainsAny[V any](ivs []V, pred func(v V) bool) bool

ContainsAny returns true if the function pred() returns true for at least one value of the slice.

func Copy

func Copy[V any](ivs []V) []V

Copy is simply a convenient combination of allocation and copying.

func Delete

func Delete[V comparable](dv V, ivs []V) []V

Delete removes the first matching value of a slice.

func DeleteAll

func DeleteAll[V comparable](dv V, ivs []V) []V

DeleteAll removes all matching valus of a slice.

func DeleteAllWith

func DeleteAllWith[V any](ivs []V, pred func(V) bool) []V

DeleteAllWith removes all values of a slice where pred returns true.

func DeleteFirst

func DeleteFirst[V any](ivs []V) []V

DeleteFirst removes the first value of a slice.

func DeleteLast

func DeleteLast[V any](ivs []V) []V

DeleteLast removes the last value of a slice.

func DeleteWhile

func DeleteWhile[V any](ivs []V, pred func(V) bool) []V

DeleteWhile removes all values as long pred() returns true.

func DeleteWith

func DeleteWith[V any](ivs []V, pred func(V) bool) []V

DeleteWith removes the first value of a slice where pred returns true.

func Filter

func Filter[V any](ivs []V, pred func(V) bool) []V

Filter creates a slice from all values where pred() returns true.

func FilterMap

func FilterMap[I, O any](ivs []I, fun func(I) (O, bool)) []O

FilterMap creates a slice from of new values created by fun() where it also returns true.

func FoldL

func FoldL[V, Acc any](vs []V, acc Acc, fun func(V, Acc) Acc) Acc

FoldL iterates over the slice from left to right. It calls fun() for each value passing the initial accumulator. The accumulator returned by each function call is used as input at the next call. The last one will be returned.

func FoldLFirst

func FoldLFirst[V any](vs []V, fun func(V, V) V) V

FoldLFirst iterates over the slice from left to right. It calls fun() for each value passing the first value as accumulator. The accumulator returned by each function call is used as input at the next call. The last one will be returned.

func FoldR

func FoldR[V, Acc any](vs []V, acc Acc, fun func(V, Acc) Acc) Acc

FoldR iterates over the slice from right to left. It calls fun() for each value passing the initial accumulator. The accumulator returned by each function call is used as input at the next call. The last one will be returned.

func FoldRLast

func FoldRLast[V any](vs []V, fun func(V, V) V) V

FoldRLast iterates over the slice from right to left. It calls fun() for each value passing the last value as accumulator. The accumulator returned by each function call is used as input at the next call. The last one will be returned.

func Head[V any](ivs []V) V

Head returns the first value of a slice.

func HeadTail

func HeadTail[V any](ivs []V) (V, []V)

HeadTail returns the first value of a slice as head and the rest as tail.

func InitLast

func InitLast[V any](ivs []V) ([]V, V)

InitLast returns the last value of a slice as last and the rest as init.

func IsEqual

func IsEqual[V comparable](first, second []V) bool

IsEqual returns true if both slices are equal.

func IsMember

func IsMember[V comparable](v V, ivs []V) bool

IsMember returns true if the slice contains the value v.

func IsPrefix

func IsPrefix[V comparable](prefix, all []V) bool

IsPrefix returns true if the first slice is the prefix of the second one.

func IsSorted

func IsSorted[V constraints.Ordered](vs []V) bool

IsSorted returns true if a slice is sorted in ascending order.

func IsSortedWith

func IsSortedWith[V any](vs []V, less func(a, b V) bool) bool

IsSortedWith returns true if a slice is sorted in ascending order using less as comparison function.

func IsSuffix

func IsSuffix[V comparable](suffix, all []V) bool

IsSuffix returns true if the first slice is the suffix of the second one.

func Join

func Join[V any](sep V, ivs []V) []V

Join create a slice mixing a separator between each value of the slice.

func Last

func Last[V any](ivs []V) V

Last returns the last value of a slice.

func Map

func Map[I, O any](ivs []I, fun func(I) O) []O

Map creates a slice of output values from the input values and converted by the map function.

func MapFoldL

func MapFoldL[I, O, Acc any](ivs []I, acc Acc, fun func(I, Acc) (O, Acc)) ([]O, Acc)

MapFoldL combines the operations of Map() and FoldL() in one pass.

func MapFoldR

func MapFoldR[I, O, Acc any](ivs []I, acc Acc, fun func(I, Acc) (O, Acc)) ([]O, Acc)

MapFoldR combines the operations of Map() and FoldR() in one pass.

func Merge

func Merge[V constraints.Ordered](vsa, vsb []V) []V

Merge merges two slices in a sorted way together.

func MergeWith

func MergeWith[V any, K constraints.Ordered](vsa, vsb []V, key func(V) K) []V

MergeWith merges two slices and uses a comparator function for sorting.

func Partition

func Partition[V any](vs []V, pred func(V) bool) ([]V, []V)

Partition checks all values of the slices and returns all where pred() returns true in one slice and false in another one. Their individual ordering will be the same of the original one.

func Reverse

func Reverse[V any](ivs []V) []V

Reverse returns the slice in reverse order.

func Search[V any](pred func(v V) bool, ivs []V) (V, bool)

Search returns the first value that satisfies the given predicate.

func Shuffle

func Shuffle[V any](vs []V) []V

Shuffle returns a randomly shuffled copy of of the given slices of values.

func Sort

func Sort[V constraints.Ordered](ivs []V) []V

Sort returns a sorted copy of the given slice of values. It uses a parallel quicksort. The type of the slice value has to fulfil the constraints.Ordered constraint.

func SortWith

func SortWith[V any](ivs []V, less func(vs []V, i, j int) bool) []V

SortsWith reurns a sorted copy of the given slice like Sort(). Instead of having to fulfil a constraint any value type but can be used. The given less function must do the comparison of two values.

func Split

func Split[V any](n int, ivs []V) ([]V, []V)

Split returns the first n values of a slice as first slice and the rest as second.

func SplitWith

func SplitWith[V any](ivs []V, pred func(V) bool) ([]V, []V)

SplitWith returns the values while pred() returns true as first and the rest as second slice.

func Subslice

func Subslice[V any](ivs []V, fpos, tpos int) []V

Subslice returns the values of slices from fpos to tpos as a new slice. Negative fpos as well as too high tpos are allowed and will be limited. Starting behind the slice or end before 0 returns nil.

func Subtract

func Subtract[V comparable](ivs, svs []V) []V

Subtract returns a new slice that is a copy of input slice, subjected to the following procedure: for each element in the subtract slice, its first occurrence in the input slice is deleted.

func TakeWhile

func TakeWhile[V any](ivs []V, pred func(V) bool) []V

TakeWhile copies all values as long pred() returns true.

func Unique

func Unique[V comparable](ivs []V) []V

Unique returns a slice which contains each value only once. The second and further values are dropped.

func UniqueMerge

func UniqueMerge[V constraints.Ordered](vsa, vsb []V) []V

UniqueMerge merges two slices in a sorted way together. Duplicates are dropped.

func UniqueMergeWith

func UniqueMergeWith[V any, K constraints.Ordered](vsa, vsb []V, key func(V) K) []V

UniqueMergeWith merges two slices and uses a key function to get a sortable key of the values. This could e.g. be a field of a struct. Duplicate key values are dropped.

func UniqueWith

func UniqueWith[V any, K comparable](ivs []V, key func(V) K) []V

UniqueWith returns a slice which contains each value return by the key function only once. The returned value could e.g. be a fiel of a struct.

Types

This section is empty.

Jump to

Keyboard shortcuts

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