Skip to content

Instantly share code, notes, and snippets.

@jackcallister
Created April 23, 2020 10:11
Show Gist options
  • Save jackcallister/61aa413939d60ce12257dc652277ab6b to your computer and use it in GitHub Desktop.
Save jackcallister/61aa413939d60ce12257dc652277ab6b to your computer and use it in GitHub Desktop.
(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