Skip to content

Instantly share code, notes, and snippets.

@edeustace
Created June 15, 2014 13:34
Show Gist options
  • Save edeustace/84010bc2869e387d08ad to your computer and use it in GitHub Desktop.
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
; 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