generics

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2025 License: BSD-3-Clause Imports: 2 Imported by: 0

README

Generics! by BABYdotRAR

A collection of some utilities involving generic types and some data structures.

Installation

go get github.com/BABYdotRAR/[email protected]

Stack Example

package main

import (
	"fmt"

	"github.com/BABYdotRAR/generics"
	ds "github.com/BABYdotRAR/generics/data_structures"
)

func stackExample() {
	// Let's create a stack of maps
	stack := ds.NewStack[map[int]string]()
	mapA := map[int]string{1: "Rem", 2: "is"}
	mapB := map[int]string{3: "the", 4: "best", 5: "waifu"}
	stack.Push(mapB)
	stack.Push(mapA)
	fmt.Printf("Current stack size: %d\n", stack.Size())
	popA, _ := stack.Pop()
	popB, _ := stack.Pop()
	if stack.IsEmpty() {
		// Now let's use other helpful functions
		valuesA := generics.MapValues(popA)
		valuesB := generics.MapValues(popB)
		fmt.Println(valuesA, valuesB)
	}
}

Result
Current stack size: 2
[Rem is] [the best waifu]

Queue Example

package main

import (
	"fmt"

	"github.com/BABYdotRAR/generics"
	ds "github.com/BABYdotRAR/generics/data_structures"
)

func queueExample() {
	// simple queue of numbers
	queue := ds.NewQueue[int]()

	// enqueue some elements
	queue.Enqueue(2)
	queue.Enqueue(4)
	queue.Enqueue(2)
	queue.Enqueue(6)
	queue.Enqueue(5)

	var res []int

	// store the current size of the queue
	res = append(res, queue.Size())
	// store all dequeues
	dequeue, _ := queue.Dequeue()
	res = append(res, dequeue)
	dequeue, _ = queue.Dequeue()
	res = append(res, dequeue)
	dequeue, _ = queue.Dequeue()
	res = append(res, dequeue)
	dequeue, _ = queue.Dequeue()
	res = append(res, dequeue)
	dequeue, _ = queue.Dequeue()
	res = append(res, dequeue)
	// if an empty queue is dequeued, it returns the zero value and false
	zero, ok := queue.Dequeue()
	fmt.Println(zero, ok)
	// let's see what res looks like
	fmt.Println(res)
	// some values are duplicated, let's remove them
	fmt.Println(generics.Unique(res))
}

Result
0 false
[5 2 4 2 6 5]
[5 2 4 6]

Documentation

Overview

Package generics provides utility functions and data structures that make working with Go generics easier.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clamp

func Clamp[T cmp.Ordered](low, high, val T) T

Clamp limits the value of val, if it's lower than low, it returns low, if val is higher that high, it returns high, otherwise returns val

func Coalesce

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

Coalesce, just like the sql function, returns the first non-zero value from values, if all values are zero, then the function returns the zero value

func CoalesceByFunc

func CoalesceByFunc[T any](isEqual func(T, T) bool, values ...T) T

CoalesceByFunc, just like the sql function, returns the first non-zero value from values, it uses isEqual(T, T) to determine whether the values are equals to the zero value, if all values are zero, then the function returns the zero value

func CompareSlice

func CompareSlice[T comparable](s, target []T) bool

CompareSlice determines whether 's' and 'target' are equals (same elements at the same position and same slice lengths)

func ConvertByJSON

func ConvertByJSON[T any](src any) (dest T, err error)

ConvertByJSON is a shortcut to the common use of the functions Marshal and Unmarshal from the json package

func InBetween

func InBetween[T cmp.Ordered](low, high, val T) bool

InBetween checks if val belongs to (low, high)

func InBetweenIncluding

func InBetweenIncluding[T cmp.Ordered](low, high, val T) bool

InBetweenIncluding checks if val belongs to [low, high]

func InBetweenIncludingHigh

func InBetweenIncludingHigh[T cmp.Ordered](low, high, val T) bool

InBetweenIncludingHigh checks if val belongs to (low, high]

func InBetweenIncludingLow

func InBetweenIncludingLow[T cmp.Ordered](low, high, val T) bool

InBetweenIncludingLow checks if val belongs to [low, high)

func IsSubset

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

IsSubset returns whether a is subset of b (duplicates ignored)

func MapKeys

func MapKeys[K comparable, V any](m map[K]V) (keys []K)

MapKeys returns the keys from m as slice

func MapKeysAndValues

func MapKeysAndValues[K comparable, V any](m map[K]V) (keys []K, values []V)

MapKeysAndValues returns the keys and values of a map as separate slices

func MapValues

func MapValues[K comparable, V any](m map[K]V) (values []V)

MapValues returns the values from m as slice

func Max

func Max[T cmp.Ordered](values ...T) T

Max returns the maximum element of values

func MaxByFunc

func MaxByFunc[T any](less func(a, b T) bool, values ...T) T

MaxByFunc returns the maximum element of values using the less function. less: returns whether a < b

func Min

func Min[T cmp.Ordered](values ...T) T

Min returns the minimum element of values

func MinByFunc

func MinByFunc[T any](less func(a, b T) bool, values ...T) T

MinByFunc returns the minimum element of values using the less function. less: returns whether a < b

func SliceToBoolMap

func SliceToBoolMap[K comparable, M ~[]K](s M) (res map[K]bool)

SliceToBoolMap transforms a slice of comparables to a boolean map with all elements set to 'true'

func SliceToMap

func SliceToMap[K comparable, M ~[]K, T any](s M, defaultValue T) (res map[K]T)

SliceToMap transforms a slice of comparables to a custom map

func Unique

func Unique[K comparable](s []K) (u []K)

Unique returns all unique elements in s

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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