slices

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2022 License: BSD-3-Clause Imports: 2 Imported by: 0

README

Tideland Go Slices

GitHub release GitHub license Go Module GoDoc Workflow Go Report Card

Description

Tideland Go Slices provides a set of functions for the processing of types slices based on generics and higher-order functions. This processing contains tests, mappings, filterings, concatings, deleting, filtering, folding and many more. Opposite to the standard library of Go with similiar functions like e.g. Sort() will not work on the same slice. All functions return new slices, even if a variable operation would have no effect.

Contributors

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

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

DeleteFirst removes the first value of a slice.

func DeleteLast added in v0.2.0

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

DeleteLast removes the last value of a slice.

func DeleteWhile added in v0.2.0

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

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

Merge merges two slices in a sorted way together.

func MergeWith added in v0.2.0

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 Sort

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

Sort provides a parallel quicksort for a slice of values with the constraint ordered.

func SortWith

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

SortWith sorts a slice based on a less function comparing the two values at the indexes i and j and returning true if the value at i has to be sorted before the one at j.

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

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

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

func UniqueMergeWith added in v0.2.0

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