Created
July 26, 2011 16:13
-
-
Save olabini/1107137 to your computer and use it in GitHub Desktop.
Phone number coding program in Ioke
This file contains 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
List splitAt = method(n, [self[0...n], self[n..-1]]) | |
PhoneNumbers = Origin mimic do( | |
words = FileSystem readFully("/usr/share/dict/words") split("\n") | |
mnemonics = { | |
2 => "ABC", | |
3 => "DEF", | |
4 => "GHI", | |
5 => "JKL", | |
6 => "MNO", | |
7 => "PQRS", | |
8 => "TUV", | |
9 => "WXYZ" | |
} | |
charCode = for:dict( | |
(digit, letters) <- mnemonics, | |
letter <- letters chars, | |
letter => digit | |
) | |
wordCode = method(word, | |
word upper chars map(c, charCode[c]) | |
) | |
wordsForNum = words select(length <= 7) groupBy(word, wordCode(word)) withDefault([]) | |
encode = method(number, | |
number = number chars map(toRational) | |
(1...(number length)) map(splitPoint, | |
(firstWords, lastWords) = number splitAt(splitPoint) map(w, wordsForNum[w]) | |
for:set( | |
firstWord <- firstWords, | |
lastWord <- lastWords, | |
"#{firstWord} #{lastWord}") || #{} | |
) reduce(+) + set(*wordsForNum[number]) | |
) | |
) | |
PhoneNumbers encode("6327537") inspect println |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment