list

package
v0.6.0 Latest Latest
Warning

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

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

Documentation

Overview

Package list contains list-related functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](items []T, ok func(T) bool) bool

Check if all list items pass the ok function

func AllEqual

func AllEqual[T comparable](items []T, value T) bool

Check if all list items are equal to the given value

func AllFalse

func AllFalse(items []bool) bool

Check if all list items are false

func AllGreater

func AllGreater[T cmp.Ordered](items []T, value T) bool

Check if all list items are greater than given value

func AllGreaterEqual

func AllGreaterEqual[T cmp.Ordered](items []T, value T) bool

Check if all list items are greater or equal than given value

func AllLess

func AllLess[T cmp.Ordered](items []T, value T) bool

Check if all list items are lesser than given value

func AllLessEqual

func AllLessEqual[T cmp.Ordered](items []T, value T) bool

Check if all list items are lesser or equal than given value

func AllNotEqual

func AllNotEqual[T comparable](items []T, value T) bool

Check if all list items are not equal to the given value

func AllSame

func AllSame[T comparable](items []T) bool

Check if all list items are the same

func AllTrue

func AllTrue(items []bool) bool

Check if all list items are true

func AllUnique

func AllUnique[T comparable](items []T) bool

Check if all list items are unique

func Any

func Any[T any](items []T, ok func(T) bool) bool

Check if any list item passes the ok function

func AnyEqual

func AnyEqual[T comparable](items []T, value T) bool

Check if any list item is equal to the given value

func AnyFalse

func AnyFalse(items []bool) bool

Check if any list item is false

func AnyTrue

func AnyTrue(items []bool) bool

Check if any list item is true

func Apply

func Apply[T any](items []T, task func(T) T) []T

Apply task function to each item

func ArgMax

func ArgMax[T cmp.Ordered](items []T) int

Get the index of the max item

func ArgMin

func ArgMin[T cmp.Ordered](items []T) int

Get the index of the min item

func Copy

func Copy[T any](items []T) []T

Returns new list with items copied from given list

func Count

func Count[T comparable](items []T, value T) int

Count the number of occurences of given value in the list

func Deduplicate

func Deduplicate[T comparable](items []T) []T

Deduplicate list, preserving given order

func Divide

func Divide(numItems, numParts int) [][2]int

Divides the number of items by the number of parts (divide workload). Return the index ranges of each part

func DivideRoundRobin

func DivideRoundRobin[T any](items []T, numParts int) [][]T

Divides the items into parts using round-robin order. Returns the divided items for each part

func Filter

func Filter[T any](items []T, keep func(T) bool) []T

Filter list: only keep items that pass keep function

func GroupBy

func GroupBy[T any, K comparable](items []T, keyFn func(T) K) map[K][]T

Group items using the key function

func GroupByFunc

func GroupByFunc[T, V any, K comparable](items []T, keyFn func(T) K, valueFn func(T) V) map[K][]V

Group items using the key function, and map the item using the value function

func IndexMap

func IndexMap[T comparable](items []T) map[T]int

Return map of item => index, loses data if items are not unique

func IndexedAll

func IndexedAll[T any](items []T, ok func(int, T) bool) bool

Check if all list items pass the ok function (accepts index and item)

func IndexedAny

func IndexedAny[T any](items []T, ok func(int, T) bool) bool

Check if any list item passes the ok function (accepts index and item)

func IndexedFilter

func IndexedFilter[T any](items []T, keep func(int, T) bool) []T

Filter list: only keep items with index that pass keep function

func IndexedMap

func IndexedMap[T, V any](items []T, convert func(int, T) V) []V

Apply convert function to each list item with index, return mapped items

func IsEmpty

func IsEmpty[T any](items []T) bool

Check if list is empty

func Last

func Last[T any](items []T, index int) T

Return the nth item from the back of the list

func Length

func Length[T any](items []T) int

Return list length

func LookupIndex

func LookupIndex[T any](items []T) func(int) T

Creates a converter function that maps the index to the list value, Panics on invalid index

func Map

func Map[T, V any](items []T, convert func(T) V) []V

Apply convert function to each list item, return mapped items

func MapIf

func MapIf[T, V any](items []T, convert func(T) (V, bool)) []V

Apply convert function to each list item, but only if second result is true, return mapped & filtered items

func MapList

func MapList[T any](indexes []int, values []T) []T

Map list indexes to given values; return mapped items. Can have zero value if index is invalid

func NotEmpty

func NotEmpty[T any](items []T) bool

Check if list is not empty

func NumRange

func NumRange[T ~uint | ~int](start, end T) []T

Return list of integers from [start, end)

func Product

func Product[T number](items []T) T

Computes product of items

func Random

func Random[T any](items []T) T

Get random item from list

func Reduce

func Reduce[T any](items []T, reduce func(T, T) T, initial T) T

Apply reduce function to each item

func Repeated

func Repeated[T any](value T, count int) []T

Creates a list containing the given <value> repeated <count> times

func Shuffle

func Shuffle[T any](items []T)

Shuffles the given items in-place

func Sum

func Sum[T number](items []T) T

Computes sum of items

func SumOf

func SumOf[T any, V number](items []T, convert func(T) V) V

Computes sum of mapped items

func TallyFunc

func TallyFunc[T any, K comparable](items []T, keyFn func(T) K) map[K]int

Tally the number of occurences of each item in the list, using the keyFn

func TallyItems

func TallyItems[T comparable](items []T) map[T]int

Tally the number of occurences of each item in the list

func ToAny

func ToAny[T any](items []T) []any

Creates a list of any items from given list

func Translate

func Translate[K comparable, V any](items []K, mask map[K]V) []V

Apply the translation mask to each list item, return mapped items. Can have zero value if item is not in mask map

Types

type SyncList

type SyncList[T any] struct {
	// contains filtered or unexported fields
}

Concurrency-safe generic list

func NewSyncList

func NewSyncList[T any]() *SyncList[T]

Create new SyncList

func SyncListFrom

func SyncListFrom[T any](items []T) *SyncList[T]

Create SyncList from existing list

func (*SyncList[T]) Append

func (sl *SyncList[T]) Append(items ...T)

SyncList.Append

func (*SyncList[T]) Clear

func (sl *SyncList[T]) Clear()

SyncList.Clear

func (*SyncList[T]) ClearItems

func (sl *SyncList[T]) ClearItems() []T

Copy SyncList's items and clear it

func (*SyncList[T]) Items

func (sl *SyncList[T]) Items() []T

SyncList underlying items

func (*SyncList[T]) Len

func (sl *SyncList[T]) Len() int

SyncList number of items

Jump to

Keyboard shortcuts

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