Last active
August 29, 2015 13:55
-
-
Save kntajus/8730359 to your computer and use it in GitHub Desktop.
Turns an integer into an array of strings that correspond to the correct wav file names to read out.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let emptyIfZero f x = | |
match x with | |
| 0 -> [] | |
| _ -> f x | |
let elementsBetween0And20 = emptyIfZero (fun x -> [ string x ]) | |
let elementsBetween21And99 x = string (x - (x % 10)) :: elementsBetween0And20 (x % 10) | |
let ifGreaterThan y f g x = if x > y then f x else g x | |
let elementsFor2DigitNumber = ifGreaterThan 20 elementsBetween21And99 elementsBetween0And20 | |
let elementsBetween100And999 x = string (x / 100) :: "hundred" :: emptyIfZero (fun x -> "and" :: elementsFor2DigitNumber x) (x % 100) | |
let elementsFor3DigitNumber = ifGreaterThan 99 elementsBetween100And999 elementsFor2DigitNumber | |
let generateSeparatorText n x y = | |
match n with | |
| "thousand" when y > 0 && y < 100 && x % 1000 > 0 -> [ n; "and" ] | |
| "thousand" when y > 0 && y < 100 -> [ "and" ] | |
| _ when x % 1000 = 0 -> [] | |
| _ -> [ n ] | |
let rec generateElementsRec l x = | |
match l with | |
| n::ns when x > 999 -> elementsFor3DigitNumber (x % 1000) :: generateSeparatorText n (x / 1000) (x % 1000) :: generateElementsRec ns (x / 1000) | |
| _ -> [ elementsFor3DigitNumber x ] | |
let generateElements x = | |
match x with | |
| 0 -> [ "0" ] | |
| _ when x < 0 -> raise <| System.ArgumentOutOfRangeException() | |
| _ -> | |
generateElementsRec [ "thousand"; "million"; "billion" ] x | |
|> List.rev | |
|> List.collect id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First 3 revisions of this were buggy! (Try 5000076 as input)