Skip to content

Instantly share code, notes, and snippets.

@cartermp
Last active August 29, 2015 14:07
Show Gist options
  • Save cartermp/3577286cbf43eb0da4de to your computer and use it in GitHub Desktop.
Save cartermp/3577286cbf43eb0da4de to your computer and use it in GitHub Desktop.
Beginning to learn Clojure
; Aliasing the string class to be more terse
(require '[clojure.string :as str])
; As a 'def', this is evaluated once
(def vowel? (set "AaEeOoIiUu"))
; Given a word, converts it into a pig latin equivalent
(defn convert-word [word]
(let [first-letter (first word)]
(if (vowel? first-letter)
(str word "ay")
(str (subs word 1) first-letter "ay"))))
; Converts a list of space-delimited words into pig latin.
(defn to-pig-latin [words]
(let [word-list (str/split words #" " )]
(map convert-word word-list)))
(def names (set `("Phillip" "Rikki" "Jim" "Emily" "Vee" "Bananas" "apple")))
(println "Names:")
(doall (map (fn [name] (print (str name " "))) names))
(println "\nEncoded as pig latin:")
(doall (map (fn [name] (print (str/join #" " (to-pig-latin name)) " ")) names))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment