kex docs Standard Library

Algebra

Algebraic structures.

Kex traits do not inherit from one another, so concrete types explicitly implement every structure whose laws they satisfy.

type Ordering

The result of Comparable.compare. Declared here rather than only inside the interpreter so that Ordering, Less, Equal and Greater reach the semantic layer the same way every other stdlib type does — through the collected interfaces — instead of existing solely as native environment bindings the type checker and name resolver cannot see.

Variants

  • Less
  • Equal
  • Greater

Implements Monoid.

Methods

Ordering is a Monoid under "first decision wins", with Equal as identity. That is what makes multi-key comparison compose instead of nesting ifs:

a.name.compare(b.name).combine(a.age.compare(b.age))

combine evaluates its argument eagerly, so the later comparison runs even when the earlier one already decided. Use thenBy when that matters.

combine Monoid

combine(_, other: Ordering) -> Ordering

reverse

reverse : Ordering

Swaps Less and Greater and fixes Equal — descending order.

thenBy

thenBy : Block<Ordering> -> Ordering

Short-circuiting combine: the tie-breaker block runs only when this comparison is Equal, so it costs nothing once the order is decided.

a.name.compare(b.name).thenBy { a.age.compare(b.age) }

From Monoid

repeat
Combines this value with itself n times.

trait Comparable

Implemented by Number.

Required methods

An implementor writes these.

compare

compare : This -> Ordering

A total order: compare answers Less, Equal or Greater. == stays independent — a type may be Equatable without being ordered.

extends Number

More methods of Number, added by this module.

Implements Comparable.

Number carries the implementation, so Integer and Float both inherit it rather than repeating the same three comparisons. Mixed receivers work because < and > promote across the two (1.compare(1.0) is Equal).

compare Comparable

compare(other: Number) -> Ordering

trait Monoid

Implemented by Integer, String, [A], Ordering, Map<K, V>, Set<A>, UnorderedSet<A>.

Required methods

An implementor writes these.

identity

identity : This

combine must be associative and identity neutral on both sides.

combine

combine : This -> This

Provided methods

Every implementor gets these, written in terms of the required methods.

repeat

repeat(n: Integer) -> This

Combines this value with itself n times. Repeating zero times returns the monoid identity; a negative repetition count is invalid.

trait Group

Implemented by Integer.

Required methods

An implementor writes these.

identity

identity : This

Combining a value with its inverse must produce identity.

combine

combine : This -> This

inverse

inverse : This

extends Integer

More methods of Integer, added by this module.

Implements Monoid, Group.

combine Monoid Group

combine(other: This) -> This

inverse Group

inverse : This

extends String

More methods of String, added by this module.

Implements Monoid.

combine Monoid

combine(other: This) -> This

extends [A]

More methods of List, added by this module.

Implements Monoid.

combine Monoid

combine(other: This) -> This