-
-
Save swannodette/160d15a988b0a53a6eaf to your computer and use it in GitHub Desktop.
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
(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