oil

package module
v0.0.0-...-d5f33ea Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2023 License: BSD-3-Clause Imports: 4 Imported by: 1

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[T OrderedNumber](x T) T

Abs returns the absolute value of a number.

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 First

func First[T any](first T, _ ...any) T

First returns its first argument.

func Fourth

func Fourth[T any](_, _, _ any, fourth T, _ ...any) T

Fourth returns its fourth argument.

func If

func If[T any](b bool, ifTrue, ifFalse T) T

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.

func Max

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

Max returns the max of two ordered numbers.

func Min

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

Min returns the min of two ordered numbers.

func Second

func Second[T any](_ any, second T, _ ...any) T

Second returns its second argument.

func Third

func Third[T any](_, _ any, third T, _ ...any) T

Third returns its third argument.

Types

type Number

type Number interface {
	OrderedNumber | constraints.Complex
}

Number is a constraint that permits any number type.

type Optional

type Optional[T any] struct {
	Val   T
	IsSet bool
}

Optional wraps any type, allowing values to be either set or unset.

func NewOptional

func NewOptional[T any](val T, isSet bool) Optional[T]

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]) Set

func (o *Optional[T]) Set(val T) *Optional[T]

Set sets a value in an Optional and returns the Optional itself.

func (*Optional[T]) SetDefault

func (o *Optional[T]) SetDefault(val T) *Optional[T]

SetDefault sets the value of an Optional if it doesn't have a value already, and returns the Optional itself.

func (*Optional[T]) Unset

func (o *Optional[T]) Unset() *Optional[T]

Unset unsets an Optional 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.

func NewPair

func NewPair[T1, T2 any](x1 T1, x2 T2) Pair[T1, T2]

NewPair creates a Pair.

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.

type Triplet

type Triplet[T1, T2, T3 any] struct {
	First  T1
	Second T2
	Third  T3
}

Triplet is a triplet of values of arbitrary types.

func NewTriplet

func NewTriplet[T1, T2, T3 any](x1 T1, x2 T2, x3 T3) Triplet[T1, T2, T3]

NewTriplet creates a Triplet.

Jump to

Keyboard shortcuts

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