6.18. kink/ORDER¶
Features of total order relations backed by `<=` operator, or `op_le` methods.
<= operator
Implementations of `<=` operator should be a total order which satisfies the following for all X, Y, and Z in the domain of the operands.
• reflexive: X <= X
• transitive: if X <= Y && Y <= Z then X <= Z
• antisymmetric: if X <= Y && Y <= X then X == Y
• strongly connected: X <= Y || Y <= X
6.18.1. ORDER.by($extract_key)¶
`by` returns a function which orders two args by the extracted keys, that is `{(:X :Y) extract_key(X) <= extract_key(Y) }`.
Precondition
$extract_key must be a function which takes one arg.
Example
:ORDER.require_from('kink/')
:TREE_SET.require_from('kink/container/')
:Set <- TREE_SET.new{(:C)
C.order(ORDER.by{(:Str) Str.ascii_downcase })
}
Set.push('foo')
Set.push('bar')
stdout.print_line(Set.have?('foo').repr) # => true
stdout.print_line(Set.have?('FOO').repr) # => true
stdout.print_line(Set.have?('hoge').repr) # => false
6.18.2. ORDER.min(X Y)¶
`min` returns X if X <= Y, otherwise Y.
Precondition
`<=` operator must be defined for `X` and `Y`.
Example
:ORDER.require_from('kink/')
stdout.print_line(ORDER.min(10 20).repr) # => 10
:Min <- [31 41 59 26 53].reduce(ORDER$min)
stdout.print_line(Min.repr) # => 26
6.18.3. ORDER.max(X Y)¶
`max` returns X if X >= Y, otherwise Y.
Precondition
`<=` operator must be defined for `X` and `Y`.
Example
:ORDER.require_from('kink/')
stdout.print_line(ORDER.max(10 20).repr) # => 20
:Max <- [31 41 59 26 53].reduce(ORDER$max)
stdout.print_line(Max.repr) # => 59