slices

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v1.0.0

func All[S ~[]T, T ~bool](s S) bool

The All function accept slice with bool elements and return true if all are true.

func AllFunc added in v1.0.0

func AllFunc[S ~[]T, T any](s S, cmd func(T) bool) bool

The AllFunc function returns true if cmd function return true for all elements. If collection is empty, true returns. So you may need to check collection on empty. Right way to use

if slices.AllFunc([]int{2,4,6}, func(val int)bool { return val&1 == 0 } ) { ...

func Any added in v1.0.0

func Any[S ~[]T, T ~bool](s S) bool

The Any function accept slice with bool elements and return true if one or more are true.

func AnyFunc added in v1.0.0

func AnyFunc[S ~[]T, T any](s S, cmd func(T) bool) bool

The AnyFunc function returns true if cmd function return true for one or more. If collection is empty, false returns. So you may need to check collection on empty. Right way to use

if slices.AnyFunc([]int{1,2,3,4}, func(val int)bool { return val&1 == 0 } ) { ...

func Filter

func Filter[S ~[]T, T any](s S, filter func(T) bool) []T

The filter function select elements of a slice that satisfy to filter function. It creates new slice with same size and append only satisfing elements. If you wnaht to remove unused cpacity from slice, use slices.Clip Filter returns the updated slice. It is therefore necessary to store the result of filtering, often in the variable holding the slice itself:

slice = slices.filter(slice, filterFunc)

func First

func First[S ~[]T, T any](s S) T

The First function returns first element of slice. If collection is empty, function panic You should use FirstOrDefault function if collection can be empty

func FirstFunc

func FirstFunc[S ~[]T, T any](s S, cmd func(T) bool) T

The FirstFunc function returns first element of slice satisfying filter (cmd). If there is no satisfying elements in collection, function panic You should use FirstOrDefaultFunc function to avoid panics

func FirstOrDefault

func FirstOrDefault[S ~[]T, T any](s S) T

The FirstOrDefault function returns first element of slice. If collection is empty, function returns default value

func FirstOrDefaultFunc

func FirstOrDefaultFunc[S ~[]T, T any](s S, cmd func(T) bool) T

The FirstOrDefaultFunc function returns first element of slice satisfying filter (cmd). If there is no satisfying elements in collection, function returns default value

func ForEach

func ForEach[S ~[]T, T any](s S, cmd func(T))

The ForEach function exec any func for each element of slice. !!!Pay attention, if you want to change slice non ref values you should use Transform or Update This example won't update source slice, just take you memory and cpu time:

s := []int{1,2,3}
slices.ForEach(s, func(i int){ i+=2 })

Right way to use this function

s := []int{1,2,3}
slices.ForEach(s, func(i int){ logger.Log("Element", i) })

func ForEachWithErr

func ForEachWithErr[S ~[]T, T any](s []T, cmd func(T) error) error

The ForEachWithErr close to ForEach function. It exec any func for each element of slice unitl error returns. Right way to use this function

s := []int{1,2,3}
slices.ForEach(s, func(i int) error {
	if i == 5{
		return "Unexpected value"
	}
	logger.Log("Element", i)
	return nil
})

func IsEmpty added in v1.0.0

func IsEmpty[S ~[]T, T any](s S) bool

The IsEmpty function checks if collection is empty. It returns true if collection is empty

func Last

func Last[S ~[]T, T any](s S) T

The Last function returns last element of slice. If collection is empty, function panic You should use LastOrDefault function if collection can be empty

func LastFunc

func LastFunc[S ~[]T, T any](s S, cmd func(T) bool) T

The LastFunc function returns last element of slice satisfying filter (cmd). If there is no satisfying elements in collection, function panic You should use FirstOrDefaultFunc function to avoid panics

func LastOrDefault

func LastOrDefault[S ~[]T, T any](s S) T

The LastOrDefault function returns last element of slice. If collection is empty, function returns default value

func LastOrDefaultFunc

func LastOrDefaultFunc[S ~[]T, T any](s S, cmd func(T) bool) T

The LastOrDefaultFunc function returns last element of slice satisfying filter (cmd). If there is no satisfying elements in collection, function returns default value

func Multiply

func Multiply[S ~[]T, T MathOps](s S) T

The Multiply function multiply each of collection element to single value.

func MultiplyFunc

func MultiplyFunc[S ~[]T, T any, R MathOps](s S, cmd func(T) R) R

The Multiply function multiply function results for each collection element. Right way to use.

val = slices.MultiplyFunc([]string{"aaa","bb"}, func(r string) int {return len(r)})

Pay attention! It's just an example.

func NotEmpty added in v1.0.0

func NotEmpty[S ~[]T, T any](s S) bool

The NotEmpty function checks if collection is empty. It returns true if collection is not empty

func Reduce

func Reduce[S ~[]T, T, R any](s S, init R, cmd func(R, T) R) R

The Reduce function apply each of collection element to single value. Right way to use. For example to join all values in slice in raw

val = slices.Reduce([]int{1,2,3}, "", func(r string, val int) string {return r + itoa.Itoa(val)})

Pay attention! It's just an example, Not recommended way to join values in string raw

func Sum

func Sum[S ~[]T, T MathOps](s S) T

The Sum function sum each of collection element to single value.

func SumFunc

func SumFunc[S ~[]T, T any, R MathOps](s S, cmd func(T) R) R

The SumFunc function sum function results for each collection element. Right way to use.

val = slices.SumFunc([]string{"aaa","bb"}, func(r string) int {return len(r)})

Pay attention! It's just an example.

func ToMap

func ToMap[S ~[]T, T any, K comparable](seq S, keySelector func(T) K) map[K]T

The ToMap function convert slice to map with keys calculated by keySelector function It creates new map with same size as slice. keySelector func must returns comparable type. If slice contain several element with equal calculated keys, value will be overriden. You can use ToMapBuckets to avoid it. Rigth way to use function

slice = slices.ToMap([]int{1,2,3}, func(val int) string { return itoa.Itoa(val) })

func ToMapBuckets

func ToMapBuckets[S ~[]T, T any, K comparable](seq S, keySelector func(T) K) map[K][]T

The ToMapBuckets function convert slice to map with keys calculated by keySelector function It creates new map with same size as slice. keySelector func must returns comparable type. If slice contain several element with equal calculated keys, all values will be saved in slice. So, it can be used as Group function. Rigth way to use function

//First example
slice = slices.ToMapBuckets([]int{1,2,3,2,1}, func(val int) string { return itoa.Itoa(val) })

//Second example
events := []struct{d date, name string}{ ... }
eventsCalendar = slices.ToMapBuckets(events, func(val struct{d date, name string}) date { return val.date })

func Transform

func Transform[S ~[]T, T, R any](source S, applyFunc func(T) R) []R

The Transform function convert each element of slice to new one. It creates new slice with same size and append all transformed values. Transform returns the updated slice. It is therefore necessary to store the result of filtering, often in the variable holding the slice itself:

slice := []int{1,2,3,4}
slice = slices.Transform(slice, filterFunc)

It also can return slice with another type

slice := []int{1,2,3,4}
slice1 := slices.Transform(slice, func(val int) string {return itoa.Itoa(val)})

func Update added in v1.0.0

func Update[S ~[]T, T any](source S, applyFunc func(T) T)

The Update function convert each element of slice. It update current slice values Right way to use:

slice := []int{1,2,3,4}
slices.Update(slice, func(val int) int {return val * 2})

Types

type MathOps

type MathOps interface {
	constraints.Integer | constraints.Float | constraints.Complex
}

Jump to

Keyboard shortcuts

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