types

package module
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2021 License: BSD-3-Clause Imports: 5 Imported by: 8

README

types

GoDev GitHub GitHub tag (latest SemVer)

types is a small Go package that provides simple helper functions on arbitrary types. The aim is to provide drop in easy replcaements for rote functions that are sometimes necessary.

If you stumble upon this repo and have an idea that seems to fit for an easy drop in, please open an issue!

Documentation

Overview

Package types contains helper functions for arbitrary types (deep less, deep equal, deep sort).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare(l, r interface{}) int

Compare returns whether l is less than, equal to, or larger than r, following the same rules as Less and Equal.

func DistinctInPlace added in v1.1.0

func DistinctInPlace(sliceptr interface{})

DistinctInPlace accepts a *[]T, sorts it using the rules of Sort in this package, and compacts it in place using the rules of Equal in this package.

This is similar to the slice generic version of sorting and compacting, but allows for even more types to be sorted.

NOTE: When generics are released, this will change to be a func(*[]T). This will be a non-breaking change to anything that does not use this function for a variable of type func(interface{}).

func Equal

func Equal(l, r interface{}) bool

Equal returns whether l is deeply equal to r.The input values must have the same type, or this will panic.

Structs are deeply equal if each field is equal. Unlike reflect, this function compares only public fields.

Nil pointers are equal to nil pointers. Recursive types are equal following all other rules, or if they recurse at the same time.

Slices are equal if they have the same length and each element is deeply equal.

Maps are equal if they have the same length and all keys and values are deeply equal.

Floats are equal if they are both NaN, or if they are both not NaN and are equal.

Complexes are equal if their real is equal and their imaginary is equal.

Chans are equal if they have the same amount of buffered elements.

Functions, interfaces, and unsafe pointers equal if their pointers are equal.

func Less

func Less(l, r interface{}) bool

Less returns whether l is deeply less than r. The input values must have the same type, or this will panic.

Structs are deeply less if each field in order is either less or equal to the comparison field. If every field is equal, this returns false. Only public fields are compared.

Nil pointers are less than non-nil pointers. Recursive types compare less following all other rules, or if they recurse sooner.

Slices are less if they are shorter, or if each element in order is less than or equal to the other. If all elements are equal and the sizes are equal, this returns false.

Maps are less if they are shorter. If they are of equal size, all keys are treated as a slice and they are compared following the same logic used for slices. If all keys are equal, then all values are compared following similar slice logic.

Floats are less if they are NaN, or using a simple comparison.

Complexes are less if their real is less. If the real is equal, then complexes are less if their imaginary is less.

Chans are less if they have fewer buffered elements.

Functions, interfaces, and unsafe pointers are never less than each other.

func Sort

func Sort(s interface{})

Sort deeply sorts any slice anywhere within s, traversing into maps, slices, and exported struct fields. Any non-primitive type is less than the other following the rules of Less.

Note that this function performs value copies. This must not be used to sort types that are not safe to copy. For example, this must not sort []struct{sync.Mutex}, but it can sort []*struct{sync.Mutex}.

If a slice contains a type that has a Less method that takes accepts itself and returns a bool, Sort uses that type's Less method to sort the slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

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