Skip to content

Instantly share code, notes, and snippets.

@fogus
Last active February 9, 2021 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fogus/9153fc71a19112812d631a11fe88dc99 to your computer and use it in GitHub Desktop.
Save fogus/9153fc71a19112812d631a11fe88dc99 to your computer and use it in GitHub Desktop.
(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}
@richhickey
Copy link

richhickey commented Feb 5, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment