by tekkk · 8y ago · 0 comments
const base = ["noa", "taha", "ua", "tolu", "fa", "nima", "ono", "fitu", "valu", "hiva"]
const tongan = n => Array.from(n.toString()).map(Number).map(i => base[i]).join(" ")
Not as pretty as I thought. Probably would look better in functional language. englishNumbers n = ten9 !! (n - 1)
where infixr 7 <<>>
(<<>>) = liftA2 (<>)
ten1 = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
ten2 = ten1 <> ["ten", "eleven", "twelve"] <> (["thir", "four"] <> prefixes) <<>> ["teen"] <> (["twen", "thir", "for"] <> prefixes) <<>> ["ty"] <<>> ([[]] <> ten1)
where prefixes = ["fif", "six", "seven", "eigh", "nine"]
ten3 = ten2 <> ten1 <<>> ["hundred"] <<>> ([[]] <> ten2)
ten6 = ten3 <> ten3 <<>> ["thousand"] <<>> ([[]] <> ten3)
ten9 = ten6 <> ten3 <<>> ["million"] <<>> ([[]] <> ten6)In Haskell, first line would be the same without the const and the other could be
tongan = intercalate " ".reverse.map ((base !!).(`mod` 10)).takeWhile (> 0).iterate (`div` 10)
(ISTM that there must be a better alternative to takeWhile + iterate).
val base = List("noa", "taha", "ua", "tolu", "fa", "nima", "ono", "fitu", "valu", "hiva")
def tongan(n: Int) = n.toString.map(_.asDigit).map(base).mkString(" ")