3.4. kink/COMPARE

Mod to provide following comparing methods.

3.4.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 or .le must be specified.

The result trait contains the following methods: op_lt, op_le, op_eq, op_ne, op_ge and op_gt.

Usage 1:

:COMPARE.require_from('kink/')

:Rat_trait <- [
  ... COMPARE.trait{(:C)
    C.lt{(:X :Y)
      X.numer * Y.denom < Y.numer * X.denom
    }
  }
  'numer' {[:R] R.Numer }
  'denom' {[:R] R.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

Usage 2:

:COMPARE.require_from('kink/')

:Month_trait <- [
  ... COMPARE.trait{(:C)
    C.by{(:M)
      M.num
    }
  }
  'num' {[:M] M.Num }
  'name' {[:M] M.Name }
]
:new_month <- {(:Num :Name)
  new_val(
    ... Month_trait
    'Num' Num
    'Name' Name
  )
}
[:January :Feburary :March] <- [
  new_month(1 'January')
  new_month(2 'Feburary')
  new_month(3 'March')
]
(January < Feburary).if_else(
  { stdout.print_line('January is less than Feburary') }
  { raise('???') }
)
# => January is less than Feburary

3.4.2. type compare_conf

The conf type of COMPARE.trait.

C.lt($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.

C.le($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.

C.eq($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.

C.ne($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.

C.by($attr)

Sets $attr to be used.

$attr must take an arg, and return the attribute of the arg. The attribute is used to test the order of the vals.

3.4.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.4.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