Ordered
type Ordered = Number | stringOrdered is the numbers and string, the built-in types that < and > compare.
Details
- A call sets
Tto one member, never to a union or toOrdereditself; narrow a union value withisfirst. - A named primitive type such as
type Meters = float64is not a member, though<works on two values of it.
Basic examples
Bound a type parameter by Ordered to compare its values:
func bigger[T: Ordered](a: T, b: T): T { if a > b { a } else { b } }
bigger(3, 8) // 8
bigger("pear", "fig") // "pear"
bigger(2.5, -1.0) // 2.5The prelude functions that compare take the same bound:
Math.max(4, 9) // 9
[3, 1, 2].sort() // [1, 2, 3]
["kiwi", "apple"].min() // "apple"Scope
Every sized integer and float type is a member, with both arguments of the one type:
func bigger[T: Ordered](a: T, b: T): T { if a > b { a } else { b } }
bigger(byte(3), byte(200)) // 200
bigger(int32(-7), int32(-2)) // -2
bigger(float32(1.5), float32(0.25)) // 1.5Inside the body, T also takes any other operator that every member takes, such as +, which adds numbers and concatenates strings:
func twice[T: Ordered](x: T): T { x + x }
twice(21) // 42
twice("ab") // "abab"Test a T against a member with is to treat strings apart. The narrowed value is a plain string, no longer a T, so keep a copy to return:
func orDefault[T: Ordered](x: T, fallback: T): T {
let original = x
if x is string {
if x == "" { fallback } else { original }
} else {
x
}
}
orDefault("", "none") // "none"
orDefault("ok", "none") // "ok"
orDefault(0, 5) // 0Ordered is also a type a value can have, a union like any other. Narrow it before you compare:
func describe(v: Ordered): string {
if v is string { "text ${v}" } else if v is int { "int ${v}" } else { "another number" }
}
describe("hi") // "text hi"
describe(7) // "int 7"
describe(0.5) // "another number"Applications
Find the largest element of any array whose elements compare, null when it is empty:
func largest[T: Ordered](xs: Array[T]): T | null {
var best: T | null = null
for x in xs {
if best == null || x > best {
best = x
}
}
best
}
largest([3, 9, 4]) // 9
largest(["pear", "apple"]) // "pear"
let none: Array[int] = []
largest(none) // nullArray.max does the same for an array.
Check that an array is sorted, for numbers and strings alike:
func isSorted[T: Ordered](xs: Array[T]): bool {
for i in Iter.range(1, len(xs)) {
if xs[i] < xs[i - 1] {
return false
}
}
true
}
isSorted([1, 2, 2, 5]) // true
isSorted(["b", "a"]) // false
isSorted([0.5, 1.0, 1.5]) // trueSort records by an Ordered key with sortBy, since a record itself is not ordered:
type Person = { name: string, age: int }
let people = [Person{ name: "Ann", age: 31 }, Person{ name: "Bob", age: 27 }]
people.sortBy({ x.age }).map({ x.name }) // ["Bob", "Ann"]
people.sortBy({ x.name }).map({ x.name }) // ["Ann", "Bob"]Properties and relations
A bounded function returns the caller's type, so the result needs no narrowing:
func bigger[T: Ordered](a: T, b: T): T { if a > b { a } else { b } }
let n: int = bigger(1, 2)
let s: string = bigger("a", "b")
assert(n == 2 && s == "b")Possible issues
A value of type Ordered is a union, so it cannot set T. Narrow it first:
func bigger[T: Ordered](a: T, b: T): T { if a > b { a } else { b } }
let a: Ordered = 1
let b: Ordered = 2
bigger(a, b) // error: T would be Ordered, which is not a single member of its bound Ordered; narrow firstComparing two Ordered values directly is a compile error too, since they may hold different kinds:
func less(a: Ordered, b: Ordered): bool {
a < b // error: < applies to a single numeric kind, string, or a named primitive type over one, not to Ordered; narrow first
}An array of mixed numbers and strings does not sort:
let xs: Array[int | string] = [1, "a"]
let sorted = xs.sort() // error: T would be int | string, which is not a single member of its bound Ordered; narrow firstBoth arguments need one type. An int and a float64 do not mix:
let m = Math.max(1, 2.0) // error: cannot use float64 where int is expectedbool is not ordered:
let m = Math.max(true, false) // error: T would be bool, which is not a member of its bound OrderedA named primitive type is not a member, though < works on two values of it. Compare the payloads:
type Meters = float64
let a = Meters(3.0)
let b = Meters(5.0)
let closer = Math.min(a, b) // error: T would be Meters, which is not a member of its bound Ordered; unwrap it with float64(...)Unwrap them with float64 to call Math.min, or compare them with < to keep the Meters type:
type Meters = float64
let a = Meters(3.0)
let b = Meters(5.0)
Math.min(float64(a), float64(b)) // 3.0
if a < b { a } else { b } // Meters(3.0)A NaN wins Math.min and Math.max, and sort puts it after every number:
Math.max(1.0, Math.NaN) // NaN
[2.0, Math.NaN, 1.0].sort() // [1.0, 2.0, NaN]See also
- Number and Integer: the numeric bounds.
- Math.min, Math.max, and Math.clamp
- Array.sort and Array.sortBy
- Array.min and Array.max
- Math.isNaN
