Last active
February 9, 2021 14:47
-
-
Save fogus/9153fc71a19112812d631a11fe88dc99 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
(comment | |
;; original emit on RHS of map destructure form | |
(if (seq? kvs) | |
(clojure.lang.PersistentHashMap/create (seq kvs)) | |
kvs) | |
) | |
(defn create-bl [& kvs] | |
;; replacement code for RHS of map destructure form | |
(if (seq? kvs) | |
(if (next kvs) | |
(if (odd? (count kvs)) | |
(conj (clojure.lang.PersistentHashMap/create (butlast kvs)) (last kvs)) | |
(clojure.lang.PersistentHashMap/create (seq kvs))) | |
(first kvs)) | |
kvs)) | |
(defn create-pt [& kvs] | |
;; replacement code for RHS of map destructure form | |
(if (seq? kvs) | |
(->> kvs (partition-all 2) (#'clojure.core/spread) (map #(if (seq? %) (vec %) %)) (reduce conj {})) | |
kvs)) | |
(defn create-as [& kvs] | |
;; replacement code for RHS of map destructure form | |
(if (seq? kvs) | |
(persistent! (reduce (fn [acc kv] (if (next kv) (assoc! acc (first kv) (second kv)) (conj! acc (first kv)))) | |
(transient {}) | |
(partition-all 2 kvs))) | |
kvs)) | |
(defn create-tr [& kvs] | |
;; replacement code for RHS of map destructure form | |
(if (seq? kvs) | |
(loop [m (transient {}) init kvs] | |
(if (next init) | |
(recur (assoc! m (first init) (second init)) | |
(-> init next next)) | |
(persistent! (conj! m (first init))))) | |
kvs)) | |
(create-tr :a 1 :b 2 {:c 3}) | |
;;=> {:a 1, :b 2, :c 3} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
create-bl - butlast might not be fast but that case might be very uncommon if "seq containing just a map" was separate branch
create-as - what if (second kv) is false?