Common

package
v0.3.13 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2024 License: MIT Imports: 5 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clean

func Clean(elem any)

Clean removes all the elements from the data structure by calling the Clean method if the element implements the Cleaner interface.

Parameters:

  • cleaner: The data structure to clean.

func Compare added in v0.2.39

func Compare[T Comparable](a, b T) (int, bool)

Compare compares two values of the same type that implement the Comparable interface. If the values are equal, the function returns 0. If the first value is less than the second value, the function returns -1. If the first value is greater than the second value, the function returns 1.

Parameters:

  • a: The first value to compare.
  • b: The second value to compare.

Returns:

  • int: -1 if a < b, 0 if a == b, 1 if a > b.
  • bool: True if the values are comparable.

Behaviors:

  • If the values are not comparable, the function returns false.

func CompareAny added in v0.2.39

func CompareAny(a, b any) (int, bool)

Compare compares two values of the same type that implement the Comparable interface. If the values are equal, the function returns 0. If the first value is less than the second value, the function returns -1. If the first value is greater than the second value, the function returns 1.

Parameters:

  • a: The first value to compare.
  • b: The second value to compare.

Returns:

  • int: -1 if a < b, 0 if a == b, 1 if a > b.
  • bool: True if the values are comparable.

Behaviors:

  • If the values are not comparable, the function returns false.

func CompareOf added in v0.2.39

func CompareOf(a, b any) (int, bool)

CompareOf compares two objects of the same type. If any of the objects implements the Comparer interface, the Compare method is called. Otherwise, the objects are compared using the < and == operators.

Parameters:

  • a: The first object to compare.
  • b: The second object to compare.

Returns:

  • int: The result of the comparison.

func CopyOf

func CopyOf(elem any) any

CopyOf creates a copy of the element by either calling the Copy method if the element implements the Copier interface or returning the element as is.

Parameters:

  • elem: The element to copy.

Returns:

  • any: A copy of the element.

func EqualOf

func EqualOf(a, b any) bool

EqualOf compares two objects of the same type. If any of the objects implements the Equaler interface, the Equals method is called. Otherwise, the objects are compared using the == operator. However, a is always checked first.

Parameters:

  • a: The first object to compare.
  • b: The second object to compare.

Returns:

  • bool: True if the objects are equal, false otherwise.

Behaviors:

  • Nil objects are always considered different.

func Find added in v0.3.2

func Find[T Comparer](S []T, elem T) (int, bool)

Find is a function that searches for an element in a slice and returns its index.

Parameters:

  • slice: The slice to search in.
  • elem: The element to search for.

Returns:

  • int: The index of the element in the slice. -1 if there is at least one non- comparable element.
  • bool: A flag indicating if the element was found.

Behaviors:

  • This function uses the Binary Search algorithm to find the element.
  • The slice must be sorted in ascending order.
  • If the element is not found, the index is the position where the element should be inserted to maintain the order of the slice.

func GetOrdinalSuffix added in v0.3.5

func GetOrdinalSuffix(number int) string

GetOrdinalSuffix returns the ordinal suffix for a given integer.

Parameters:

  • number: The integer for which to get the ordinal suffix.

Returns:

  • string: The ordinal suffix for the number.

For example, for the number 1, the function returns "1st"; for the number 2, it returns "2nd"; and so on.

func HashCode added in v0.3.13

func HashCode(elem any) int

HashCode returns the hash code of an element. If the element implements the Hashable interface, the Hash method is called. Otherwise, the hash code is calculated using the default hash function.

Parameters:

  • elem: The element to get the hash code from.

Returns:

  • int: The hash code of the element.

func IsComparable added in v0.2.43

func IsComparable(value any) bool

IsComparable returns true if the value is comparable with other values of the same type using the < and > operators or the Comparable interface.

Parameters:

  • value: The value to check.

Returns:

  • bool: True if the value is comparable, false otherwise.

func IsEmpty added in v0.2.43

func IsEmpty(elem any) bool

IsEmpty returns true if the element is empty.

Parameters:

  • elem: The element to check.

Returns:

  • bool: True if the element is empty, false otherwise.

func RunesOf added in v0.3.2

func RunesOf(str any) []rune

RunesOf returns the runes of a string. If the string implements the Runer interface, the Runes method is called. Otherwise, the string is converted to a slice of runes.

Parameters:

  • str: The string to get the runes from.

Returns:

  • []rune: The runes of the string.

func Sort added in v0.3.2

func Sort[T Comparer](S []T)

Sort is a function that sorts a slice of elements in ascending order.

Parameters:

  • slice: The slice to sort.

Behaviors:

  • This function uses the Quick Sort algorithm to sort the slice.
  • The elements in the slice must implement the Comparer interface.

func StableSort added in v0.3.13

func StableSort[T Comparer](S []T, isAsc bool) error

StableSort is a function that sorts a slice of elements in ascending order while preserving the order of equal elements.

Parameters:

  • slice: The slice to sort.
  • isAsc: A flag indicating if the sort is in ascending order.

Returns:

  • error: An error of type *ErrNotComparable if the elements are not comparable.

Behaviors:

  • This function uses the Merge Sort algorithm to sort the slice.
  • The elements in the slice must implement the Comparer interface.

func StringOf

func StringOf(elem any) string

StringOf converts any type to a string.

Parameters:

  • elem: The element to convert to a string.

Returns:

  • string: The string representation of the element.

Behaviors:

  • String elements are returned as is.
  • fmt.Stringer elements have their String method called.
  • error elements have their Error method called.
  • []byte and []rune elements are converted to strings.
  • Other elements are converted to strings using fmt.Sprintf and the %v format.

func TypeOf added in v0.2.43

func TypeOf(value any) string

TypeOf returns the type of the value as a string.

Parameters:

  • value: The value to get the type of.

Returns:

  • string: The type of the value.

Types

type Cleaner

type Cleaner interface {
	// Clean removes all the elements from the data structure.
	Clean()
}

Cleaner is an interface that provides a method to remove all the elements from a data structure.

type Comparable

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

Comparable is an interface that defines the behavior of a type that can be compared with other values of the same type using the < and > operators. The interface is implemented by the built-in types int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, and string.

type Comparer added in v0.2.39

type Comparer interface {
	// Compare returns a negative value if the object is less than the other object,
	// zero if they are equal, and a positive value if the object is greater
	// than the other object.
	//
	// Parameters:
	//   - other: The other object to compare to.
	//
	// Returns:
	//   - int: The result of the comparison.
	//   - bool: True if the objects are comparable, false otherwise.
	Compare(other Comparer) (int, bool)
}

Comparer is an interface that defines a method to compare two objects of the same type.

type Copier

type Copier interface {
	// Copy creates a copy of the element.
	//
	// Returns:
	//   - Copier: The copy of the element.
	Copy() Copier
}

Copier is an interface that provides a method to create a copy of an element.

type DoFunc

type DoFunc[T any] func(T)

DoFunc is a generic type that represents a function that takes a value and does something with it.

Parameters:

  • T: The type of the value.

type DualDoFunc

type DualDoFunc[T any, U any] func(T, U)

DualDoFunc is a generic type that represents a function that takes two values and does something with them.

Parameters:

  • T: The type of the first value.
  • U: The type of the second value.

type Equaler

type Equaler interface {
	// Equals returns true if the object is equal to the other object.
	//
	// Parameters:
	//   - other: The other object to compare to.
	//
	// Returns:
	//   - bool: True if the objects are equal, false otherwise.
	Equals(other Equaler) bool
}

Equaler is an interface that defines a method to compare two objects of the same type for equality.

type ErrNotComparable added in v0.3.13

type ErrNotComparable[A, B Comparer] struct {
	// First is the first value that is not comparable.
	First A

	// Second is the second value that is not comparable.
	Second B
}

ErrNotComparable is an error type that is returned when two values are not comparable.

func NewErrNotComparable added in v0.3.13

func NewErrNotComparable[A, B Comparer](first A, second B) *ErrNotComparable[A, B]

NewErrNotComparable creates a new ErrNotComparable error with the provided values.

Parameters:

  • first: The first value that is not comparable.
  • second: The second value that is not comparable.

Returns:

  • *ErrNotComparable: A pointer to the new error.

func (*ErrNotComparable[A, B]) Error added in v0.3.13

func (e *ErrNotComparable[A, B]) Error() string

Error returns the error message: "values <First> and <Second> are not comparable".

Returns:

  • string: The error message.

type EvalManyFunc added in v0.2.38

type EvalManyFunc[E, R any] func(elem E) ([]R, error)

EvalManyFunc is a function that evaluates many elements.

Parameters:

  • elem: The element to evaluate.

Returns:

  • []R: The results of the evaluation.
  • error: An error if the evaluation failed.

type EvalOneFunc added in v0.2.38

type EvalOneFunc[E, R any] func(elem E) (R, error)

EvalOneFunc is a function that evaluates one element.

Parameters:

  • elem: The element to evaluate.

Returns:

  • R: The result of the evaluation.
  • error: An error if the evaluation failed.

type Hashable added in v0.3.13

type Hashable interface {
	// Hash returns the hash code of the element.
	//
	// Returns:
	//   - int: The hash code of the element.
	Hash() int
}

Hashable is an interface that provides a method to get the hash code of an element.

type MainFunc added in v0.2.39

type MainFunc func() error

MainFunc is a function type that takes no parameters and returns an error. It is used to represent things such as the main function of a program.

Returns:

  • error: An error if the function failed.

type Objecter added in v0.3.2

type Objecter interface {
	fmt.Stringer
	Copier
	Equaler
}

Objecter is an interface that defines the behavior of an object that can be copied, compared, and converted to a string.

type RoutineFunc added in v0.2.39

type RoutineFunc func()

Routine is a function type used to represent a go routine.

type Runer added in v0.3.2

type Runer interface {
	// Runes returns the runes of the string.
	//
	// Returns:
	//   - []rune: The runes of the string.
	Runes() []rune
}

Runer is an interface that provides a method to get the runes of a string.

Jump to

Keyboard shortcuts

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