Skip to content

Instantly share code, notes, and snippets.

Created Sep 22, 2020
What would you like to do?
Recursively calculating bonuses
; A list of employees
(def employees-list [{:name "Tom" :salary 60000 :performance_multiplier 0.25}
{:name "Sarah" :salary 70000 :performance_multiplier 0.2}
{:name "Greg" :salary 45000 :performance_multiplier 0.4}])
; Let's calculate their bonuses!
(defn calculate-bonuses [employees]
(map (fn [e] {:name (:name e) :bonus (* (:salary e) (:performance_multiplier e))}) employees))
; (calculate-bonuses employees-list)
; => ({:name "Tom", :bonus 15000.0} {:name "Sarah", :bonus 14000.0} {:name "Greg", :bonus 18000.0})
; We've had a bad year, so anyone with a performance multiplier under 0.3 isn't getting a bonus
(defn high-performers [employees]
(filter (fn [e] (>= (:performance_multiplier e) 0.3)) employees))
; (high-performers employees-list)
; => ({:name "Greg", :salary 45000, :performance_multiplier 0.4})
; We need to know the total amount that will be paid out, for finance's sake.
(defn sum-of-bonuses [bonuses]
(reduce (fn [acc b] (+ acc (:bonus b))) 0 bonuses))
; (sum-of-bonuses (calculate-bonuses (high-performers employees-list)))
; => 18000.0
; (sum-of-bonuses (calculate-bonuses employees-list))
; => 47000.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment