Module Nat

Natural-number operations and iterators.

type Nat = Nat
dec Nat.Clamp : [Int, Nat, Nat] Nat

Nat.Clamp(x)(lo, hi) clamps x to the inclusive range [lo, hi].

dec Nat.FromString : [String] either {
  .err !,
  .ok Nat,
}

Parses a decimal string into a natural number. Returns .err! when the string is not a valid non-negative integer.

dec Nat.Max : [Nat, Int] Nat

Nat.Max(n, m) returns the larger of n and m. The result is always a Nat, even though m is an Int.

dec Nat.Min : [Nat, Nat] Nat

Returns the smaller of two natural numbers.

dec Nat.Mod : [Nat, Nat] Nat

Nat.Mod(m, n) is the remainder of dividing m by n. Returns 0 when n is 0.

dec Nat.Range : [Nat, Nat] List<Nat>

Nat.Range(lo, hi) produces the naturals from lo (inclusive) to hi (exclusive).

Nat.Range(0, 4)  // = *(0, 1, 2, 3)
dec Nat.Repeat : [Nat] recursive either {
  .end !,
  .step self,
}

Produces n repetitions of .step, followed by .end!.

Nat.Repeat(3).begin.case {
  .end! => "done",
  .step next => ... next.loop,
}
dec Nat.RepeatLazy : [Nat] recursive either {
  .end !,
  .step box choice {
    .next => self,
  },
}

Like Repeat, but each .step carries a boxed continuation behind .next. Use it when later steps might not be needed.