From 82b08b492450114b037d3463daff90569c3a71cb Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Wed, 7 Oct 2015 10:10:15 +0200 Subject: [PATCH] a5 done:), and very nice too --- a5/mart/a5.icl | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/a5/mart/a5.icl b/a5/mart/a5.icl index 9c5d939..fb6930e 100644 --- a/a5/mart/a5.icl +++ b/a5/mart/a5.icl @@ -1,42 +1,51 @@ module a5 -/* - Mart Lubbers s4109503 - Charlie Gerhardus s3050009 -*/ +// Mart Lubbers s4109503 +// Charlie Gerhardus s3050009 import iTasks -import Text // To check string length -:: Idea = {idea :: String, details :: Maybe Note, user :: Name, number :: Int} :: ProtoIdea = {idea :: String, details :: Maybe Note} +:: Idea = {number :: Int, idea :: String, details :: Maybe Note, user :: Name, + likes :: Int} :: Name:== String -derive class iTask Idea, ProtoIdea // generic magic +derive class iTask Idea, ProtoIdea + +instance == Idea where + (==) x y = x.Idea.number == y.Idea.number ideas :: Shared [Idea] ideas = sharedStore "Ideas" [] storeIdea :: Name ProtoIdea -> Idea storeIdea n x=:{ProtoIdea | idea,details} = - {Idea | user=n, idea=idea, details=details, number=42} + {Idea | user=n, idea=idea, details=details, likes=0, number=0} addNewIdea :: Name -> Task [Idea] addNewIdea name = enterInformation "Add new idea" [EnterWith (storeIdea name)] >>* [OnAction ActionOk (hasValue (\v.return v))] - >>= \n.upd (\is.[n:is]) ideas - -viewList = (enterChoiceWithShared "Idea collection" [] ideas) - >&^ viewSharedInformation "Selected idea" [] + >>= \n.upd (\is.is ++ [{n & number=length is}]) ideas + +likeIdea :: Idea [Idea] -> [Idea] +likeIdea i x = let (ind, y) = removeIndex i x in + insertAt ind {i & likes=i.likes+1} y + +deleteIdea :: Idea [Idea] -> [Idea] +deleteIdea i x = let (ind, y) = removeIndex i x in + let (b, g) = splitAt ind y in b ++ (map (\z.{z & number=z.number-1}) g) + +viewList :: Name -> Task [Idea] +viewList name = (enterChoiceWithShared "Idea collection" [] ideas) + >&^ viewSharedInformation "Selected idea" [] + >>* [OnAction ActionOk (hasValue (\_.get ideas)), + OnAction (Action "Delete All" [ActionIcon "delete"]) + (always (upd (\_.[]) ideas)), + OnAction ActionDelete (hasValue (\v.upd (deleteIdea v) ideas)), + OnAction (Action "Like" []) + (ifValue (\x=:{user}.user <> name) (\v.upd (likeIdea v) ideas))] mainTask = enterInformation "What is your username?" [] - >>= \name.forever ( - (addNewIdea name -|| viewList) -// >>* [OnAction ActionOk (always (return (get ideas)))] -// OnAction (Action "Delete All" [ActionIcon "delete"]) (always (upd (\_.[]) ideas)) -//// OnAction (Action "Delete" [ActionIcon "delete"]) (always (return Void)), -//// OnAction (Action "Like" [ActionIcon "like"]) (always (return Void)) -// ] - ) + >>= \name.forever ((addNewIdea name -|| forever (viewList name))) Start :: *World -> *World Start world = startEngine mainTask world -- 2.20.1