Revise grade-school to match up with the exercise description

This commit is contained in:
Erik Simmler 2016-03-16 21:16:54 -04:00
parent 3b6ba9da5a
commit 6b79102bac
2 changed files with 56 additions and 33 deletions

View file

@ -1,44 +1,31 @@
module GradeSchool (..) where module GradeSchool (..) where
import Dict exposing (..) import Dict exposing (..)
import Result exposing (toMaybe)
-- TODO: implement these classes type alias Grade =
-- type Grade = Int Int
-- type Student = String
-- type School = Dict Int List String
schoolFromList : List ( Int, String ) -> Dict Int (List String) type alias Student =
schoolFromList schoolList = String
List.foldl
(\tuple -> \dict -> addStudent (fst tuple) (snd tuple) dict)
newSchool
schoolList
schoolToList : Dict Int (List String) -> List ( Int, List String ) type alias School =
schoolToList school = Dict Int (List Student)
Dict.toList school
newSchool : Dict Int (List String) empty : School
newSchool = empty =
Dict.empty Dict.empty
addStudent : Int -> String -> Dict Int (List String) -> Dict Int (List String) addStudent : Grade -> Student -> School -> School
addStudent grade student school = addStudent grade student school =
Dict.insert grade (List.sort (student :: (studentsInGrade grade school))) school Dict.insert grade (List.sort (student :: (studentsInGrade grade school))) school
gradeWithStudents : Int -> List String -> Dict Int (List String) studentsInGrade : Grade -> School -> List Student
gradeWithStudents grade students =
Dict.singleton grade (List.sort students)
studentsInGrade : Int -> Dict Int (List String) -> List String
studentsInGrade grade school = studentsInGrade grade school =
case (Dict.get grade school) of case (Dict.get grade school) of
Just list -> Just list ->
@ -46,3 +33,8 @@ studentsInGrade grade school =
Nothing -> Nothing ->
[] []
allStudents : School -> List ( Grade, List Student )
allStudents school =
Dict.toList school |> List.sortBy fst

View file

@ -3,8 +3,7 @@ module Main (..) where
import Task import Task
import Console import Console
import ElmTest exposing (..) import ElmTest exposing (..)
import GradeSchool as S exposing (..) import GradeSchool exposing (addStudent, studentsInGrade, allStudents)
import Dict
tests : Test tests : Test
@ -14,30 +13,62 @@ tests =
[ test [ test
"add student" "add student"
(assertEqual (assertEqual
[ ( 2, [ "Aimee" ] ) ] [ "Aimee" ]
(S.schoolToList (S.addStudent 2 "Aimee" S.newSchool)) (GradeSchool.empty
|> addStudent 2 "Aimee"
|> studentsInGrade 2
)
) )
, test , test
"add more students in same class" "add more students in same class"
(assertEqual (assertEqual
[ ( 2, [ "Blair", "James", "Paul" ] ) ] [ "Blair", "James", "Paul" ]
(S.schoolToList (S.gradeWithStudents 2 [ "James", "Blair", "Paul" ])) (GradeSchool.empty
|> addStudent 2 "James"
|> addStudent 2 "Blair"
|> addStudent 2 "Paul"
|> studentsInGrade 2
)
) )
, test , test
"add students to different grades" "add students to different grades"
(assertEqual (assertEqual
[ ( 3, [ "Chelsea" ] ), ( 7, [ "Logan" ] ) ] [ [ "Chelsea" ], [ "Logan" ] ]
(S.schoolToList (S.schoolFromList [ ( 3, "Chelsea" ), ( 7, "Logan" ) ])) (let
school =
GradeSchool.empty
|> addStudent 3 "Chelsea"
|> addStudent 7 "Logan"
in
[ studentsInGrade 3 school, studentsInGrade 7 school ]
)
) )
, test , test
"get students in a grade" "get students in a grade"
(assertEqual (assertEqual
[ "Bradley", "Franklin" ] [ "Bradley", "Franklin" ]
(S.studentsInGrade 5 (S.schoolFromList [ ( 5, "Franklin" ), ( 5, "Bradley" ), ( 1, "Jeff" ) ])) (GradeSchool.empty
|> addStudent 5 "Franklin"
|> addStudent 5 "Bradley"
|> addStudent 1 "Jeff"
|> studentsInGrade 5
)
)
, test
"get all students in the school"
(assertEqual
[ ( 3, [ "Kyle" ] ), ( 4, [ "Christopher", "Jennifer" ] ), ( 6, [ "Kareem" ] ) ]
(GradeSchool.empty
|> addStudent 4 "Jennifer"
|> addStudent 6 "Kareem"
|> addStudent 4 "Christopher"
|> addStudent 3 "Kyle"
|> allStudents
)
) )
, test , test
"get students in a non-existent grade" "get students in a non-existent grade"
(assertEqual [] (S.studentsInGrade 1 S.newSchool)) (assertEqual [] (studentsInGrade 1 GradeSchool.empty))
] ]