Skip to content

Instantly share code, notes, and snippets.

@swannodette
Forked from theotherzach/name_that_nil.clj
Last active August 29, 2015 14:13
Show Gist options
  • Save swannodette/160d15a988b0a53a6eaf to your computer and use it in GitHub Desktop.
Save swannodette/160d15a988b0a53a6eaf to your computer and use it in GitHub Desktop.
(defn lookup-contract [m allowed]
(reify
clojure.lang.ILookup
(valAt [coll k]
(.valAt coll k nil))
(valAt [coll k not-found]
(if (contains? allowed m)
(.valAt coll k not-found)
(throw (IllegalArgumentException.
(str k " violates lookup contract, allowed " allowed)))))))
(defmacro cmap [& ks]
`(fn [m#]
(lookup-contract m# ~(set ks))))
(def scene (cmap :subject :action :object))
(def scenes [(scene
{:subject "Frankie"
:action "say"
:object "relax"})
(scene
{:subject "Lucy"
:action "?s"
:object "Clojure"})
(scene
{:subject "Rich"
:action "tries"
:object "a new conditioner"})])
(defn people-in-scenes [scenes]
(->> scenes
(map :bad-key)
(interpose ", ")
(reduce str)))
(comment
(println "People:" (people-in-scenes scenes))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment