Skip to content

Instantly share code, notes, and snippets.

@shaunparker
Last active Feb 10, 2019
Embed
What would you like to do?
Tagged literal to reduce namespaced keyword verbosity

An example of using a custom tagged literal to reduce the verbosity of namespaced keywords in code.

clj -m example

Running from the gist:

clojure -Sdeps '{:deps {keyword-tagged-literal-ex {:git/url "https://gist.github.com/shaunparker/c89ac63cba98d05d9586a837654b1c1e" :sha "bb8513e88698c0cf12108b6df19a96679661c3cc"}}}' -m example
(ns custom-readers
(:require
[clojure.set :as set]
[clojure.walk :as walk]))
(def key->namespaced-key
"Defines the mapping of non-namespaced keywords to namespaced-keywords for documents/entities."
{:entities :document/entities
:extracted :entity/extracted
:tags :entity/tags})
(defn docize-map
[m]
(set/rename-keys m key->namespaced-key))
(defn read-doc
[m]
(walk/postwalk #(cond-> %
(map? %) docize-map)
m))
(comment
(= #doc/ent {:entities {:extracted []
:tags []}}
{:document/entities {:entity/extracted []
:entity/tags []}}))
{doc/ent custom-readers/read-doc}
{:paths [""]
:deps {org.clojure/clojure {:mvn/version "1.10.0"}}}
(ns example
(:require [custom-readers]))
(defn -main
[& args]
(println
"#doc/ent {:entities {:extracted [] :tags []}} = "
#doc/ent {:entities {:extracted [] :tags []}}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment