From 4355d5ae806ebbc0cdbef0b39f61330e8747df0c Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 29 Sep 2020 10:00:10 +0200 Subject: [PATCH] :tada: Add nil-free hashmap factory macros. --- common/app/common/data.cljc | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/common/app/common/data.cljc b/common/app/common/data.cljc index fe84e3f83..c0033c171 100644 --- a/common/app/common/data.cljc +++ b/common/app/common/data.cljc @@ -6,7 +6,7 @@ (ns app.common.data "Data manipulation and query helper functions." - (:refer-clojure :exclude [concat read-string]) + (:refer-clojure :exclude [concat read-string hash-map]) (:require [clojure.set :as set] [linked.set :as lks] #?(:cljs [cljs.reader :as r] @@ -115,6 +115,34 @@ [data] (into {} (remove (comp nil? second) data))) + +(defmacro without-nils + "A generic helper macro that removes nils from hash-map from + collection at compile time. If it is not possible (a symbol is + received), fallback to runtume nil removing procediment thanks to + `remove-nil-vals` function." + [param] + (cond + (symbol? param) + `(remove-nil-vals ~param) + + (map? param) + `~(remove-nil-vals param) + + :else + (throw (ex-info "Invalid arguments" + {:type :internal + :code :invalid-arguments})))) + +(defmacro hash-map + "A closure friendly macro for build nil-free hash-maps at compile + time." + [& kvpairs] + (let [data (->> (partition 2 kvpairs) + (remove (comp nil? second)) + (mapcat identity))] + `(hash-map ~@data))) + (defn without-keys "Return a map without the keys provided in the `keys` parameter."