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:
Note that all our students only have one name. (It's a small town, what do you want?)
Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:
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?
Refer to the Installing Elm page for information about installing elm.
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:
To automatically run tests again when you save changes:
$ elm-test --watch
As you work your way through the tests suite in the file
be sure to remove the
calls from each test until you get them all passing!
A pairing session with Phil Battos at gSchool http://gschool.it
It is possible to submit an incomplete solution so you can see how others have completed the exercise.
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
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.