Documentation
¶
Overview ¶
Package common contains common types and functions.
Index ¶
- Variables
- func All[E any](s []E, condition bool, predicate func(E) bool) bool
- func AllFalse[E any](s []E, predicate func(E) bool) bool
- func AllTrue[E any](s []E, predicate func(E) bool) bool
- func DefensiveCopy[T any](v []T) []T
- func DurationValue(distance Distance, speed Speed, timeUnit time.Duration) float64
- func Filter[T any](v []T, f func(T) bool) []T
- func FindIndex[E any](s []E, predicate func(E) bool) int
- func GroupBy[T any, K comparable](s []T, f func(T) K) map[K][]T
- func Has[E any](s []E, condition bool, predicate func(E) bool) bool
- func HasFalse[E any](s []E, predicate func(E) bool) bool
- func HasTrue[E any](s []E, predicate func(E) bool) bool
- func Map[T any, R any](v []T, f func(T) R) []R
- func MapSlice[T any, R any](v []T, f func(T) []R) []R
- func NSmallest[T any](items []T, f func(T) float64, n int) []T
- func RandomElement[T any](source *rand.Rand, elements []T) T
- func RandomElementIndices[T any](source *rand.Rand, elements []T, n int) []int
- func RandomElements[T any](source *rand.Rand, elements []T, n int) []T
- func RandomIndex(source *rand.Rand, size int, indicesUsed map[int]bool) int
- func Unique[T comparable](s []T) []T
- func UniqueDefined[T any, I comparable](items []T, f func(T) I) []T
- func Values[M ~map[K]V, K comparable, V any](m M) []V
- func WithinTolerance(a, b, tolerance float64) bool
- type Alias
- type Distance
- type DistanceUnit
- type FastHaversine
- type Lazy
- type Location
- type Locations
- type Speed
- type SpeedUnit
- type Statistics
Constants ¶
This section is empty.
Variables ¶
var KilometersPerHour = &speedUnitImpl{ distanceUnit: Kilometers, duration: time.Hour, }
KilometersPerHour is a speed unit of kilometers per hour.
var MetersPerSecond = &speedUnitImpl{ distanceUnit: Meters, duration: time.Second, }
MetersPerSecond is a speed unit of meters per second.
var MilesPerHour = &speedUnitImpl{ distanceUnit: Miles, duration: time.Hour, }
MilesPerHour is a speed unit of miles per hour.
Functions ¶
func DefensiveCopy ¶
func DefensiveCopy[T any](v []T) []T
DefensiveCopy returns a defensive copy of a slice.
func DurationValue ¶
DurationValue returns the value of a duration in the given time unit. Will panic if the time unit is zero.
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 MapSlice ¶
MapSlice maps a slice of type T to a slice of type R using the function f returning a slice of R.
func NSmallest ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
WithinTolerance returns true if a and b are within the given tolerance.
Types ¶
type Alias ¶
Alias is an interface that allows for sampling from a discrete distribution in O(1) time.
func NewAlias ¶
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 ¶
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.
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 ¶
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.
type Locations ¶
type Locations []Location
Locations is a slice of Location.
type Speed ¶
type Speed interface {
// Value returns the speed in the specified unit.
Value(unit SpeedUnit) float64
}
Speed is the interface for a 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.