Created
April 23, 2020 10:11
-
-
Save jackcallister/61aa413939d60ce12257dc652277ab6b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(def school (reduce (fn [acc g] (assoc acc g { :grade g :students [] })) {} (range 1 8))) | |
(defn add-student [name grade school] | |
(assoc-in | |
school [grade :students] | |
(vec (sort (conj (:students (get school grade)) name))))) | |
(defn add-students [school [name grade]] | |
(add-student name grade school)) | |
(defn students-by-grade [school] | |
(reduce | |
(fn [acc [k v]] | |
(if (= (count (:students v)) 0) acc | |
(conj acc v))) [] school)) | |
(add-student "Jack" 1 (add-student "Aimee" 2 (add-student "Jack" 2 school))) | |
; Same as ^ | |
(reduce add-students school {"Jack" 1 "James" 1 "Amy" 5}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment