comp

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultPend added in v0.5.3

func DefaultPend[T any](x, y T) func() int

DefaultPend creates a default comparison and pends comparing the two given values until the returned method is called.

func Equal

func Equal[T any](a, b T) bool

Equal determines if the two values are equal.

This will check if the objects are Equatable, otherwise it will fallback to a DeepEqual. This will not check for Equatable within a slice, array, map, etc only in the top level object.

This will not check for Comparable types. Any struct that implements Comparable should also implement Equatable where both agree.

func Or

func Or(comps ...func() int) int

Or will return the first non-zero value returned by a comparison or it will return zero.

The given functions will only be evaluated if all prior tests have returned zero.

Types

type Comparable

type Comparable[T any] interface {
	// CompareTo returns a comparison result of this object and the given object.
	//
	// The comparison results should be:
	// `< 0` if this is less than the given other,
	// `== 0` if this equals the given other,
	// `> 0` if this is greater than the given other.
	CompareTo(other T) int
}

Comparable is an object which can be compared against another object.

The given T variable type is typically the type that is implementing this interface.

type Comparer

type Comparer[T any] func(x, y T) int

Comparer returns the comparison result between the two given values.

The comparison results should be: `< 0` if x is less than y, `== 0` if x equals y, `> 0` if x is greater than y.

func Bool

func Bool() Comparer[bool]

Bool is a comparer that compares the given boolean values.

| x | y | result | |:---:|:---:|:------:| | F | F | 0 | | F | T | -1 | | T | F | 1 | | T | T | 0 |

func ComparableComparer

func ComparableComparer[T Comparable[T]]() Comparer[T]

ComparableComparer returns a comparer which compares the given comparable type.

func Default

func Default[T any]() Comparer[T]

Default tries to create a comparer for the given type. If a comparer isn't able to be created, this will return nil.

func Descender

func Descender[T any](cmp Comparer[T]) Comparer[T]

Descender returns a comparer which negates the given comparer.

Typically negating a comparer will change a sort from ascending to descending.

func Duration

func Duration() Comparer[time.Duration]

Duration returns a comparer which compares the given time duration.

func Epsilon

func Epsilon[T Num](epsilon T) Comparer[T]

Epsilon returns an epsilon comparer which compares the given floating point types.

An epsilon comparator should be used when comparing calculated floating point numbers since calculations may accrue small errors and make the actual value very close to the expected value but not exactly equal. The two floating point values are equal if very close to each other. The values must be within the given epsilon to be considered equal.

The given epsilon must be greater than zero. If the epsilon is less than or equal to zero, this will fallback to an ordered comparer.

func FromLess

func FromLess[T any](less IsLessThan[T]) Comparer[T]

FromLess returns a comparer which compares two values using a IsLessThan function to perform the comparison.

func Ordered

func Ordered[T cmp.Ordered]() Comparer[T]

Ordered returns a comparer which compares the given ordered type.

func Slice

func Slice[S ~[]T, T any](elemCmp Comparer[T]) Comparer[S]

Slice compares two slices with the given element comparer.

func Time

func Time() Comparer[time.Time]

Time returns a comparer which compares the given time duration.

func (Comparer[T]) Pend

func (cmp Comparer[T]) Pend(x, y T) func() int

Pend will wait to perform the given comparison until the returned method is called. This is designed to help with `Or`.

func (Comparer[T]) Reverse

func (cmp Comparer[T]) Reverse() Comparer[T]

Reverse gets a comparer that performs the opposite comparison. This makes a typical ascending sort into a descending sort.

func (Comparer[T]) ToLess

func (cmp Comparer[T]) ToLess() IsLessThan[T]

ToLess gets an IsLessThan for this comparer.

type Equatable

type Equatable interface {
	// Equals returns true if this object and the given object are equal.
	Equals(other any) bool
}

Equatable is an object which can be checked for equality against another object.

type IsLessThan

type IsLessThan[T any] func(x, y T) bool

IsLessThan returns true if the given x is less than the given y, otherwise false.

type Num

type Num interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~uintptr | ~float32 | ~float64
}

Num is any number value type.

Jump to

Keyboard shortcuts

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