diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/lux/data/number.lux | 66 |
1 files changed, 31 insertions, 35 deletions
diff --git a/stdlib/source/lux/data/number.lux b/stdlib/source/lux/data/number.lux index e9009102b..06a8809e1 100644 --- a/stdlib/source/lux/data/number.lux +++ b/stdlib/source/lux/data/number.lux @@ -172,8 +172,8 @@ ) ## [Values & Syntax] -(def: (get-char full idx) - (-> Text Nat (Maybe Text)) +(def: (get-char idx full) + (-> Nat Text Text) ("lux text clip" full idx (n.inc idx))) (do-template [<struct> <base> <char-set> <error>] @@ -181,7 +181,7 @@ (def: (encode value) (loop [input value output ""] - (let [digit (maybe;assume (get-char <char-set> (n.% <base> input))) + (let [digit (get-char (n.% <base> input) <char-set>) output' ("lux text concat" digit output) input' (n./ <base> input)] (if (n.= +0 input') @@ -197,7 +197,7 @@ (loop [idx +1 output +0] (if (n.< input-size idx) - (let [digit (maybe;assume (get-char input idx))] + (let [digit (get-char idx input)] (case ("lux text index" <char-set> digit +0) #;None (#E;Error ("lux text concat" <error> repr)) @@ -226,20 +226,19 @@ "-" "")] (loop [input (|> value (i./ <base>) (:: Number<Int> abs)) - output (|> value (i.% <base>) (:: Number<Int> abs) int-to-nat - (get-char <char-set>) - maybe;assume)] + output (get-char (|> value (i.% <base>) (:: Number<Int> abs) int-to-nat) + <char-set>)] (if (i.= 0 input) ("lux text concat" sign output) - (let [digit (maybe;assume (get-char <char-set> (int-to-nat (i.% <base> input))))] + (let [digit (get-char (int-to-nat (i.% <base> input)) <char-set>)] (recur (i./ <base> input) ("lux text concat" digit output)))))))) (def: (decode repr) (let [input-size ("lux text size" repr)] (if (n.>= +1 input-size) - (let [sign (case (get-char repr +0) - (^ (#;Some "-")) + (let [sign (case (get-char +0 repr) + "-" -1 _ @@ -248,7 +247,7 @@ (loop [idx (if (i.= -1 sign) +1 +0) output 0] (if (n.< input-size idx) - (let [digit (maybe;assume (get-char input idx))] + (let [digit (get-char idx input)] (case ("lux text index" <char-set> digit +0) #;None (#E;Error <error>) @@ -267,7 +266,7 @@ (def: (de-prefix input) (-> Text Text) - (maybe;assume ("lux text clip" input +1 ("lux text size" input)))) + ("lux text clip" input +1 ("lux text size" input))) (do-template [<struct> <nat> <char-bit-size> <error>] [(struct: #export <struct> (Codec Text Deg) @@ -315,8 +314,8 @@ (if (f.= 0.0 dec-left) ("lux text concat" "." output) (let [shifted (f.* <base> dec-left) - digit (|> shifted (f.% <base>) frac-to-int int-to-nat - (get-char <char-set>) maybe;assume)] + digit (get-char (|> shifted (f.% <base>) frac-to-int int-to-nat) + <char-set>)] (recur (f.% 1.0 shifted) ("lux text concat" output digit))))))] ("lux text concat" whole-part decimal-part))) @@ -324,8 +323,8 @@ (def: (decode repr) (case ("lux text index" repr "." +0) (#;Some split-index) - (let [whole-part (maybe;assume ("lux text clip" repr +0 split-index)) - decimal-part (maybe;assume ("lux text clip" repr (n.inc split-index) ("lux text size" repr)))] + (let [whole-part ("lux text clip" repr +0 split-index) + decimal-part ("lux text clip" repr (n.inc split-index) ("lux text size" repr))] (case [(:: <int> decode whole-part) (:: <int> decode decimal-part)] (^multi [(#;Some whole) (#;Some decimal)] @@ -369,8 +368,8 @@ (if (n.<= chunk-size num-digits) (list digits) (let [boundary (n.- chunk-size num-digits) - chunk (maybe;assume ("lux text clip" digits boundary num-digits)) - remaining (maybe;assume ("lux text clip" digits +0 boundary))] + chunk ("lux text clip" digits boundary num-digits) + remaining ("lux text clip" digits +0 boundary)] (list& chunk (segment-digits chunk-size remaining))))))) (def: (bin-segment-to-hex input) @@ -499,10 +498,10 @@ (let [sign (:: Number<Frac> signum value) raw-bin (:: Binary@Codec<Text,Frac> encode value) dot-idx (maybe;assume ("lux text index" raw-bin "." +0)) - whole-part (maybe;assume ("lux text clip" raw-bin - (if (f.= -1.0 sign) +1 +0) - dot-idx)) - decimal-part (maybe;assume ("lux text clip" raw-bin (n.inc dot-idx) ("lux text size" raw-bin))) + whole-part ("lux text clip" raw-bin + (if (f.= -1.0 sign) +1 +0) + dot-idx) + decimal-part ("lux text clip" raw-bin (n.inc dot-idx) ("lux text size" raw-bin)) hex-output (|> (<from> false decimal-part) ("lux text concat" ".") ("lux text concat" (<from> true whole-part)) @@ -518,8 +517,8 @@ 1.0)] (case ("lux text index" repr "." +0) (#;Some split-index) - (let [whole-part (maybe;assume ("lux text clip" repr (if (f.= -1.0 sign) +1 +0) split-index)) - decimal-part (maybe;assume ("lux text clip" repr (n.inc split-index) ("lux text size" repr))) + (let [whole-part ("lux text clip" repr (if (f.= -1.0 sign) +1 +0) split-index) + decimal-part ("lux text clip" repr (n.inc split-index) ("lux text size" repr)) as-binary (|> (<to> decimal-part) ("lux text concat" ".") ("lux text concat" (<to> whole-part)) @@ -672,14 +671,13 @@ (loop [idx +0 output (make-digits [])] (if (n.< length idx) - (let [char (maybe;assume (get-char input idx))] - (case ("lux text index" "0123456789" char +0) - #;None - #;None - - (#;Some digit) - (recur (n.inc idx) - (digits-put idx digit output)))) + (case ("lux text index" "0123456789" (get-char idx input) +0) + #;None + #;None + + (#;Some digit) + (recur (n.inc idx) + (digits-put idx digit output))) (#;Some output))) #;None))) @@ -743,9 +741,7 @@ false)] (if (and dotted? (n.<= (n.inc bit;width) length)) - (case (|> ("lux text clip" input +1 length) - maybe;assume - text-to-digits) + (case (text-to-digits ("lux text clip" input +1 length)) (#;Some digits) (loop [digits digits idx +0 |