common

package
v0.22.1-dev.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package common contains common types and functions.

Index

Constants

This section is empty.

Variables

View Source
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

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 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 WithinTolerance

func WithinTolerance(a, b, tolerance float64) bool

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

func Haversine(from, to Location) Distance

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

func NewLocation(
	longitude float64,
	latitude float64,
) Location

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.

func (Location) Latitude

func (l Location) Latitude() float64

func (Location) Longitude

func (l Location) Longitude() float64

func (Location) String

func (l Location) String() string

type Locations

type Locations []Location

Locations is a slice of 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 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.

Jump to

Keyboard shortcuts

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