Documentation
¶
Overview ¶
Package common contains common types and functions.
Index ¶
- Variables
- 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 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 WithinTolerance(a, b, tolerance float64) bool
- type Distance
- type DistanceUnit
- type FastHaversine
- type Location
- type Locations
- type Speed
- type SpeedUnit
- type Statistics
Constants ¶
This section is empty.
Variables ¶
var MetersPerSecond = &speedUnitImpl{ distanceUnit: Meters, duration: time.Second, }
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 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 WithinTolerance ¶
WithinTolerance returns true if a and b are within the given tolerance.
Types ¶
type Distance ¶
type Distance struct {
// contains filtered or unexported fields
}
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
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 )
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
Distance returns the distance between two locations in meters.
type Location ¶
type Location struct {
// contains filtered or unexported fields
}
Location represents a location on the earth.
func NewLocation ¶
NewLocation creates a new Location. Will panic if longitude or latitude are out of range. Longitude must be between -180 and 180. Latitude must be between -90 and 90.
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 NewKilometersPerHour ¶
func NewKilometersPerHour() SpeedUnit
NewKilometersPerHour returns a new speed unit of kilometers per hour.
func NewMilesPerHour ¶
func NewMilesPerHour() SpeedUnit
NewMilesPerHour returns a new speed unit of miles per hour.
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
}
func NewStatistics ¶
func NewStatistics[T any](v []T, f func(T) float64) Statistics
NewStatistics creates a new statistics object.