A
Size: a a a
A
AP
A
A
DL
A
(defn distribute [amount distribution]
(loop [total-amount amount
keys-left (keys distribution)
total-weight (reduce + (vals distribution))]
(if (= total-weight 0)
total-amount
(let [key (first keys-left)
weight (get distribution key)
amount (/ (* total-amount weight) total-weight)
used (key amount)]
(recur (- total-amount used)
(rest keys-left)
(- total-weight weight))))))
A
A
A
DL
(defn distribute [amount distribution]
(loop [total-amount amount
keys-left (keys distribution)
total-weight (reduce + (vals distribution))]
(if (= total-weight 0)
total-amount
(let [key (first keys-left)
weight (get distribution key)
amount (/ (* total-amount weight) total-weight)
used (key amount)]
(recur (- total-amount used)
(rest keys-left)
(- total-weight weight))))))
PG
DL
[current-key keys-left] (keys distribution)
A
A
A
(defn distribute [amount distribution]
(first
(reduce-kv
(fn [[total-amount total-weight] key weight]
(if (= total-weight 0)
[total-amount 0]
(let [amount (/ (* total-amount weight) total-weight)
used (key amount)]
[(- total-amount used) (- total-weight weight)])))
[amount (reduce + (vals distribution))]
distribution)))
A
A
DL