a civilized version of the same function

Date: 2016-11-06 08:45 pm (UTC)
(defn rec-map-mult [n M]
  (reduce (fn [M [k v]]
            (let [new-v
                   (cond
                     (map? v) (rec-map-mult n v)
                     (number? v) (* n v)
                     :else 0)]
               (if (or (= new-v {}) (and (number? new-v) (zero? new-v)))
                   M
                   (assoc M k new-v))))
          {} M))
#'user/rec-map-mult

user=> (rec-map-mult  3 { :a 3 :b {:c 7 :d 9} })
{:b {:d 27, :c 21}, :a 9}
This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

Profile

dmm: (Default)
Dataflow matrix machines (by Anhinga anhinga)

May 2025

S M T W T F S
    123
456 78910
11 121314151617
18192021222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 30th, 2025 09:10 pm
Powered by Dreamwidth Studios