Documentation
¶
Overview ¶
Package oil is stuff that should be language builtins: min, max, tuples, optionals, functions to access maps and while creating the entry if it doesn't exist... A lot of small things to oil your cogs.
Index ¶
- func Abs[T OrderedNumber](x T) T
- func Atoi[T constraints.Signed](s, whatIsIt string, min T, max T) (T, error)
- func Atou[T constraints.Unsigned](s, whatIsIt string, min T, max T) (T, error)
- func FanIn[T any](consumer chan<- T, producers ...<-chan T)
- func FanOut[T any](producer <-chan T, consumers ...chan<- T)
- func First[T any](first T, _ ...any) T
- func Fourth[T any](_, _, _ any, fourth T, _ ...any) T
- func If[T any](b bool, ifTrue, ifFalse T) T
- func Ignore(...any)
- func MapFromSlice[K comparable, V any](slice []K, value V) map[K]V
- func MapGet[K comparable, V any](m map[K]V, key K, defaultValue V) V
- func MapGetOrNew[K comparable, V any](m map[K]V, key K, create func() V) V
- func MapGetOrNewRef[K comparable, V any](m map[K]*V, key K) *V
- func MapSetDefault[K comparable, V any](m map[K]V, key K, value V) map[K]V
- func Max[T constraints.Ordered](a, b T) T
- func Min[T constraints.Ordered](a, b T) T
- func Second[T any](_ any, second T, _ ...any) T
- func Third[T any](_, _ any, third T, _ ...any) T
- type Number
- type Optional
- type OrderedNumber
- type Pair
- type Quadruplet
- type Triplet
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Atoi ¶
func Atoi[T constraints.Signed](s, whatIsIt string, min T, max T) (T, error)
Atoi parses an integer value, verifies that it's between min and max, and if there's a parse error or it's out of bounds, returns an error message that looks like: invalid $whatIsIt blah blah
func Atou ¶
func Atou[T constraints.Unsigned](s, whatIsIt string, min T, max T) (T, error)
Atou parses an unsigned integer value, verifies that it's between min and max, and if there's a parse error or it's out of bounds, returns an error message that looks like: invalid $whatIsIt blah blah
func FanIn ¶
func FanIn[T any](consumer chan<- T, producers ...<-chan T)
FanIn writes anything it reads from a number of channels, the producers, to a single channel, the consumer. If all the producers get closed, it closes the consumer and returns. Whenever there's a write to a producer, the consumer must be read, otherwise, FanIn could get stuck.
func FanOut ¶
func FanOut[T any](producer <-chan T, consumers ...chan<- T)
FanOut replicates everything it reads from a channel, the producer, to an arbitrary number of channels, the consumers. If the producer is closed, FanOut closes the consumers and returns. Whenever there's a write to the producer, all consumers must be read, otherwise, FanOut coudl get stuck.
func If ¶
If mimics the C ternary operator ("?"). It returns ifTrue or ifFalse depending on the value of a boolean.
func Ignore ¶
func Ignore(...any)
Ignore ignores all its arguments and does nothing. It's convenient when a linter bugs you about ignoring an error that you really don't care about.
func MapFromSlice ¶
func MapFromSlice[K comparable, V any](slice []K, value V) map[K]V
MapFromSlice creates a map whose keys are the elements of a slice, and values are all the same.
func MapGet ¶
func MapGet[K comparable, V any](m map[K]V, key K, defaultValue V) V
MapGet gets a value from a map and returns a default if the map doens't have the specified key.
func MapGetOrNew ¶
func MapGetOrNew[K comparable, V any](m map[K]V, key K, create func() V) V
MapGetOrNew gets a value from a map for a given key, or creates it (and inserts it) by calling a user specified function if it doesn't exist. It returns the value.
func MapGetOrNewRef ¶
func MapGetOrNewRef[K comparable, V any](m map[K]*V, key K) *V
MapGetOrNewRef gets a value from a map of references for a given key, or creates it (and inserts it) by calling new if it doesn't exist. It returns the value.
func MapSetDefault ¶
func MapSetDefault[K comparable, V any](m map[K]V, key K, value V) map[K]V
MapSetDefault sets a value in a map for a given key, if that key isn't in the map already. It returns the map itself.
Types ¶
type Number ¶
type Number interface { OrderedNumber | constraints.Complex }
Number is a constraint that permits any number type.
type Optional ¶
Optional wraps any type, allowing values to be either set or unset.
func NewOptional ¶
NewOptional creates a new Optional.
func (Optional[T]) Get ¶
func (o Optional[T]) Get(defaultValue T) T
Get gets the value from an Optional or a default value if it's unset.
func (*Optional[T]) SetDefault ¶
SetDefault sets the value of an Optional if it doesn't have a value already, and returns the Optional itself.
type OrderedNumber ¶
type OrderedNumber interface { constraints.Float | constraints.Integer }
OrderedNumber is a constraint that permits any ordered number type.
type Pair ¶
type Pair[T1, T2 any] struct { First T1 Second T2 }
Pair is a pair of values of arbitrary types.
type Quadruplet ¶
type Quadruplet[T1, T2, T3, T4 any] struct { First T1 Second T2 Third T3 Fourth T4 }
Quadruplet is a quadruplet of values of arbitrary types.
func NewQuadruplet ¶
func NewQuadruplet[T1, T2, T3, T4 any](x1 T1, x2 T2, x3 T3, x4 T4) Quadruplet[T1, T2, T3, T4]
NewQuadruplet creates a Quadruplet.