maps

package module
v0.0.0-...-d96e32b Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 2 Imported by: 0

README

maps

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSizeIsBelowOne = errors.New("size is below 1")
View Source
var KeyNotFoundError = errors.New("key not found")

Functions

func Aggregate

func Aggregate[TSource ~map[TKey]TValue, TKey comparable, TValue any, TAccumulator any](source TSource, seed TAccumulator, accumulator func(accumulator TAccumulator, key TKey, value TValue) TAccumulator) (result TAccumulator)

Applies an accumulate function over a sequence of values. The specified seed value is used as the initial accumulator value.

Parameters

source map[TKey]TValue

A sequence of values to aggregate over.

seed TAccumulator

The initial accumulator value.

accumulator func(accumulator TAccumulator, key TKey, value TValue) TAccumulator

An accumulate function to be invoked on each element.

Returns

result TAccumulator

The final accumulator value.

func All

func All[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, predicate func(key TKey, value TValue) bool) (result bool)

Determines whether all elements of a sequence satisfy a condition.

Parameters

source map[TKey]TValue

A sequence that contains the elements to apply the predicate to.

predicate func(key TKey, value TValue) bool

A function to test each element for a condition.

Returns

result bool

True if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty or nil; otherwise, false.

func Any

func Any[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, predicate ...func(key TKey, value TValue) bool) bool

Determines whether any element of a sequence satisfies a condition.

Parameters

source map[TKey]TValue

A sequence whose elements to apply the predicate to.

predicate func(key TKey, value TValue) bool

A function to test each element for a condition.

Returns

result bool

True if the source sequence is not empty or nil and at least one of its elements passes the test in the specified predicate; otherwise, false.

Remarks

If the predicate parameter is omitted, the function returns true if the source sequence contains any elements; otherwise, false.

func Chunk

func Chunk[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, size int) (result []TSource, err error)

Splits the elements of a sequence of values into chunks of size at most size.

Parameters

source map[TKey]TValue

A sequence of values whose elements to chunk.

size int

The maximum size of each chunk.

Returns

result []map[TKey]TValue

A sequence that contains the elements of the input sequence split into chunks of size size. The elements in each returned map are in an indeterminate order.

err error

ErrSizeIsBelowOne when size of source sequence is below 1.

Remarks

Each chunk except the last one will be of size size. The last chunk will contain the remaining elements and may be of a smaller size.

func Contains

func Contains[TSource ~map[TKey]TValue, TKey comparable, TValue comparable](source TSource, pair KeyValuePair[TKey, TValue]) (result bool)

Determines whether a sequence of values contains a specified key-value pair.

Parameters

source map[TKey]TValue

A sequence of values in which to locate a key-value pair.

pair KeyValue[TKey, TValue]

The key-value pair to locate in the sequence.

Returns

result bool

True if the source sequence contains the specified key and its value equals the specified value; otherwise, false.

func ContainsAll

func ContainsAll[TSource ~map[TKey]TValue, TKey comparable, TValue comparable](source TSource, pairs ...KeyValuePair[TKey, TValue]) (result bool)

Determines whether a sequence of values contains all specified key-value pairs by using the default equality comparer.

Parameters

source map[TKey]TValue

A sequence of values in which to locate key-value pairs.

pairs KeyValue[TKey, TValue]

The key-value pairs to locate in the sequence.

Returns

result bool

True if the source sequence contains all specified key-value pairs; otherwise, false.

func ContainsAllEquatable

func ContainsAllEquatable[TSource ~map[TKey]TValue, TKey comparable, TValue iEquatable[TValue]](source TSource, pairs ...KeyValuePair[TKey, TValue]) (result bool)

Determines whether a sequence of values contains all specified key-value pairs by using the IEquatable interface.

Parameters

source map[TKey]TValue

A sequence of values in which to locate key-value pairs.

pairs KeyValue[TKey, TValue]

The key-value pairs to locate in the sequence.

Returns

result bool

True if the source sequence contains all specified key-value pairs; otherwise, false.

func ContainsAllEquatableValue

func ContainsAllEquatableValue[TSource ~map[TKey]TValue, TKey comparable, TValue iEquatable[TValue]](source TSource, values ...TValue) (result bool)

Determines whether a sequence of values contains all specified values by using the IEquatable interface.

Parameters

source map[TKey]TValue

A sequence of values in which to locate values.

values TValue

The values to locate in the sequence.

Returns

result bool

True if the source sequence contains all values equal to all of the values in the specified values; otherwise, false.

func ContainsAllFunc

func ContainsAllFunc[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, equality func(this TValue, other TValue) bool, pairs ...KeyValuePair[TKey, TValue]) (result bool)

Determines whether a sequence of values contains all specified key-value pairs by using a specified equality comparer function.

Parameters

source map[TKey]TValue

A sequence of values in which to locate key-value pairs.

pairs KeyValue[TKey, TValue]

The key-value pairs to locate in the sequence.

equality func(this TValue, other TValue) bool

A function that compares two values of type TValue for equality.

Returns

result bool

True if the source sequence contains all specified key-value pairs; otherwise, false.

func ContainsAllFuncValue

func ContainsAllFuncValue[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, equality func(this TValue, other TValue) bool, values ...TValue) (result bool)

Determines whether a sequence of values contains all specified values by using a specified equality comparer function.

Parameters

source map[TKey]TValue

A sequence of values in which to locate values.

values TValue

The values to locate in the sequence.

equality func(this TValue, other TValue) bool

A function that compares two values of type TValue for equality.

Returns

result bool

True if the source sequence contains all values equal to all of the values in the specified values; otherwise, false.

func ContainsAllKeys

func ContainsAllKeys[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, keys ...TKey) (result bool)

Determines whether a sequence of values contains all specified keys.

Parameters

source map[TKey]TValue

A sequence of values in which to locate keys.

keys TKey

The keys to locate in the sequence.

Returns

result bool

True if the source sequence contains all specified keys; otherwise, false.

func ContainsAllValues

func ContainsAllValues[TSource ~map[TKey]TValue, TKey comparable, TValue comparable](source TSource, values ...TValue) (result bool)

Determines whether a sequence of values contains all specified values by using the default equality comparer.

Parameters

source map[TKey]TValue

A sequence of values in which to locate values.

values TValue

The values to locate in the sequence.

Returns

result bool

True if the source sequence contains all values equal to all of the values in the specified values; otherwise, false.

func ContainsAny

func ContainsAny[TSource ~map[TKey]TValue, TKey comparable, TValue comparable](source TSource, pairs ...KeyValuePair[TKey, TValue]) (result bool)

Determines whether a sequence of values contains any specified key-value pair by using the default equality comparer.

Parameters

source map[TKey]TValue

A sequence of values in which to locate key-value pairs.

pairs KeyValue[TKey, TValue]

The key-value pairs to locate in the sequence.

Returns

result bool

True if the source sequence contains any specified key-value pair; otherwise, false.

func ContainsAnyEquatable

func ContainsAnyEquatable[TSource ~map[TKey]TValue, TKey comparable, TValue iEquatable[TValue]](source TSource, pairs ...KeyValuePair[TKey, TValue]) (result bool)

Determines whether a sequence of values contains any specified key-value pair by using the IEquatable interface.

Parameters

source map[TKey]TValue

A sequence of values in which to locate key-value pairs.

pairs KeyValue[TKey, TValue]

The key-value pairs to locate in the sequence.

Returns

result bool

True if the source sequence contains any specified key-value pair; otherwise, false.

func ContainsAnyEquatableValue

func ContainsAnyEquatableValue[TSource ~map[TKey]TValue, TKey comparable, TValue iEquatable[TValue]](source TSource, values ...TValue) (result bool)

Determines whether a sequence of values contains any specified value by using the IEquatable interface.

Parameters

source map[TKey]TValue

A sequence of values in which to locate values.

values TValue

The values to locate in the sequence.

Returns

result bool

True if the source sequence contains any value equal to any of the values in the specified values; otherwise, false.

func ContainsAnyFunc

func ContainsAnyFunc[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, equality func(this TValue, other TValue) bool, pairs ...KeyValuePair[TKey, TValue]) (result bool)

Determines whether a sequence of values contains any specified key-value pair by using a specified equality comparer function.

Parameters

source map[TKey]TValue

A sequence of values in which to locate key-value pairs.

pairs KeyValue[TKey, TValue]

The key-value pairs to locate in the sequence.

equality func(this TValue, other TValue) bool

A function that compares two values of type TValue for equality.

Returns

result bool

True if the source sequence contains any specified key-value pair; otherwise, false.

func ContainsAnyFuncValue

func ContainsAnyFuncValue[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, equality func(this TValue, other TValue) bool, values ...TValue) (result bool)

Determines whether a sequence of values contains any specified value by using a specified equality comparer function.

Parameters

source map[TKey]TValue

A sequence of values in which to locate values.

values TValue

The values to locate in the sequence.

equality func(this TValue, other TValue) bool

A function that compares two values of type TValue for equality.

Returns

result bool

True if the source sequence contains any value equal to any of the values in the specified values; otherwise, false.

func ContainsAnyKey

func ContainsAnyKey[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, keys ...TKey) (result bool)

Determines whether a sequence of values contains any specified key.

Parameters

source map[TKey]TValue

A sequence of values in which to locate keys.

keys TKey

The keys to locate in the sequence.

Returns

result bool

True if the source sequence contains any specified key; otherwise, false.

func ContainsAnyValue

func ContainsAnyValue[TSource ~map[TKey]TValue, TKey comparable, TValue comparable](source TSource, values ...TValue) (result bool)

Determines whether a sequence of values contains any specified value by using the default equality comparer.

Parameters

source map[TKey]TValue

A sequence of values in which to locate values.

values TValue

The values to locate in the sequence.

Returns

result bool

True if the source sequence contains any value equal to any of the values in the specified values; otherwise, false.

func ContainsEquatable

func ContainsEquatable[TSource ~map[TKey]TValue, TKey comparable, TValue iEquatable[TValue]](source TSource, pair KeyValuePair[TKey, TValue]) (result bool)

Determines whether a sequence of values contains a specified key-value pair by using the IEquatable interface.

Parameters

source map[TKey]TValue

A sequence of values in which to locate a key-value pair.

pair KeyValue[TKey, TValue]

The key-value pair to locate in the sequence.

Returns

result bool

True if the source sequence contains the specified key and its value equals the specified value; otherwise, false.

func ContainsEquatableValue

func ContainsEquatableValue[TSource ~map[TKey]TValue, TKey comparable, TValue iEquatable[TValue]](source TSource, value TValue) (result bool)

Determines whether a sequence of values contains a specified value by using the IEquatable interface.

Parameters

source map[TKey]TValue

A sequence of values in which to locate a value.

value TValue

The value to locate in the sequence.

Returns

result bool

True if the source sequence contains the specified value; otherwise, false.

func ContainsFunc

func ContainsFunc[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, pair KeyValuePair[TKey, TValue], equality func(this TValue, other TValue) bool) (result bool)

Determines whether a sequence of values contains a specified key-value pair by using a specified equality comparer function.

Parameters

source map[TKey]TValue

A sequence of values in which to locate a key-value pair.

pair KeyValue[TKey, TValue]

The key-value pair to locate in the sequence.

equality func(this TValue, other TValue) bool

A function that compares two values of type TValue for equality.

Returns

result bool

True if the source sequence contains the specified key and its value equals the specified value; otherwise, false.

func ContainsFuncValue

func ContainsFuncValue[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, value TValue, equality func(this TValue, other TValue) bool) (result bool)

Determines whether a sequence of values contains a specified value by using a specified equality comparer function.

Parameters

source map[TKey]TValue

A sequence of values in which to locate a value.

value TValue

The value to locate in the sequence.

equality func(this TValue, other TValue) bool

A function that compares two values of type TValue for equality.

Returns

result bool

True if the source sequence contains the specified value; otherwise, false.

func ContainsKey

func ContainsKey[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, key TKey) (result bool)

Determines whether a sequence of values contains a specified key.

Parameters

source map[TKey]TValue

A sequence of values in which to locate a key.

key TKey

The key to locate in the sequence.

Returns

result bool

True if the source sequence contains the specified key; otherwise, false.

func ContainsValue

func ContainsValue[TSource ~map[TKey]TValue, TKey comparable, TValue comparable](source TSource, value TValue) (result bool)

Determines whether a sequence of values contains a specified value.

Parameters

source map[TKey]TValue

A sequence of values in which to locate a value.

value TValue

The value to locate in the sequence.

Returns

result bool

True if the source sequence contains the specified value; otherwise, false.

func Convert

func Convert[TSource ~map[TKey]TValue, TKey comparable, TValue any, TResultKey comparable, TResultValue any](source TSource, converter func(key TKey, value TValue) (TResultKey, TResultValue, error)) (result map[TResultKey]TResultValue, err error)

Converts each element of a sequence of values into a new key-value pair and returns a new map with the converted values.

Parameters

source map[TKey]TValue

A sequence of values whose elements are converted.

converter func(key TKey, value TValue) (TResultKey, TResultValue, error)

A function that converts a source key-value pair into a new key-value pair.

Returns

result map[TResultKey]TResultValue

A map that contains the converted key-value pairs. The elements in the returned map are in an indeterminate order.

err error

An error returned by the conversion function, if any.

func Count

func Count[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, predicate ...func(key TKey, value TValue) bool) (count int)

Returns a number that represents how many elements in the specified sequence satisfy a condition.

Parameters

source map[TKey]TValue

A sequence that contains elements to be tested and counted.

predicate func(key TKey, value TValue) bool [OPTIONAL]

A function to test each element for a condition.

Returns

count int

A number that represents how many elements in the sequence satisfy the condition in the predicate function or the number of elements in the input sequence if the predicate is omitted.

func ElementAt

func ElementAt[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, key TKey) (value TValue, err error)

Returns the value in a map at the specified key. If the key does not exist, it returns KeyNotFoundError.

Parameters

source map[TKey]TValue

The source map whose element is returned.

key TKey

The key of the value to return.

Returns

value TValue

The value at the specified key.

err error

An error indicating whether the key was found. It is KeyNotFoundError when the key does not exist.

func ElementAtOrDefault

func ElementAtOrDefault[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, key TKey) (value TValue)

Returns the value in a map at the specified key. If the key does not exist, it returns the default value of TValue.

Parameters

source map[TKey]TValue

The source map whose element is returned.

key TKey

The key of the value to return.

Returns

value TValue

The value at the specified key, or the default value of TValue when the key does not exist.

func ElementAtOrFallback

func ElementAtOrFallback[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, key TKey, fallback TValue) (value TValue)

Returns the value in a map at the specified key. If the key does not exist, it returns the provided fallback value.

Parameters

source map[TKey]TValue

The source map whose element is returned.

key TKey

The key of the value to return.

fallback TValue

The value to return when the key does not exist.

Returns

value TValue

The value at the specified key, or the fallback value when the key does not exist.

func Keys

func Keys[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource) (result []TKey)

Gets all keys from a sequence of values and returns them as a slice.

Parameters

source map[TKey]TValue

A sequence of values from which all keys are retrieved.

Returns

result []TKey

A slice that contains all keys of the given sequence. The keys in the returned slice are in an indeterminate order.

func Select

func Select[TSource ~map[TKey]TValue, TKey comparable, TValue any, TResultKey comparable, TResultValue any](source TSource, selector func(key TKey, value TValue) (TResultKey, TResultValue)) (result map[TResultKey]TResultValue)

Projects each key-value pair of a map into a new key-value pair and returns a new map with the projected entries.

Parameters

source map[TKey]TValue

A sequence of values whose elements are projected.

selector func(key TKey, value TValue) (TResultKey, TResultValue)

A function to transform each key-value pair into a new key and value.

Returns

result map[TResultKey]TResultValue

A map that contains the projected entries. The elements in the returned map are in an indeterminate order.

func SelectMany

func SelectMany[TSource ~map[TKey]TValue, TKey comparable, TValue any, TResultKey comparable, TResultValue any](source TSource, selector func(key TKey, value TValue) map[TResultKey]TResultValue) (result map[TResultKey]TResultValue)

Projects each element of a sequence of values to a map and flattens the resulting maps into one map.

Parameters

source map[TKey]TValue

A sequence of values whose elements are projected.

selector func(key TKey, value TValue) map[TResultKey]TResultValue

A function to transform each key-value pair into a map.

Returns

result map[TResultKey]TResultValue

A map that contains the flattened results of the projected maps. The elements in the returned map are in an indeterminate order. If the selector yields duplicate keys, the last value encountered wins.

func Values

func Values[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource) (result []TValue)

Gets all values from a sequence of values and returns them as a slice.

Parameters

source map[TKey]TValue

A sequence of values from which all values are retrieved.

Returns

result []TValue

A slice that contains all values of the given sequence. The values in the returned slice are in an indeterminate order.

func Where

func Where[TSource ~map[TKey]TValue, TKey comparable, TValue any](source TSource, predicate func(key TKey, value TValue) bool) (result TSource)

Filters a sequence of values based on a predicate and returns a new map that contains elements that satisfy the condition.

Parameters

source map[TKey]TValue

A sequence of values whose elements are filtered.

predicate func(key TKey, value TValue) bool

A function to test each key-value pair for a condition.

Returns

result map[TKey]TValue

A map that contains elements from the source sequence that satisfy the predicate. The elements in the returned map are in an indeterminate order.

Types

type KeyValuePair

type KeyValuePair[TKey comparable, TValue any] struct {
	Key   TKey
	Value TValue
}

Jump to

Keyboard shortcuts

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