# 4d47's solution

## to Grade School in the Clojure Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given students' names along with the grade that they are in, create a roster for the school.

In the end, you should be able to:

• "OK."
• Get a list of all students enrolled in a grade
• "Which students are in grade 2?"
• "We've only got Jim just now."
• Get a sorted list of all students in all grades. Grades should sort as 1, 2, 3, etc., and students within a grade should be sorted alphabetically by name.
• "Who all is enrolled in school right now?"

Note that all our students only have one name. (It's a small town, what do you want?)

## For bonus points

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

• If you're working in a language with mutable data structures and your implementation allows outside code to mutate the school's internal DB directly, see if you can prevent this. Feel free to introduce additional tests.

Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?

## Source

A pairing session with Phil Battos at gSchool http://gschool.it

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

``````(ns grade-school-test
(:require [clojure.test :refer [deftest is]]

(def db {})

(is (= {2 ["James", "Blair", "Paul"]}
(-> db

(is (= {3 ["Chelsea"] 7 ["Logan"]}
(-> db

(-> db

(is (= (sorted-map 3 ["Kyle"]
4 ["Christopher" "Jennifer"]
6 ["Kareem"] )
(-> db

(is (= [3 4 6]
(-> db
(keys)))))``````
``````(ns grade-school)

(get db level []))

(assoc db level (conj (grade db level) name)))

(defn sorted [db]
(into (sorted-map) (for [[k v] db] [k (sort v)])))``````

Nice one liner in sorted. I knew there had to be a really simple and elegant way to do that, but I didn't manage to figure it out for myself, sorted-map vs. sorted-by vs. sorted-map-by confused the heck out of me.

I also managed to miss the fact that you can pass a default value to get, so that's good to know!

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?