Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ComparerFunc ¶
ComparerFunc defines a function type that compares two values of type T and returns their relative ordering. It is used throughout the enumerable library for sorting, ordering, and comparison operations.
Type Parameters:
T - the type of values to compare (can be any type)
The function should return:
-1 if x is less than y 0 if x is equal to y +1 if x is greater than y
Implementations should ensure the following mathematical properties:
- Consistency: f(x, y) should always return the same result for identical inputs
- Antisymmetry: if f(x, y) < 0 then f(y, x) > 0
- Transitivity: if f(x, y) < 0 and f(y, z) < 0 then f(x, z) < 0
- Equality: f(x, y) == 0 if and only if x and y are considered equal
Notes:
- For natural ordering of built-in types, use predefined comparer functions
- For custom ordering logic, create ComparerFunc instances
- Thread safety depends on the function implementation
- Nil handling should be consistent within the function
var ComparerBool ComparerFunc[bool] = func(a, b bool) int {
if !a && b {
return -1
} else if a && !b {
return 1
}
return 0
}
ComparerBool is a predefined ComparerFunc for comparing two bool values. It performs a logical comparison where false is considered less than true and returns:
-1 if the first boolean is false and the second is true 0 if both booleans are equal +1 if the first boolean is true and the second is false
This comparer adheres to the required mathematical properties of consistency, antisymmetry, transitivity, and equality as defined by the ComparerFunc type.
var ComparerByte ComparerFunc[byte] = func(a, b byte) int {
if a < b {
return -1
} else if a > b {
return 1
}
return 0
}
ComparerByte is a predefined ComparerFunc for comparing two byte values. It performs a numerical comparison between two unsigned 8-bit integers and returns:
-1 if the first byte is less than the second 0 if both bytes are equal +1 if the first byte is greater than the second
This comparer adheres to the required mathematical properties of consistency, antisymmetry, transitivity, and equality as defined by the ComparerFunc type.
var ComparerFloat32 ComparerFunc[float32] = func(a, b float32) int {
if a < b {
return -1
} else if a > b {
return 1
}
return 0
}
ComparerFloat32 is a predefined ComparerFunc for comparing two float32 values. It performs a numerical comparison between two float32 values and returns:
-1 if the first value is less than the second 0 if both values are equal +1 if the first value is greater than the second
This comparer adheres to the required mathematical properties of consistency, antisymmetry, transitivity, and equality as defined by the ComparerFunc type.
Note: This comparison does not handle NaN values specially - NaN comparisons follow Go's built-in comparison rules where NaN is not equal to anything, including itself.
var ComparerFloat64 ComparerFunc[float64] = func(a, b float64) int {
if a < b {
return -1
} else if a > b {
return 1
}
return 0
}
ComparerFloat64 is a predefined ComparerFunc for comparing two float64 values. It performs a numerical comparison between two float64 values and returns:
-1 if the first value is less than the second 0 if both values are equal +1 if the first value is greater than the second
This comparer adheres to the required mathematical properties of consistency, antisymmetry, transitivity, and equality as defined by the ComparerFunc type.
Note: This comparison does not handle NaN values specially - NaN comparisons follow Go's built-in comparison rules where NaN is not equal to anything, including itself.
var ComparerInt ComparerFunc[int] = func(a, b int) int {
if a < b {
return -1
} else if a > b {
return 1
}
return 0
}
ComparerInt is a predefined ComparerFunc for comparing two int values. It performs a natural numeric comparison between two integers and returns:
-1 if the first integer is less than the second 0 if both integers are equal +1 if the first integer is greater than the second
This comparer adheres to the required mathematical properties of consistency, antisymmetry, transitivity, and equality as defined by the ComparerFunc type.
var ComparerInt64 ComparerFunc[int64] = func(a, b int64) int {
if a < b {
return -1
} else if a > b {
return 1
}
return 0
}
ComparerInt64 is a predefined ComparerFunc for comparing two int64 values. It performs a natural numeric comparison between two int64 integers and returns:
-1 if the first integer is less than the second 0 if both integers are equal +1 if the first integer is greater than the second
This comparer adheres to the required mathematical properties of consistency, antisymmetry, transitivity, and equality as defined by the ComparerFunc type.
var ComparerRune ComparerFunc[rune] = func(a, b rune) int {
if a < b {
return -1
} else if a > b {
return 1
}
return 0
}
ComparerRune is a predefined ComparerFunc for comparing two rune values. It performs a numerical comparison between two Unicode code points and returns:
-1 if the first rune is less than the second 0 if both runes are equal +1 if the first rune is greater than the second
This comparer adheres to the required mathematical properties of consistency, antisymmetry, transitivity, and equality as defined by the ComparerFunc type.
var ComparerString ComparerFunc[string] = func(a, b string) int {
if a < b {
return -1
} else if a > b {
return 1
}
return 0
}
ComparerString is a predefined ComparerFunc for comparing two string values. It performs a lexicographic comparison between two strings and returns:
-1 if the first string is lexicographically less than the second 0 if both strings are equal +1 if the first string is lexicographically greater than the second
This comparer adheres to the required mathematical properties of consistency, antisymmetry, transitivity, and equality as defined by the ComparerFunc type.
var ComparerTime ComparerFunc[time.Time] = func(a, b time.Time) int {
if a.Before(b) {
return -1
} else if a.After(b) {
return 1
}
return 0
}
ComparerTime is a predefined ComparerFunc for comparing two time.Time values. It performs a chronological comparison between two time values and returns:
-1 if the first time is before the second 0 if both times are equal +1 if the first time is after the second
This comparer adheres to the required mathematical properties of consistency, antisymmetry, transitivity, and equality as defined by the ComparerFunc type.
Note: This comparison uses time.Time's built-in Before() and After() methods for accurate chronological ordering.