Documentation
¶
Overview ¶
Package kv provides a custom map collection type, functions and methods related to maps. The types and methods from kv don't guarantee order. See kv/ordered for that.
Index ¶
- func CountBy[T comparable, K comparable, V any](c Collection[K, V], f func(v V) T) map[T]int
- type Collection
- func Collect[T any](items ...T) Collection[int, T]
- func CollectMap[K comparable, V any](items map[K]V) Collection[K, V]
- func CollectSlice[T any](items []T) Collection[int, T]
- func Combine[K comparable, V any](keys slice.Collection[K], values slice.Collection[V]) Collection[K, V]
- func CombineE[K comparable, V any](keys slice.Collection[K], values slice.Collection[V]) (Collection[K, V], error)
- func (c Collection[K, V]) Concat(concatTo Collection[K, V]) Collection[K, V]
- func (c Collection[K, V]) Contains(f collections.Matcher[int, V]) bool
- func (c Collection[K, V]) Copy() Collection[K, V]
- func (c Collection[K, V]) Count() int
- func (c Collection[K, V]) Each(f func(k K, v V)) Collection[K, V]
- func (c Collection[K, V]) Every(f collections.AnyMatcher) bool
- func (c Collection[K, V]) Filter(f func(k K, v V) bool) Collection[K, V]
- func (c Collection[K, V]) Flip() Collection[K, V]
- func (c Collection[K, V]) FlipE() (Collection[K, V], error)
- func (c Collection[K, V]) Forget(k K) Collection[K, V]
- func (c Collection[K, V]) ForgetE(k K) (Collection[K, V], error)
- func (c *Collection[K, V]) Get(k K) V
- func (c Collection[K, V]) GetE(k K) (V, error)
- func (c Collection[K, V]) IsEmpty() bool
- func (c Collection[K, V]) Keys() slice.Collection[K]
- func (c Collection[K, V]) KeysValues() (slice.Collection[K], slice.Collection[V])
- func (c Collection[K, V]) Map(f func(k K, v V) V) Collection[K, V]
- func (c Collection[K, V]) Merge(other Collection[K, V]) Collection[K, V]
- func (c Collection[K, V]) Only(keys []K) Collection[K, V]
- func (c Collection[K, V]) Put(k K, v V) Collection[K, V]
- func (c Collection[K, V]) Reject(f func(k K, v V) bool) Collection[K, V]
- func (c Collection[K, V]) Search(v V) K
- func (c Collection[K, V]) SearchE(v V) (K, error)
- func (c Collection[K, V]) Tap(f func(Collection[K, V])) Collection[K, V]
- func (c Collection[K, V]) ToSlice() []V
- func (c Collection[K, V]) Unless(execute bool, f func(collection Collection[K, V]) Collection[K, V]) Collection[K, V]
- func (c Collection[K, V]) UnlessEmpty(f func(collection Collection[K, V]) Collection[K, V]) Collection[K, V]
- func (c Collection[K, V]) UnlessNotEmpty(f func(collection Collection[K, V]) Collection[K, V]) Collection[K, V]
- func (c Collection[K, V]) Values() slice.Collection[V]
- func (c Collection[K, V]) When(execute bool, f func(collection Collection[K, V]) Collection[K, V]) Collection[K, V]
- func (c Collection[K, V]) WhenEmpty(f func(collection Collection[K, V]) Collection[K, V]) Collection[K, V]
- func (c Collection[K, V]) WhenNotEmpty(f func(collection Collection[K, V]) Collection[K, V]) Collection[K, V]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CountBy ¶
func CountBy[T comparable, K comparable, V any](c Collection[K, V], f func(v V) T) map[T]int
CountBy calls `f` with every value in `c` and counts the numbers of occurrences of the return value
Types ¶
type Collection ¶
type Collection[K comparable, V any] map[K]V
Collection is a custom generic map type specially useful to pipe generic method.
func Collect ¶
func Collect[T any](items ...T) Collection[int, T]
Collect returns the result of CollectSlice passing the given items.
func CollectMap ¶
func CollectMap[K comparable, V any](items map[K]V) Collection[K, V]
CollectMap returns the given map as a Collection.
func CollectSlice ¶
func CollectSlice[T any](items []T) Collection[int, T]
CollectSlice makes a map[int]T, given each item a numeric sequential key corresponding to the item index.
func Combine ¶ added in v0.0.4
func Combine[K comparable, V any]( keys slice.Collection[K], values slice.Collection[V], ) Collection[K, V]
Combine calls CombineE, omitting the error.
func CombineE ¶ added in v0.0.4
func CombineE[K comparable, V any]( keys slice.Collection[K], values slice.Collection[V], ) (Collection[K, V], error)
CombineE uses the first slice as keys and the second as values to build a map. The order is preserved. Should the length of keys and values slices be different, an instance of errors.KeysValuesLengthMismatch is returned.
func (Collection[K, V]) Concat ¶
func (c Collection[K, V]) Concat(concatTo Collection[K, V]) Collection[K, V]
Concat puts the key-value pairs from concatTo on the original collection. Should two keys equal, the original value will be preserved. To override the values, see Collection.Merge.
func (Collection[K, V]) Contains ¶
func (c Collection[K, V]) Contains(f collections.Matcher[int, V]) bool
Contains checks if any values on the Collection match f.
func (Collection[K, V]) Copy ¶ added in v0.0.4
func (c Collection[K, V]) Copy() Collection[K, V]
Copy returns a new collection equivalent to the copied.
func (Collection[K, V]) Count ¶
func (c Collection[K, V]) Count() int
Count returns the number of elements stored on the collection
func (Collection[K, V]) Each ¶
func (c Collection[K, V]) Each(f func(k K, v V)) Collection[K, V]
Each ia a typical for loop. The current key and values are passed to the closure on each iteration. Order is not guaranteed on each execution.
func (Collection[K, V]) Every ¶
func (c Collection[K, V]) Every(f collections.AnyMatcher) bool
Every checks if every value on the Collection match f.
func (Collection[K, V]) Filter ¶
func (c Collection[K, V]) Filter(f func(k K, v V) bool) Collection[K, V]
Filter makes a new collection containing only the key-value pairs matched by f.
func (Collection[K, V]) Flip ¶
func (c Collection[K, V]) Flip() Collection[K, V]
Flip calls Collection.FlipE, omitting the error.
func (Collection[K, V]) FlipE ¶ added in v0.0.4
func (c Collection[K, V]) FlipE() (Collection[K, V], error)
FlipE makes a new collection, flipping the values with the keys.
func (Collection[K, V]) Forget ¶ added in v0.0.4
func (c Collection[K, V]) Forget(k K) Collection[K, V]
Forget deletes the given key and returns the collection.
func (Collection[K, V]) ForgetE ¶ added in v0.0.4
func (c Collection[K, V]) ForgetE(k K) (Collection[K, V], error)
ForgetE checks if the key exist before attempting to delete it. Should the key not exist, an instance of errors.KeyNotFoundError is returned. The original collection is always returned.
func (*Collection[K, V]) Get ¶
func (c *Collection[K, V]) Get(k K) V
Get calls GetE, omitting the error.
func (Collection[K, V]) GetE ¶
func (c Collection[K, V]) GetE(k K) (V, error)
GetE indexes the map with k, returning the corresponding value when it exists. Should k not exist, a zeroed T value and an instance of errors.KeyNotFound error is returned. This function is safe to be used with empty maps.
func (Collection[K, V]) IsEmpty ¶
func (c Collection[K, V]) IsEmpty() bool
IsEmpty checks if the collection is empty.
func (Collection[K, V]) Keys ¶
func (c Collection[K, V]) Keys() slice.Collection[K]
Keys returns a new slice.Collection containing all of the maps keys. Ordering is not guaranteed.
func (Collection[K, V]) KeysValues ¶ added in v0.0.4
func (c Collection[K, V]) KeysValues() (slice.Collection[K], slice.Collection[V])
KeysValues returns two slices containing all of the keys and values of the map. Indexes from both slice will correspond to the former key-value pair (i.e. calling Combine(collection.KeysValues()) returns a new collection equivalent to the one that originated the new collection).
func (Collection[K, V]) Map ¶
func (c Collection[K, V]) Map(f func(k K, v V) V) Collection[K, V]
Map applies f to each element of the map and builds a new map with f's returned value. The built map is returned. Order is not guaranteed.
func (Collection[K, V]) Merge ¶
func (c Collection[K, V]) Merge(other Collection[K, V]) Collection[K, V]
Merge works similarly to Concat, but overrides conflicting keys.
func (Collection[K, V]) Only ¶
func (c Collection[K, V]) Only(keys []K) Collection[K, V]
Only returns a new collection containing only the key-value pairs from the keys slice.
func (Collection[K, V]) Put ¶
func (c Collection[K, V]) Put(k K, v V) Collection[K, V]
Put inserts v in the key represented by k. If k already exists on the map, it's value is overridden.
func (Collection[K, V]) Reject ¶
func (c Collection[K, V]) Reject(f func(k K, v V) bool) Collection[K, V]
Reject makes a new collection containing only the kill value pairs not matched by f.
func (Collection[K, V]) Search ¶
func (c Collection[K, V]) Search(v V) K
Search uses SearchE, omitting the error.
func (Collection[K, V]) SearchE ¶
func (c Collection[K, V]) SearchE(v V) (K, error)
SearchE searches for v in the collection. The evaluation is done using reflect.DeepEqual. Should the value be found, it's key is returned. Otherwise, T's zeroed value and an instance of errors.ValueNotFoundError is returned. Considering the lack of ordering on maps, should multiple matching values exist, multiple calls to SearchE may return multiple different keys.
func (Collection[K, V]) Tap ¶
func (c Collection[K, V]) Tap(f func(Collection[K, V])) Collection[K, V]
Tap passes the collection to f and returns the collection.
func (Collection[K, V]) ToSlice ¶
func (c Collection[K, V]) ToSlice() []V
ToSlice is an alias to Collection.Values.
func (Collection[K, V]) Unless ¶
func (c Collection[K, V]) Unless( execute bool, f func(collection Collection[K, V]) Collection[K, V], ) Collection[K, V]
Unless calls f with the collection when execute is false. Usually, execute will be the result of a function call. E.g.: c.Unless(c.IsEmpty(), func(c Collection){ fmt.Printf("%v", c)}) prints the collection when the collection is not empty.
func (Collection[K, V]) UnlessEmpty ¶
func (c Collection[K, V]) UnlessEmpty( f func(collection Collection[K, V]) Collection[K, V], ) Collection[K, V]
UnlessEmpty calls f with the collection when the collection is not empty.
func (Collection[K, V]) UnlessNotEmpty ¶
func (c Collection[K, V]) UnlessNotEmpty( f func(collection Collection[K, V]) Collection[K, V], ) Collection[K, V]
UnlessNotEmpty calls f with the collection when the collection is empty.
func (Collection[K, V]) Values ¶ added in v0.0.4
func (c Collection[K, V]) Values() slice.Collection[V]
Values returns a new slice.Collection containing all of the map's values. Ordering is not guaranteed.
func (Collection[K, V]) When ¶
func (c Collection[K, V]) When( execute bool, f func(collection Collection[K, V]) Collection[K, V], ) Collection[K, V]
When calls f with the collection when execute is true. Usually, execute will be the result of a function call. E.g.: c.When(!c.IsEmpty(), func(c Collection){ fmt.Printf("%v", c)}) prints the collection when the collection is not empty.
func (Collection[K, V]) WhenEmpty ¶
func (c Collection[K, V]) WhenEmpty( f func(collection Collection[K, V]) Collection[K, V], ) Collection[K, V]
WhenEmpty calls f with the collection when the collection is empty.
func (Collection[K, V]) WhenNotEmpty ¶
func (c Collection[K, V]) WhenNotEmpty( f func(collection Collection[K, V]) Collection[K, V], ) Collection[K, V]
WhenNotEmpty calls f with the collection when the collection is not empty.