3.3. kink/COMPARE¶
Mod to provide following comparing methods.
3.3.1. COMPARE.trait($config)¶
Makes a trait of comparing methods.
$config must take a compare_conf. For the compare_conf, at least one of lt_fun or le_fun must be specified.
The result trait contains the following methods: op_lt, op_le, op_eq, op_ne, op_ge and op_gt.
Usage:
:Rat_trait <- [
... COMPARE.trait{(:Conf)
Conf.lt_fun{(:X :Y)
X.numer * Y.denom < Y.numer * X.denom
}
}
'numer' { \recv.Numer }
'denom' { \recv.Denom }
]
:rat <- {(:Numer :Denom)
new_val(
... Rat_trait
'Numer' Numer
'Denom' Denom
)
}
(rat(3 5) > rat(1 2)).if_else(
{ stdout.print_line('3/5 is bigger than 1/2') }
{ raise('???') }
)
# => 3/5 is bigger than 1/2
3.3.2. type compare_conf¶
The conf type of COMPARE.trait.
Compare_conf.lt_fun($lt?)¶
Sets $lt? to be used.
$lt? must take two args, and return true if the first arg is less than the second, otherwise return false.
Compare_conf.le_fun($le?)¶
Sets $le? to be used.
$le? must take two args, and return true if the first arg is less than or equal to the second, otherwise return false.
Compare_conf.eq_fun($eq?)¶
Sets $eq? to be used.
$eq? must take two args, and return true if the first arg is equal to the second, otherwise return false.
Compare_conf.ne_fun($ne?)¶
Sets $ne? to be used.
$ne? must take two args, and return true if the first arg is not equal to the second, otherwise return false.
3.3.3. COMPARE.min(X Y)¶
Returns X if X <= Y, otherwise Y.
Example:
stdout.print_line(COMPARE.min(10 20).repr) # => 10
:Min <- [31 41 59 26 53].reduce(COMPARE$min).just
stdout.print_line(Min.repr) # => 26
3.3.4. COMPARE.max(X Y)¶
Returns X if X >= Y, otherwise Y.
Example:
stdout.print_line(COMPARE.max(10 20).repr) # => 20
:Min <- [31 41 59 26 53].reduce(COMPARE$max).just
stdout.print_line(Min.repr) # => 59