mirror of
https://github.com/correl/elm.git
synced 2025-03-30 17:00:06 -09:00
Add GradeSchool example and test
This commit is contained in:
parent
40a69d61d0
commit
4b5ff7afb3
3 changed files with 64 additions and 1 deletions
31
GradeSchool/GradeSchoolExample.elm
Normal file
31
GradeSchool/GradeSchoolExample.elm
Normal file
|
@ -0,0 +1,31 @@
|
|||
module GradeSchoolExample where
|
||||
|
||||
import Dict exposing (..)
|
||||
import Result exposing (toMaybe)
|
||||
-- TODO: implement these classes
|
||||
-- type Grade = Int
|
||||
-- type Student = String
|
||||
-- type School = Dict Int List String
|
||||
|
||||
schoolFromList : List (Int, String) -> Dict Int (List String)
|
||||
schoolFromList schoolList = List.foldl (\tuple -> \dict -> addStudent (fst tuple) (snd tuple) dict)
|
||||
newSchool schoolList
|
||||
|
||||
schoolToList : Dict Int (List String) -> List (Int, List String)
|
||||
schoolToList school = Dict.toList school
|
||||
|
||||
newSchool : Dict Int (List String)
|
||||
newSchool = Dict.empty
|
||||
|
||||
addStudent : Int -> String -> Dict Int (List String) -> Dict Int (List String)
|
||||
addStudent grade student school =
|
||||
Dict.insert grade (List.sort (student :: (studentsInGrade grade school))) school
|
||||
|
||||
gradeWithStudents : Int -> List String -> Dict Int (List String)
|
||||
gradeWithStudents grade students = Dict.singleton grade (List.sort students)
|
||||
|
||||
studentsInGrade : Int -> Dict Int (List String) -> List String
|
||||
studentsInGrade grade school =
|
||||
case (Dict.get grade school) of
|
||||
Just list -> list
|
||||
Nothing -> []
|
31
GradeSchool/GradeSchoolTest.elm
Normal file
31
GradeSchool/GradeSchoolTest.elm
Normal file
|
@ -0,0 +1,31 @@
|
|||
module GradeSchoolTest where
|
||||
|
||||
-- TODO - remove example inclusion once Problem sets are ready to go live or CI is set up.
|
||||
|
||||
-- import GradeSchoolExample exposing (addStudent,
|
||||
-- newSchool, gradeWithStudents, schoolFromList,
|
||||
-- studentsInGrade, schoolToList)
|
||||
|
||||
import GradeSchoolExample as S exposing (..)
|
||||
|
||||
import ElmTest.Test exposing (test, Test, suite)
|
||||
import ElmTest.Assertion exposing (assert, assertEqual)
|
||||
import ElmTest.Runner.Element exposing (runDisplay)
|
||||
|
||||
import Dict
|
||||
|
||||
tests : Test
|
||||
tests = suite "GradeSchool Test Suite"
|
||||
[
|
||||
test "add student" (assertEqual [(2, ["Aimee"])]
|
||||
(S.schoolToList (S.addStudent 2 "Aimee" S.newSchool))),
|
||||
test "add more students in same class" (assertEqual [(2, ["Blair", "James", "Paul"])]
|
||||
(S.schoolToList (S.gradeWithStudents 2 ["James", "Blair", "Paul"]))),
|
||||
test "add students to different grades" (assertEqual [(3, ["Chelsea"]), (7, ["Logan"])]
|
||||
(S.schoolToList (S.schoolFromList [(3, "Chelsea"), (7, "Logan")]))),
|
||||
test "get students in a grade" (assertEqual ["Bradley", "Franklin"]
|
||||
(S.studentsInGrade 5 (S.schoolFromList [(5, "Franklin"), (5, "Bradley"), (1, "Jeff")]))),
|
||||
test "get students in a non-existent grade" (assertEqual [] (S.studentsInGrade 1 S.newSchool))
|
||||
]
|
||||
|
||||
main = runDisplay tests
|
|
@ -27,7 +27,8 @@
|
|||
"SpaceAge",
|
||||
"Anagram",
|
||||
"NucleotideCount",
|
||||
"PhoneNumber"
|
||||
"PhoneNumber",
|
||||
"GradeSchool"
|
||||
],
|
||||
"exposed-modules": [],
|
||||
"dependencies": {
|
||||
|
|
Loading…
Add table
Reference in a new issue