Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Phone number coding program in Ioke
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