plumatic / plumbing

Prismatic's Clojure(Script) utility belt

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

(map/flatten {:foo false}) -> '()

weissjeffm opened this issue · comments

This function ignores the value false, which I don't think it should. I haven't looked at it carefully but I don't understand what the (when m ...) inside flatten-helper is for. Seems to work ok without it (but again there may be cornercases I haven't tried).

This impl seems to work ok:

(defn flatten
  "Transform a nested map into a seq of [keyseq leaf-val] pairs"
  [m]
  (when m
    ((fn flatten-helper [keyseq m]
       (if (map? m)
         (mapcat (fn [[k v]] (flatten-helper (conj keyseq k) v)) m)
         [[keyseq m]]))
     [] m)))

Thanks for the report.

Yeah, I'm not sure why it's that way either. I guess the intention was maybe to remove nil. In that case we could do (when (not (nil? m)) instead of (when m. But I don't have a strong opinion about which is the right semantics.

PR welcome!