Created
September 19, 2012 08:09
-
-
Save fdb/3748329 to your computer and use it in GitHub Desktop.
Solving the expression problem in Clojure using maps.
This file contains 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
(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