genericz

package module
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: BSD-2-Clause Imports: 2 Imported by: 0

README

genericz - convenience made possible since generics

The main package contains

  • Min(a ...T) T and Max(a ...T) T
  • Ternary(cond bool, a, b T) T
  • Coalesce(...T) T, CoalesceSlice(...T) T and CoalesceMap(...T) T
  • SortWithData(comparables []C, data []D)

The packages below are intended to contain additions to golang.org/x/exp/slices and golang.org/x/exp/maps and thus won't be the full set that you need. (Over time there will be overlap as we won't remove methods as it would break backwards compatibility.)

mapz

The mapz package contains:

  • KeysSorted(m map[K]V) []K, ValuesSorted(m map[K]V) []V and ValuesSortedByKey(m map[K]V) []V
  • MinKey(m map[K]V) K and MaxKey(m map[K]V) K
  • DeleteWithLock(l sync.Locker, m map[K]V, key K) and StoreWithLock(l sync.Locker, m map[K]V, key K, value V)
  • Ordered(m map[K]V) iter.Seq2[K, V]

The SyncMap is a type-safe sync.Map.

The MutexMap is a variant of SyncMap that uses a mutex and a regular map.

The AppendMap is an append-only map perfect for caching values that never change. It slightly cheaper than a sync.Map because values can't change.

slicez

The slicez packages contains Diff, Filter, Map, Unique, Concat, Sum and Pop.

orderedobject

The orderedobject allows you to decode a JSON dict while preserving order. Can be used with most json encoders/decoders.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Coalesce added in v0.17.0

func Coalesce[T comparable](alternatives ...T) T

Coalesce returns the first argument that's not zero.

func CoalesceMap added in v0.17.0

func CoalesceMap[T ~map[K]V, K comparable, V any](alternatives ...T) T

CoalesceMap returns the first argument that has a len() of more than 0.

func CoalesceSlice added in v0.17.0

func CoalesceSlice[T ~[]V, V any](alternatives ...T) T

CoalesceSlice returns the first argument that has a len() of more than 0.

func Max

func Max[T constraints.Ordered](a ...T) T

Max returns the highest value given.

func Min

func Min[T constraints.Ordered](a ...T) T

Min returns the lowest value given.

func SortWithData added in v0.16.0

func SortWithData[C constraints.Ordered, D any](comparables []C, data []D)

SortWithData sorts two slices together. The slices will be sorted in order of comparables. The data slice is only reordered. SortWithData may fail to sort correctly when sorting slices of floating-point numbers containing Not-a-number (NaN) values.

func Ternary added in v0.8.0

func Ternary[T any](cond bool, a, b T) T

Ternary returns `a` if `cond` is true, `b` otherwise.

Types

This section is empty.

Directories

Path Synopsis
Package orderedobject allows you to decode JSON dicts while preserving order.
Package orderedobject allows you to decode JSON dicts while preserving order.

Jump to

Keyboard shortcuts

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