common

package
v0.23.8-dev.2 Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package common contains common types and functions.

Index

Constants

This section is empty.

Variables

View Source
var KilometersPerHour = &speedUnitImpl{
	distanceUnit: Kilometers,
	duration:     time.Hour,
}

KilometersPerHour is a speed unit of kilometers per hour.

View Source
var MetersPerSecond = &speedUnitImpl{
	distanceUnit: Meters,
	duration:     time.Second,
}

MetersPerSecond is a speed unit of meters per second.

View Source
var MilesPerHour = &speedUnitImpl{
	distanceUnit: Miles,
	duration:     time.Hour,
}

MilesPerHour is a speed unit of miles per hour.

Functions

func All

func All[E any](s []E, condition bool, predicate func(E) bool) bool

All returns true if all the given predicate evaluations evaluate to condition.

func AllFalse

func AllFalse[E any](s []E, predicate func(E) bool) bool

AllFalse returns true if all the given predicate evaluations is false.

func AllTrue

func AllTrue[E any](s []E, predicate func(E) bool) bool

AllTrue returns true if all the given predicate evaluations are true.

func DefensiveCopy

func DefensiveCopy[T any](v []T) []T

DefensiveCopy returns a defensive copy of a slice.

func DurationValue

func DurationValue(
	distance Distance,
	speed Speed,
	timeUnit time.Duration,
) float64

DurationValue returns the value of a duration in the given time unit. Will panic if the time unit is zero.

func Filter

func Filter[T any](v []T, f func(T) bool) []T

Filter filters a slice using a predicate function.

func FindIndex

func FindIndex[E any](s []E, predicate func(E) bool) int

FindIndex returns the first index i satisfying predicate(s[i]).

func GroupBy

func GroupBy[T any, K comparable](s []T, f func(T) K) map[K][]T

GroupBy groups the elements of a slice by a key function.

func Has

func Has[E any](s []E, condition bool, predicate func(E) bool) bool

Has returns true if any of the given predicate evaluations is condition.

func HasFalse

func HasFalse[E any](s []E, predicate func(E) bool) bool

HasFalse returns true if any of the given predicate evaluations evaluate to false.

func HasTrue

func HasTrue[E any](s []E, predicate func(E) bool) bool

HasTrue returns true if any of the given predicate evaluations evaluate to true.

func Map

func Map[T any, R any](v []T, f func(T) R) []R

Map maps a slice of type T to a slice of type R using the function f.

func MapSlice

func MapSlice[T any, R any](v []T, f func(T) []R) []R

MapSlice maps a slice of type T to a slice of type R using the function f returning a slice of R.

func NSmallest

func NSmallest[T any](items []T, f func(T) float64, n int) []T

NSmallest returns the n-smallest items in the slice items using the function f to determine the value of each item. If n is greater than the length of items, all items are returned.

func RandomElement

func RandomElement[T any](
	source *rand.Rand,
	elements []T,
) T

RandomElement returns a random element from the given slice. If the slice is empty, panic is raised. If source is nil, a new source is created using the current time.

func RandomElementIndices

func RandomElementIndices[T any](
	source *rand.Rand,
	elements []T,
	n int,
) []int

RandomElementIndices returns a slice of n random element indices from the given slice. If n is greater than the length of the slice, all indices are returned. If n is less than or equal to zero, an empty slice is returned. If source is nil, a new source is created using the current time.

func RandomElements

func RandomElements[T any](
	source *rand.Rand,
	elements []T,
	n int,
) []T

RandomElements returns a slice of n random elements from the given slice. If n is greater than the length of the slice, all elements are returned. If n is less than or equal to zero, an empty slice is returned. If source is nil, a new source is created using the current time.

func RandomIndex

func RandomIndex(source *rand.Rand, size int, indicesUsed map[int]bool) int

RandomIndex returns a random index from the given size. If the index has already been used, a new index is generated. If source is nil, a new source is created using the current time.

func Unique

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

Unique is a universal duplicate removal function for type instances in a slice that implement the comparable interface.

func UniqueDefined

func UniqueDefined[T any, I comparable](items []T, f func(T) I) []T

UniqueDefined is a universal duplicate removal function for type instances in a slice that implement the comparable interface. The function f is used to extract the comparable value from the type instance.

func Values

func Values[M ~map[K]V, K comparable, V any](m M) []V

Values returns a slice of all values in the given map.

func WithinTolerance

func WithinTolerance(a, b, tolerance float64) bool

WithinTolerance returns true if a and b are within the given tolerance.

Types

type Alias

type Alias interface {
	Sample(rng *rand.Rand) int
}

Alias is an interface that allows for sampling from a discrete distribution in O(1) time.

func NewAlias

func NewAlias(weights []float64) (Alias, error)

NewAlias creates a new Alias from the given weights. The weights must be positive and at least one weight must be given. The weights are normalized to sum to 1. NewAlias([]float64{1, 2, 3}) will return an Alias that will return 0 with probability 1/6, 1 with probability 1/3 and 2 with probability 1/2.

type Distance

type Distance struct {
	// contains filtered or unexported fields
}

Distance is a distance in a given unit.

func Haversine

func Haversine(from, to Location) (Distance, error)

Haversine calculates the distance between two locations using the Haversine formula. Haversine is a good approximation for short distances (up to a few hundred kilometers).

func NewDistance

func NewDistance(
	meters float64,
	unit DistanceUnit,
) Distance

NewDistance returns a new distance.

func (Distance) Value

func (d Distance) Value(unit DistanceUnit) float64

Value returns the distance in the specified unit.

type DistanceUnit

type DistanceUnit int

DistanceUnit is the unit of distance.

const (
	// Kilometers is 1000 meters.
	Kilometers DistanceUnit = iota
	// Meters is the distance travelled by light in a vacuum in
	// 1/299,792,458 seconds.
	Meters
	// Miles is 1609.34 meters.
	Miles
)

func (DistanceUnit) String

func (d DistanceUnit) String() string

String returns the string representation of the distance unit.

type FastHaversine

type FastHaversine struct {
	// contains filtered or unexported fields
}

FastHaversine is a fast approximation of the haversine distance.

func NewFastHaversine

func NewFastHaversine(lat float64) FastHaversine

NewFastHaversine returns a new FastHaversine.

func (FastHaversine) Distance

func (f FastHaversine) Distance(from, to Location) (float64, error)

Distance returns the distance between two locations in meters.

type Lazy

type Lazy[T any] func() T

Lazy is a function that returns a value of type T. The value is only calculated once, and the result is cached for subsequent calls.

func DefineLazy

func DefineLazy[T any](f func() T) Lazy[T]

DefineLazy returns a Lazy[T] that will call the given function to calculate the value. The value is only calculated once, and the result is cached for subsequent calls.

type Location

type Location interface {
	// Longitude returns the longitude of the location.
	Longitude() float64
	// Latitude returns the latitude of the location.
	Latitude() float64
	// Equals returns true if the location is equal to the location given as an
	// argument.
	Equals(Location) bool
	// IsValid returns true if the location is valid. A location is valid if
	// the bounds of the longitude and latitude are correct.
	IsValid() bool
}

Location represents a physical location on the earth.

func NewInvalidLocation

func NewInvalidLocation() Location

NewInvalidLocation creates a new invalid Location. Longitude and latitude are not important.

func NewLocation

func NewLocation(longitude float64, latitude float64) (Location, error)

NewLocation creates a new Location. An error is returned if the longitude is not between (-180, 180) or the latitude is not between (-90, 90).

type Locations

type Locations []Location

Locations is a slice of Location.

func (Locations) Centroid

func (l Locations) Centroid() (Location, error)

Centroid returns the centroid of the locations. If locations is empty, the centroid will be an invalid location.

func (Locations) Unique

func (l Locations) Unique() Locations

Unique returns a new slice of Locations with unique locations.

type Speed

type Speed interface {
	// Value returns the speed in the specified unit.
	Value(unit SpeedUnit) float64
}

Speed is the interface for a speed.

func NewSpeed

func NewSpeed(
	distance float64,
	unit SpeedUnit,
) Speed

NewSpeed creates a new speed.

type SpeedUnit

type SpeedUnit interface {
	// DistanceUnit returns the distance unit of the speed unit.
	DistanceUnit() DistanceUnit
	// Duration returns the duration of the speed unit.
	Duration() time.Duration
}

SpeedUnit represents a unit of speed.

func NewSpeedUnit

func NewSpeedUnit(
	distanceUnit DistanceUnit,
	duration time.Duration,
) SpeedUnit

NewSpeedUnit returns a new speed unit.

type Statistics

type Statistics struct {
	Count              float64
	Sum                float64
	Average            float64
	Maximum            float64
	MaximumIndex       int
	Minimum            float64
	MinimumIndex       int
	StandardDeviation  float64
	Quartile1          float64
	Quartile2          float64
	Quartile3          float64
	InterQuartileRange float64
	MidHinge           float64
}

Statistics describes statistics.

func NewStatistics

func NewStatistics[T any](v []T, f func(T) float64) Statistics

NewStatistics creates a new statistics object.

func (Statistics) Report

func (s Statistics) Report() string

Report returns a string containing a report of the statistics.

Jump to

Keyboard shortcuts

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