From: Mart Lubbers Date: Thu, 11 Oct 2018 13:08:05 +0000 (+0200) Subject: updates X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=fe990e60c8a3ddf16f39295c854c295dab5e4d18;hp=-c;p=clean-tests.git updates --- fe990e60c8a3ddf16f39295c854c295dab5e4d18 diff --git a/afp/a4/skeleton4.icl b/afp/a4/skeleton4.icl index af496b7..8e4d8c6 100644 --- a/afp/a4/skeleton4.icl +++ b/afp/a4/skeleton4.icl @@ -73,7 +73,7 @@ gToString{|OBJECT|} fx (OBJECT x) = fx x instance + String where + s t = s +++ t -Start w = doTasks (changeName student) w +Start w = doTasks (changeNameEdcomb student) w enterStudent :: Task Student enterStudent = enterInformation "Enter a student" [] @@ -88,32 +88,34 @@ selectStudent :: ([Student] -> Task Student) selectStudent = enterChoice "Pick a student" [] selectStudentOnlyName :: ([Student] -> Task Student) -selectStudentOnlyName = enterChoice "Pick a student" [ChooseFromDropdown \s->s.Student.name] +selectStudentOnlyName = enterChoice "Pick a student" [ChooseFromDropdown \{Student|name}->name] selectStudentFormat :: ([Student] -> Task Student) selectStudentFormat = enterChoice "Pick a student" [ChooseFromDropdown gToString{|*|}] selectPartner :: ([Student] -> Task [Student]) -selectPartner = enterMultipleChoice "Pick a partner" [ChooseFromDropdown \s->s.Student.name + "(" + gToString{|*|} s.Student.bama + ")"] +selectPartner = enterMultipleChoice "Pick a partner" [ChooseFromDropdown \{name,bama}->name + "(" + gToString{|*|} bama + ")"] changeName :: Student -> Task Student changeName s = viewInformation "Student to change" [] s - ||- updateInformation "New name" [UpdateAs (\s->s.Student.name) (\s n->{Student | s & name=n})] s + ||- updateInformation "New name" [UpdateAs (\{Student|name}->name) (\s n->{Student | s & name=n})] s changeNameEdcomb :: Student -> Task Student changeNameEdcomb s - = updateInformation "New name" [UpdateUsing id (\_ v->v) studed] s + = updateInformation "New name" [UpdateUsing id (\_ v->v) nameEditor] s + >>= viewInformation "done" [] where - studed :: Editor Student - studed = bijectEditorValue - (\s->(s.Student.name, s.snum, s.bama, s.year)) + nameEditor :: Editor Student + nameEditor = bijectEditorValue + (\{name=n,snum=s,bama=b,year=y}->(n, s, b, y)) (\(n,s,b,y)->{name=n,snum=s,bama=b,year=y}) (container4 - gEditor{|*|} - (withChangedEditMode toView gEditor{|*|}) - (withChangedEditMode toView gEditor{|*|}) - (withChangedEditMode toView gEditor{|*|}) + (gEditor{|*|} <<@ labelAttr "name") + (withChangedEditMode toView gEditor{|*|} <<@ labelAttr "snum") + (withChangedEditMode toView gEditor{|*|} <<@ labelAttr "bama") + (withChangedEditMode toView gEditor{|*|} <<@ labelAttr "year") ) + toView (Update a) = View a toView v = v diff --git a/afp/a5/a5.icl b/afp/a5/a5.icl new file mode 100644 index 0000000..be81700 --- /dev/null +++ b/afp/a5/a5.icl @@ -0,0 +1,95 @@ +module a5 + +import Data.List +import Data.Func +import Text + +import iTasks + +derive class iTask Function, Question +:: Function = Student | Teacher | Admin +:: Question = + { question :: String + , answers :: [String] + , correct :: Int + } + +questions :: Shared [Question] +questions = sharedStore "questions" + [{question="Afp cool?" + ,answers=["yes", "no"] + ,correct=0} + ,{question="Mart the awesomest TA?" + ,answers=["yes", "no"] + ,correct=0} + ] + +Start w = doTasks login w + +login :: Task [Question] +login = enterInformation "Enter your function" [] + >>= \function->case function of + Teacher = teacher + Admin = admin + Student = student + +student :: Task [Question] +student = get questions + >>= \qs->sequence (map makeQuestion qs) @ distillResult + >>= viewInformation "Result" [] + >>* [OnAction (Action "Quit") (always login)] +where + makeQuestion :: Question -> Task Bool + makeQuestion q = enterChoice q.question [ChooseFromList snd] (zip2 [0..] q.answers) + @? \v->case v of + NoValue = NoValue + Value (idx, _) _ = Value (idx == q.correct) True + + distillResult :: [Bool] -> String + distillResult [] = "No questions answered!" + distillResult b = concat + [ "You had " + , toString good + , " answers correct and " + , toString bad + , " answers incorrect which results in a score of: " + , toString $ good*100 / (good+bad) + , "/100" + ] + where + good = length [()\\True<-b] + bad = length [()\\False<-b] + +admin :: Task [Question] +admin = updateSharedInformation "Questions" [] questions + +teacher :: Task [Question] +teacher = forever + $ enterChoiceWithShared "Choose an item to edit" [ChooseFromList id] questions + >>* + [ OnAction (Action "Append") (withValue $ Just o append) + , OnAction (Action "Delete") (withValue $ Just o delete) + , OnAction (Action "Edit") (withValue $ Just o edit) + , OnAction (Action "Clear") (withValue $ Just o clear) + , OnAction (Action "First") (always first) + , OnAction (Action "Quit") (always login) + ] +where + append choice = orCancel (enterInformation () []) \nq->upd (insertAfter choice nq) questions + delete choice = upd (deleteBy (===) choice) questions + edit choice = orCancel (updateInformation () [] choice) $ replace choice + clear choice = orCancel (enterInformation () []) $ replace choice + first = orCancel (enterInformation () []) \nq->upd (\x->[nq:x]) questions + + replace choice nq = upd (deleteBy (===) choice o insertAfter choice nq) questions + + insertAfter :: a a [a] -> [a] | gEq{|*|} a + insertAfter after el [] = [el] + insertAfter after el [e:es] + | e === after = [e,el:es] + = insertAfter after el es + + orCancel do done = do >>* + [ OnAction (Action "Cancel") (always teacher) + , OnAction (Action "Continue") (withValue (Just o done)) + ]