Source/Packages

Std.Natural

packages/foundation/standard/src/Std/Natural.alpha

376 lines51 declarations16.0 KiBSHA-256 7e7128c5e781

def · lines 342–372

naturalDecimalBuilder

Full file
The decimal digits of a natural, most significant first ("0" for zero): the text form of a counter, a sequence number or a tick in an AOS-JSON/1 stream (Observe.Value), and of a token cache path's ranges (Data.TokenCachePath). `fuel` is the caller's bound on the number of digits LESS ONE: a natural with more than `fuel + 1` digits keeps only its last `fuel + 1`, so every caller names a bound its values cannot exceed (a u64 has at most 20 digits).
342def naturalDecimalBuilder =
343  (lambda unrestricted fuel : Nat .
344    nat-eliminate
345      (lambda unrestricted current : Nat . pi unrestricted value : Nat . BytesBuilder)
346      (lambda unrestricted value : Nat .
347        then bytes-builder-chunk bytes nat-to-byte (naturalAdd 48) (naturalModuloUnchecked value) 10)
348      (lambda unrestricted predecessor : Nat .
349        unrestricted induction : (pi unrestricted value : Nat . BytesBuilder) .
350        unrestricted value : Nat .
351        let unrestricted quotient =
352          (naturalDivideUnchecked value 10)
353          in
354          nat-eliminate
355            (lambda unrestricted nonzero : Nat . BytesBuilder)
356            (then bytes-builder-chunk
357              bytes
358              nat-to-byte
359              (naturalAdd 48)
360              (naturalModuloUnchecked value)
361              10)
362            (lambda unrestricted quotientPredecessor : Nat .
363              unrestricted quotientInduction : BytesBuilder .
364              then (bytes-builder-append (induction quotient))
365                bytes-builder-chunk
366                bytes
367                nat-to-byte
368                (naturalAdd 48)
369                (naturalModuloUnchecked value)
370                10)
371            (naturalNonzero quotient))
372      fuel)

The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.