Created
October 15, 2021 09:28
-
-
Save runejuhl/025121ecd459542859d04fc5e70daa2b to your computer and use it in GitHub Desktop.
scoring and summation
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
(defn scoring | |
[[s1 s2]] | |
(cond | |
(= s1 s2) [1 1] | |
(zero? s1) [3 0] | |
(pos? s1) [3 1] | |
:else [0 0])) | |
(def sum-points | |
(partial reduce (partial merge-with +))) | |
(defn calculate-scores | |
[scores] | |
(map (fn [{:keys [player1 player2 score]}] | |
(let [[s1 s2] score | |
sorted-score (sort score) | |
reverse? (not= score sorted-score) | |
points (cond-> (scoring sorted-score) | |
reverse? reverse)] | |
(apply hash-map (interleave [player1 player2] points)))) | |
scores)) | |
(let [scores [{:player1 "W" | |
:player2 "L" | |
:score [2 0]} | |
{:player1 "W" | |
:player2 "L" | |
:score [2 1]}]] | |
(calculate-scores scores) | |
;; => ({"L" 3, "W" 0} {"L" 3, "W" 1}) | |
(sum-points (calculate-scores scores)) | |
;; {"L" 6, "W" 1} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment