Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am alexhall on github.
  • I am alexhall (https://keybase.io/alexhall) on keybase.
  • I have a public key ASAqRBJeuEw_1GUFIS4T-7cFTtGtTP_WvIWlksYN4zc7jwo

To claim this, I am signing this object:

@alexhall
alexhall / unnest.clj
Created June 17, 2013 19:38
Flattens a nested map structure.
(defn unnest-keys [x]
(into {} (mapcat (fn [[k v]]
(if (map? v)
(map (fn [[k' v']]
[(str (name k) "." k') v'])
(unnest-keys v))
[[(name k) v]]))
x)))
@alexhall
alexhall / gist:5286484
Created April 1, 2013 17:51
declaring dynamic functions
;; internal helper function
(defn default-foo [] "Hello, world!")
;; public API
(def ^:dynamic *foo* default-foo)
(defn bar [] (*foo*))
;; Usage
(bar)
(binding [*foo* #("Goodbye, world!")]
@alexhall
alexhall / gist:3899779
Created October 16, 2012 14:54
Multimethod prefer-method use
user> (defmulti m type)
#'user/m
user> (defmethod m Comparable [_] "comparable")
#<MultiFn clojure.lang.MultiFn@49c1e1e7>
user> (defmethod m Number [_] "number")
#<MultiFn clojure.lang.MultiFn@49c1e1e7>
user> (m "a")
"comparable"
user> (m 7)
; Evaluation aborted (exception complaining about conflicting matches for Number and Comparable)