dmm: (dynosaur)
[personal profile] dmm
(defn rec-map-mult [scalar rec-map-of-ns]
  (if (map? rec-map-of-ns)
      (into {}
            (map #(vector (first %1)
                          (rec-map-mult scalar (second %1))
                  )
                 rec-map-of-ns
            )
      )
      (if (number? rec-map-of-ns)
          (* scalar rec-map-of-ns)
          0
      )
  )
)

user=> (rec-map-mult  3 { :a 3 :b {:c 7 :d 9} })
{:a 9, :b {:c 21, :d 27}}

a civilized version of the same function

Date: 2016-11-06 08:45 pm (UTC)
From: [identity profile] anhinga-travel.livejournal.com
(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}

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. 25th, 2025 11:06 am
Powered by Dreamwidth Studios