Created
June 15, 2014 13:34
-
-
Save edeustace/84010bc2869e387d08ad to your computer and use it in GitHub Desktop.
Use the first key in a map as the dispatch value for defmulti
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
; Example of using the first key in a map as the dispatch value for defmulti | |
(defmulti foo (fn [x] (first (map key x))) ) | |
(defmethod foo :a [x] ["a -> " x]) | |
(defmethod foo :b [x] ["b -> " x]) | |
(defmethod foo :default [x] ["default -> " x]) | |
(println (foo {:a "a"})) | |
(println (foo {:b "b"})) | |
(println (foo {:c "c"})) | |
(println (foo {:d "d"})) | |
; returns | |
;[a -> {:a a}] | |
;[b -> {:b b}] | |
;[default -> {:c c}] | |
;[default -> {:d d}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment