Avatar of paulfioravanti

paulfioravanti's solution

to Grade School in the Elm Track

Published at Jul 01 2019 · 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 first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

$ elm-test

Automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the test suite, 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 [ ( 3, [ "Kyle" ] ), ( 4, [ "Christopher", "Jennifer" ] ), ( 6, [ "Kareem" ] ) ]
                        (GradeSchool.empty
                            |> addStudent 4 "Jennifer"
                            |> addStudent 6 "Kareem"
                            |> addStudent 4 "Christopher"
                            |> addStudent 3 "Kyle"
                            |> 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 =
    let
        addName : Maybe (List Student) -> Maybe (List Student)
        addName names =
            case names of
                Just students ->
                    Just (student :: students)

                Nothing ->
                    Just [ student ]
    in
    school
        |> Dict.update grade addName


studentsInGrade : Grade -> School -> List Student
studentsInGrade grade school =
    let
        allStudentsInGrade =
            Dict.get grade school
    in
    case allStudentsInGrade of
        Just students ->
            List.sort students

        Nothing ->
            []


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

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?