🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of epequeno

epequeno's solution

to Grade School in the Elm Track

Published at Apr 19 2021 · 0 comments
Instructions
Test suite
Solution

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:

  • Add a student's name to the roster for a grade
    • "Add Jim to grade 2."
    • "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?"
    • "Grade 1: Anna, Barb, and Charlie. Grade 2: Alex, Peter, and Zoe. Grade 3…"

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?

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The code you have to write is located inside the src/ directory of the exercise. Elm automatically installs packages dependencies the first time you run the tests so we can start by running the tests from the exercise directory with:

$ elm-test

To automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the tests suite in the file tests/Tests.elm, be sure to remove the skip <| calls from each test until you get them all passing!

Source

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

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (tests)

import Expect
import GradeSchool exposing (addStudent, allStudents, studentsInGrade)
import Test exposing (..)


tests : Test
tests =
    describe "GradeSchool"
        [ test "add student" <|
            \() ->
                Expect.equal [ "Aimee" ]
                    (GradeSchool.empty
                        |> addStudent 2 "Aimee"
                        |> studentsInGrade 2
                    )
        , skip <|
            test "add more students in same class" <|
                \() ->
                    Expect.equal [ "Blair", "James", "Paul" ]
                        (GradeSchool.empty
                            |> addStudent 2 "James"
                            |> addStudent 2 "Blair"
                            |> addStudent 2 "Paul"
                            |> studentsInGrade 2
                        )
        , skip <|
            test "add students to different grades" <|
                \() ->
                    Expect.equal [ [ "Chelsea" ], [ "Logan" ] ]
                        (let
                            school =
                                GradeSchool.empty
                                    |> addStudent 3 "Chelsea"
                                    |> addStudent 7 "Logan"
                         in
                         [ studentsInGrade 3 school, studentsInGrade 7 school ]
                        )
        , skip <|
            test "get students in a grade" <|
                \() ->
                    Expect.equal [ "Bradley", "Franklin" ]
                        (GradeSchool.empty
                            |> addStudent 5 "Franklin"
                            |> addStudent 5 "Bradley"
                            |> addStudent 1 "Jeff"
                            |> studentsInGrade 5
                        )
        , skip <|
            test "get all students in the school" <|
                \() ->
                    Expect.equal [ ( 1, [ "Anna", "Barb", "Charlie" ] ), ( 2, [ "Alex", "Peter", "Zoe" ] ), ( 3, [ "Jim" ] ) ]
                        (GradeSchool.empty
                            |> addStudent 2 "Peter"
                            |> addStudent 1 "Anna"
                            |> addStudent 1 "Barb"
                            |> addStudent 2 "Zoe"
                            |> addStudent 2 "Alex"
                            |> addStudent 3 "Jim"
                            |> addStudent 1 "Charlie"
                            |> allStudents
                        )
        , skip <|
            test "get students in a non-existent grade" <|
                \() -> Expect.equal [] (studentsInGrade 1 GradeSchool.empty)
        ]
module GradeSchool exposing (addStudent, allStudents, empty, studentsInGrade)

import Dict exposing (Dict)


type alias Grade =
    Int


type alias Student =
    String


type alias School =
    Dict Grade (List Student)


empty : School
empty =
    Dict.empty


addStudent : Grade -> Student -> School -> School
addStudent grade student school =
    Dict.insert grade
        (student
            :: studentsInGrade grade school
            |> List.sort
        )
        school


studentsInGrade : Grade -> School -> List Student
studentsInGrade grade school =
    Dict.get grade school
        |> Maybe.withDefault []


allStudents : School -> List ( Grade, List Student )
allStudents school =
    school
        |> Dict.toList

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?
  • Are there new concepts here that you could read more about to improve your understanding?