Last active
April 27, 2018 17:47
-
-
Save dawranliou/b08e6d3059253471050e98c80875ae8b to your computer and use it in GitHub Desktop.
Functional Programming in Scala 6.5 in Clojure
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
(def words | |
(clojure.string/split-lines (slurp "https://lamp.epfl.ch/files/content/sites/lamp/files/teaching/progfun/linuxwords.txt"))) | |
(def mnem | |
{\2 "ABC" \3 "DEF" \4 "GHI" \5 "JKL" \6 "MNO" \7 "PQRS" \8 "TUV" \9 "WXYZ"}) | |
(def char-code | |
(into {} (for [[k vs] mnem | |
v vs] | |
[v k]))) | |
(defn word-code | |
[word] | |
(apply str (map char-code (.toUpperCase word)))) | |
(def words-for-num | |
(group-by word-code words)) | |
(defn decode | |
[number] | |
(if (empty? number) | |
#{()} | |
(into #{} (for [split (range 1 (inc (.length number))) | |
word (get words-for-num (subs number 0 split)) | |
rest (decode (subs number split))] | |
(cons word rest))))) | |
(defn translate | |
[number] | |
(map (partial clojure.string/join " ") (decode number))) | |
(translate "5299626") | |
;; ("lazy Nan" "jazz man" "lazy Mao" "jazz Nan" "lazy man" "jazz Mao") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment