Skip to content

Instantly share code, notes, and snippets.

Last active Feb 10, 2019
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 "" :sha "bb8513e88698c0cf12108b6df19a96679661c3cc"}}}' -m example
(ns custom-readers
[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
(set/rename-keys m key->namespaced-key))
(defn read-doc
(walk/postwalk #(cond-> %
(map? %) docize-map)
(= #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]
"#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