generic

package module
v0.0.0-...-26b6835 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: Unlicense Imports: 7 Imported by: 0

README

go-generic

[!NOTE] This project is under development. Breaking changes are to be expected.

Go Reference Go Report Card

This repository contains a collection of Go utility functions and types built with Go's generics.

Example usage

package main

import (
	"fmt"

	"github.com/NublyBR/go-generic"
)

func main() {
	ints := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

	// Filter

	evens := generic.Filter(func(i int) bool {
		return i%2 == 0
	}, ints...)

	fmt.Println(evens) // [2 4 6 8 10 12 14 16 18 20]

	// Map

	mult := generic.Map(func(i int) int {
		return i * 2
	}, ints...)

	fmt.Println(mult) // [2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40]
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnmarshalError = errors.New("unmarshal error")

Functions

func Abs

func Abs[T Signed](value T) T

func All

func All[T any](slice []T, fn func(T) bool) bool

All checks if a given function fn returns true for all elements in the slice slice of type T.

func Any

func Any[T any](slice []T, fn func(T) bool) bool

Any checks if a given function fn returns true for at least one element in the slice slice of type T.

func AsMap

func AsMap[K comparable, V any](slice Items[K, V]) map[K]V

func Average

func Average[T Numeric](args ...T) T

Average calculates the average value of the arguments.

This function will round implicitly with integer types.

func Clamp

func Clamp[T Orderable](value, min, max T) T

Clamp restricts a value to be within the range [min, max].

func CopyMap

func CopyMap[K comparable, V any](mp map[K]V) map[K]V

func CopySlice

func CopySlice[T any](slice []T) []T

func EqualApprox

func EqualApprox[T Numeric](lhs, rhs, delta T) bool

func EqualsMap

func EqualsMap[K, V comparable](a, b map[K]V) bool

func EqualsSlice

func EqualsSlice[T comparable](a, b []T) bool

func EqualsSlice2

func EqualsSlice2[T comparable](a, b [][]T) bool

func Filter

func Filter[T any](slice []T, fn func(T) bool) []T

func FilterMap

func FilterMap[K comparable, V any](mp map[K]V, fn func(K, V) bool) map[K]V

func If

func If[T any](cond bool, trueVal, falseVal T) T

If evaluates a condition and returns either the trueVal or falseVal based on the condition.

func Map

func Map[A any, B any](slice []A, fn func(A) B) []B

func MapRange

func MapRange[T Numeric](value, inMin, inMax, outMin, outMax T) T

MapRange linearly maps a value from the range [inMin, inMax] to the range [outMin, outMax].

func Max

func Max[T Orderable](args ...T) T

Max returns the maximum value among the arguments.

func Min

func Min[T Orderable](args ...T) T

Min returns the minimum value among the arguments.

func Partition

func Partition[T any](slice []T, fn func(T) bool) (match []T, rest []T)

func Reverse

func Reverse[T any](slice []T) []T

func ReverseInPlace

func ReverseInPlace[T any](slice []T)

func Sort

func Sort[T Orderable](slice []T)

func SortBy

func SortBy[T any, K Orderable](slice []T, fn func(T) K)

func SortFunc

func SortFunc[T any](slice []T, less func(a, b T) bool)

func Sorted

func Sorted[T Orderable](slice []T) []T

func SortedBy

func SortedBy[T any, K Orderable](slice []T, fn func(T) K) []T

func SortedFunc

func SortedFunc[T any](slice []T, less func(a, b T) bool) []T

func Sum

func Sum[T Orderable](args ...T) T

Sum returns the sum of all arguments.

func Unique

func Unique[T comparable](slice []T) []T

func UniqueBy

func UniqueBy[K comparable, T any](slice []T, fn func(T) K) []T

func Zip

func Zip[T any](args ...[]T) [][]T

Types

type Item

type Item[K comparable, V any] struct {
	Key   K
	Value V
}

func (Item[K, V]) String

func (item Item[K, V]) String() string

type Items

type Items[K comparable, V any] []Item[K, V]

func AsItems

func AsItems[K comparable, V any](mp map[K]V) Items[K, V]

func (Items[K, V]) Map

func (items Items[K, V]) Map() map[K]V

func (Items[K, V]) String

func (items Items[K, V]) String() string

func (Items[K, V]) TopN

func (items Items[K, V]) TopN(n int, less func(lhs, rhs Item[K, V]) bool) Items[K, V]

type Iterator

type Iterator[T any] func() (T, bool)

func NewIterator

func NewIterator[T any](slice []T) Iterator[T]

type Numeric

type Numeric interface {
	Signed | Unsigned
}

type Once

type Once[K comparable] struct {
	Keys []K
}

func (*Once[K]) First

func (o *Once[K]) First(key K) bool

type Orderable

type Orderable cmp.Ordered

type OrderedMap

type OrderedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewOrderedMap

func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V]

New empty OrderedMap

func NewOrderedMapFromSorted

func NewOrderedMapFromSorted[K Orderable, V any](mp map[K]V) *OrderedMap[K, V]

New OrderedMap from regular map, with keys sorted

func (*OrderedMap[K, V]) AsList

func (om *OrderedMap[K, V]) AsList() *OrderedMap[K, V]

Mark OrderedMap to be encoded as a list of lists when marshaled as JSON

func (*OrderedMap[K, V]) Delete

func (om *OrderedMap[K, V]) Delete(key K) V

Delete given key

func (*OrderedMap[K, V]) Exists

func (om *OrderedMap[K, V]) Exists(key K) bool

Check if given key exists in map

func (*OrderedMap[K, V]) Get

func (om *OrderedMap[K, V]) Get(key K) V

Get value in map by key

func (*OrderedMap[K, V]) GetOk

func (om *OrderedMap[K, V]) GetOk(key K) (V, bool)

Get value in map by key with ok

func (*OrderedMap[K, V]) GetOr

func (om *OrderedMap[K, V]) GetOr(key K, default_ V) V

Get value in map by key or default

func (*OrderedMap[K, V]) Iter

func (om *OrderedMap[K, V]) Iter(fn func(K, V))

Iterate over OrderedMap with function

func (*OrderedMap[K, V]) IterBreak

func (om *OrderedMap[K, V]) IterBreak(fn func(K, V) bool)

Iterate over OrderedMap with function that can break at any point

Return false to break, true to continue

func (*OrderedMap[K, V]) Keys

func (om *OrderedMap[K, V]) Keys() []K

Get all keys

func (*OrderedMap[K, V]) Len

func (om *OrderedMap[K, V]) Len() int

Get OrderedMap length

func (*OrderedMap[K, V]) MarshalJSON

func (om *OrderedMap[K, V]) MarshalJSON() ([]byte, error)

func (*OrderedMap[K, V]) Set

func (om *OrderedMap[K, V]) Set(key K, value V)

Set value in OrderedMap

func (*OrderedMap[K, V]) SortKeys

func (om *OrderedMap[K, V]) SortKeys(less func(a, b K) bool)

Sort OrderedMap keys by given less function

func (*OrderedMap[K, V]) String

func (om *OrderedMap[K, V]) String() string

Get string representation of OrderedMap

ordered-map[key1: val1, key2: val2]

func (*OrderedMap[K, V]) ToMap

func (om *OrderedMap[K, V]) ToMap() map[K]V

Convert to regular unordered map

func (*OrderedMap[K, V]) UnmarshalJSON

func (om *OrderedMap[K, V]) UnmarshalJSON(b []byte) error

func (*OrderedMap[K, V]) Values

func (om *OrderedMap[K, V]) Values() []V

Get all values

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~float32 | ~float64
}

type Stack

type Stack[T any] []T

func NewStack

func NewStack[T any](elems ...T) Stack[T]

Create new empty or pre-populated stack

func (*Stack[T]) Clear

func (s *Stack[T]) Clear()

Empty the stack

func (*Stack[T]) Len

func (s *Stack[T]) Len() int

Get current length of the stack

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() T

Pop an element from the stack

Info: Popping from an empty stack will cause a panic

func (*Stack[T]) PopN

func (s *Stack[T]) PopN(n int) []T

Pop N elements from the stack

Info: Popping from an empty stack will cause a panic

func (*Stack[T]) Push

func (s *Stack[T]) Push(elem ...T)

Push elements to the stack

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Zipped

type Zipped[A, B any] struct {
	A A
	B B
}

func Zip2

func Zip2[A, B any](a []A, b []B) []Zipped[A, B]

Jump to

Keyboard shortcuts

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