From 1f2a2a54022c02e4641ad7651a2b0fd7bcb59b38 Mon Sep 17 00:00:00 2001 From: rovdyl Date: Tue, 5 Feb 2019 21:55:40 -0600 Subject: [PATCH] simplify handling unsupported numbers Removes the usage of ++need, and therefore any possibility of accidentally crashing, by performing the check of unsupported numbers before anything else. Idea originally suggested by @philipcmonk https://github.com/urbit/arvo/pull/1053#issuecomment-460814835 --- lib/number-to-words.hoon | 72 +++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/lib/number-to-words.hoon b/lib/number-to-words.hoon index 1bb53dda70..52a0f6606e 100644 --- a/lib/number-to-words.hoon +++ b/lib/number-to-words.hoon @@ -36,27 +36,32 @@ |= num=@u ^- (unit tape) =+ numbers + ?: (gte num max) + ~ + :- ~ + |- + ^- tape :: 0-19 - ?: =(num 0) `"zero" - ?: =(num 1) `"one" - ?: =(num 2) `"two" - ?: =(num 3) `"three" - ?: =(num 4) `"four" - ?: =(num 5) `"five" - ?: =(num 6) `"six" - ?: =(num 7) `"seven" - ?: =(num 8) `"eight" - ?: =(num 9) `"nine" - ?: =(num 10) `"ten" - ?: =(num 11) `"eleven" - ?: =(num 12) `"twelve" - ?: =(num 13) `"thirteen" - ?: =(num 14) `"fourteen" - ?: =(num 15) `"fifteen" - ?: =(num 16) `"sixteen" - ?: =(num 17) `"seventeen" - ?: =(num 18) `"eighteen" - ?: =(num 19) `"nineteen" + ?: =(num 0) "zero" + ?: =(num 1) "one" + ?: =(num 2) "two" + ?: =(num 3) "three" + ?: =(num 4) "four" + ?: =(num 5) "five" + ?: =(num 6) "six" + ?: =(num 7) "seven" + ?: =(num 8) "eight" + ?: =(num 9) "nine" + ?: =(num 10) "ten" + ?: =(num 11) "eleven" + ?: =(num 12) "twelve" + ?: =(num 13) "thirteen" + ?: =(num 14) "fourteen" + ?: =(num 15) "fifteen" + ?: =(num 16) "sixteen" + ?: =(num 17) "seventeen" + ?: =(num 18) "eighteen" + ?: =(num 19) "nineteen" :: 20-99 :: :: tpl: tens place @@ -68,15 +73,15 @@ =/ sfx ?: |(=(rem 0) (gte tpl 10)) ~ - ['-' (need $(num rem))] - ?: =(tpl 2) `(weld "twenty" sfx) - ?: =(tpl 3) `(weld "thirty" sfx) - ?: =(tpl 4) `(weld "forty" sfx) - ?: =(tpl 5) `(weld "fifty" sfx) - ?: =(tpl 6) `(weld "sixty" sfx) - ?: =(tpl 7) `(weld "seventy" sfx) - ?: =(tpl 8) `(weld "eighty" sfx) - ?: =(tpl 9) `(weld "ninety" sfx) + ['-' $(num rem)] + ?: =(tpl 2) (weld "twenty" sfx) + ?: =(tpl 3) (weld "thirty" sfx) + ?: =(tpl 4) (weld "forty" sfx) + ?: =(tpl 5) (weld "fifty" sfx) + ?: =(tpl 6) (weld "sixty" sfx) + ?: =(tpl 7) (weld "seventy" sfx) + ?: =(tpl 8) (weld "eighty" sfx) + ?: =(tpl 9) (weld "ninety" sfx) :: 100-max :: :: num-break: repeated pattern from 100 on @@ -88,15 +93,14 @@ :: |= [min=@u str=tape] =/ rem (mod num min) - :- ~ ;: weld - (need ^$(num (div num min))) + ^$(num (div num min)) [' ' str] ?: =(rem 0) ~ %+ weld ?:((lth rem one-hundred) " and " ", ") - (need ^$(num rem)) + ^$(num rem) == :: ?: (lth num one-thousand) @@ -141,7 +145,5 @@ (num-break one-octodecillion "octodecillion") ?: (lth num one-vigintillion) (num-break one-novemdecillion "novemdecillion") - ?: (lth num max) - (num-break one-vigintillion "vigintillion") - ~ + (num-break one-vigintillion "vigintillion") --