Welcome! Please see the About page for a little more info on how this works.

0 votes
ago in Collections by

I know that https://ask.clojure.org/index.php/1913/use-transients-with-select-keys-if-possible exists to optimize select-keys for many keys, but I noticed that it does a lot of work even if the original map is nil. Would there be interest in a patch that returns an empty map when given nil? Something like:

(defn select-keys*
  "Returns a map containing only those entries in map whose key is in keys"
  {:added "1.0"
   :static true}
  [map keyseq]
  (if map
    (loop [ret {} keys (seq keyseq)]
      (if keys
        (let [entry (. clojure.lang.RT (find map (first keys)))]
          (recur
           (if entry
             (conj ret entry)
             ret)
           (next keys)))
        (with-meta ret (meta map))))
    {}))
ago by
I wonder how often it is called with nil vs an empty map?

Feels to me like this is an edge case that -- if the application code is a hot path when passing nil -- the application code should have the guard on nil.

Otherwise, every user and every call now has to pay the tax of that extra if in all cases.
ago by
It's no more a tax than any other (when foo ...) call wrapping the body of a nil-punning function.

Please log in or register to answer this question.

...