Documentation ¶
Overview ¶
Package typ includes generic types that are missing from the Go standard library. This includes types for sets, sorted slices, trees, etc.
Index ¶
- func Abs[T Real](v T) T
- func Clamp[T Ordered](v, min, max T) T
- func Clamp01[T Real](v T) T
- func Coal[T comparable](values ...T) T
- func Compare[T Ordered](a, b T) int
- func DerefZero[P ~*V, V any](ptr P) V
- func Digits10[T Integer](v T) int
- func DigitsSign10[T Integer](v T) int
- func IsNil[T any](value T) bool
- func IsZero[T comparable](value T) bool
- func Less[T Ordered](a, b T) bool
- func Max[T Ordered](v ...T) T
- func Min[T Ordered](v ...T) T
- func Product[T Number](v ...T) T
- func Ref[T any](value T) *T
- func Sum[T Number](v ...T) T
- func Tern[T any](cond bool, ifTrue, ifFalse T) T
- func TernCast[T any](cond bool, value any, ifFalse T) T
- func Zero[T any]() T
- func ZeroOf[T any](T) T
- type Complex
- type Float
- type Integer
- type Number
- type Ordered
- type Real
- type Signed
- type Unsigned
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Abs ¶
func Abs[T Real](v T) T
Abs returns the absolute value of a number, in other words removing the sign, in other words (again) changing negative numbers to positive and leaving positive numbers as-is.
Abs(0) // => 0 Abs(15) // => 15 Abs(-15) // => 15
func Clamp ¶
func Clamp[T Ordered](v, min, max T) T
Clamp returns the value clamped between the minimum and maximum values.
func Clamp01 ¶
func Clamp01[T Real](v T) T
Clamp01 returns the value clamped between 0 (zero) and 1 (one).
func Coal ¶
func Coal[T comparable](values ...T) T
Coal will return the first non-zero value. Equivalent to the "null coalescing" operator from other languages, or the SQL "COALESCE(...)" expression.
var result = null ?? myDefaultValue; // C#, JavaScript, PHP, etc var result = typ.Coal(nil, myDefaultValue) // Go
Example ¶
bindAddressFromUser := "" bindAddressDefault := "localhost:8080" fmt.Println("Adress 1:", typ.Coal(bindAddressFromUser, bindAddressDefault)) bindAddressFromUser = "192.168.1.10:80" fmt.Println("Adress 2:", typ.Coal(bindAddressFromUser, bindAddressDefault))
Output: Adress 1: localhost:8080 Adress 2: 192.168.1.10:80
func Compare ¶
Compare checks if either value is greater or equal to the other. The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
func DerefZero ¶
func DerefZero[P ~*V, V any](ptr P) V
DerefZero returns the dereferenced value, or zero if it was nil.
func Digits10 ¶
Digits10 returns the number of digits in the number as if it would be converted to a string in base 10. This is computed by comparing its value to all orders of 10, making it increadibly faster than calculating logaritms or by performing divisions.
func DigitsSign10 ¶
DigitsSign10 returns the number of digits in the number as if it would be converted to a string in base 10, plus 1 if the number is negative to account for the negative sign. This is computed by comparing its value to all orders of 10, making it increadibly faster than calculating logaritms or by performing divisions.
func IsZero ¶
func IsZero[T comparable](value T) bool
IsZero returns true if the value is zero. If the type implements
IsZero() bool
then that method is also used.
func Product ¶
func Product[T Number](v ...T) T
Product multiplies together all numbers from the arguments. Returns 1 if no arguments.
func Ref ¶
func Ref[T any](value T) *T
Ref returns a pointer to the value. Useful when working with literals.
func Sum ¶
func Sum[T Number](v ...T) T
Sum adds upp all numbers from the arguments. Returns 0 if no arguments.
func Tern ¶
Tern returns different values depending on the given conditional boolean. Equivalent to the "ternary" operator from other languages.
var result = 1 > 2 ? "yes" : "no"; // C#, JavaScript, PHP, etc var result = typ.Tern(1 > 2, "yes", "no") // Go
Example ¶
age := 16 fmt.Println("To drink I want a glass of", typ.Tern(age >= 18, "wine", "milk"))
Output: To drink I want a glass of milk
Types ¶
type Complex ¶
type Complex interface { ~complex64 | ~complex128 }
Complex is a constraint that permits any complex numeric type. If future releases of Go add new predeclared complex numeric types, this constraint will be modified to include them.
type Float ¶
Float is a constraint that permits any floating-point type. If future releases of Go add new predeclared floating-point types, this constraint will be modified to include them.
type Integer ¶
Integer is a constraint that permits any integer type. If future releases of Go add new predeclared integer types, this constraint will be modified to include them.
type Ordered ¶
Ordered is a constraint that permits any ordered type: any type that supports the operators < <= >= >. If future releases of Go add new ordered types, this constraint will be modified to include them.
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
pkg
|
|
arrays
Package arrays contains an implementation of a 2D array.
|
Package arrays contains an implementation of a 2D array. |
avl
Package avl contains a AVL-tree (Adelson-Velsky and Landis tree) implementation, which is a binary search tree (BST) that performs self-balancing on insertion and deletion.
|
Package avl contains a AVL-tree (Adelson-Velsky and Landis tree) implementation, which is a binary search tree (BST) that performs self-balancing on insertion and deletion. |
chans
Package chans contains utility constraints, functions, and types regarding Go channels, such as the PubSub type for fan-out events.
|
Package chans contains utility constraints, functions, and types regarding Go channels, such as the PubSub type for fan-out events. |
lists
Package lists contains a linked list implementation and common list-like data structures, such as a Queue, a Stack, and a Ring type.
|
Package lists contains a linked list implementation and common list-like data structures, such as a Queue, a Stack, and a Ring type. |
maps
Package maps contains utility functions for maps.
|
Package maps contains utility functions for maps. |
sets
Package sets contains a Set implementation, based on the built in map type.
|
Package sets contains a Set implementation, based on the built in map type. |
slices
Package slices contains utility functions for Go slices, as well as a Sorted slice type.
|
Package slices contains utility functions for Go slices, as well as a Sorted slice type. |
sync2
Package sync2 contains forks from the Go "sync" package but with generic implementations, such as a generic Pool, Map, and AtomicValue.
|
Package sync2 contains forks from the Go "sync" package but with generic implementations, such as a generic Pool, Map, and AtomicValue. |