-
-
Save m-doughty/9eeca6552b9657fd2a5ff089d2ba1089 to your computer and use it in GitHub Desktop.
Recursively calculating bonuses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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