Skip to content

Instantly share code, notes, and snippets.

@xixixao

xixixao/Switch

Created Apr 3, 2013
Embed
What would you like to do?
Clojure vs Coffee
#(defn parting
# "returns a String parting in a given language"
# ([] (parting "World"))
# ([name] (parting name "en"))
# ([name language]
# (condp = language
# "en" (str "Goodbye, " name)
# "es" (str "Adios, " name)
# (throw (IllegalArgumentException.
# (str "unsupported language " language))))))
#
#(println (parting)) ; -> Goodbye, World
#(println (parting "Mark")) ; -> Goodbye, Mark
#(println (parting "Mark" "es")) ; -> Adios, Mark
#(println (parting "Mark", "xy")) ; -> java.lang.IllegalArgumentException: unsupported language xy
parting = (name = "World", language = "en") ->
switch language
when "en" then "Goodbye, #{name}"
when "es" then "Adios, #{name}"
else throw "unsupported language #{language}"
log parting()
log parting "Mark"
log parting "Mark", "es"
log parting "Mark", "xy"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment