Skip to content

Instantly share code, notes, and snippets.

@fdb
Created September 19, 2012 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdb/3748329 to your computer and use it in GitHub Desktop.
Save fdb/3748329 to your computer and use it in GitHub Desktop.
Solving the expression problem in Clojure using maps.
(defn move-formatter [command]
(format "<MoveCommand %s,%s>" (:x command) (:y command)))
(defn stop-formatter [command]
"<StopCommand>")
(defn default-formatter [command]
(format "<Unknown command %s>" command))
(def formatters {
"move" move-formatter
"stop" stop-formatter})
(defn format-command [command]
(let [formatter (or
(formatters (:type command))
default-formatter)]
(formatter command)))
(defn print-command [command]
(println (format-command command)))
(def mc {:type "move" :x 10 :y 20})
(def sc {:type "stop"})
(def d (java.util.Date. 1348040520000))
(print-command mc)
(print-command sc)
(print-command d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment