@@ -1,10 +1,72 @@
1−module TodoBench where
1+module Realistic.Todo (benchmark) where
3import String
4import String
7−-- The full application state of our todo app.
7+benchmark : Benchmark.Benchmark
9+ Benchmark.suite "todomvc business logic"
10+ [ run "type in 5 todos, each 15 characters long" typingActions
11+ , run "do everything" batchActions
15+run : String -> List Action -> Benchmark.Benchmark
16+run description actions =
18+ (description ++ " - " ++ toString (List.length actions) ++ " actions")
19+ (\_ -> List.foldl update emptyModel actions)
25+ List.map (\n -> UpdateField (String.repeat n "X")) [1..15]
28+ List.concatMap enter [1..5]
34+ [ UpdateField ("My task " ++ toString i)
40+ , UpdateTask i "New Task text"
41+ , EditingTask i False
54+ [ List.concatMap addTask [1..50]
55+ , [CheckAll True, CheckAll False]
56+ , List.concatMap editTask (List.filter isEven [1 .. 50])
57+ , List.map check [5, 7, 9, 13, 17, 23, 37]
58+ , List.map uncheck [7, 13, 23, 37]
59+ , [DeleteComplete, Delete 7, Delete 23]
60+ , List.concatMap addTask [51..80]
61+ , List.map check (List.filter isEven [51..80])
62+ , [DeleteComplete, CheckAll True, DeleteComplete]
8type alias Model =
70type alias Model =
9 { tasks : List Task
71 { tasks : List Task
10 , field : String
72 , field : String
@@ -39,11 +101,10 @@ emptyModel =
44−-- A description of the kinds of actions that can be performed on the model of
105+-- UPDATE
45−-- our application. See the following post for more info on this pattern and
106+
46−-- some alternatives: http://elm-lang.org/learn/Architecture.elm
107+
47type Action
108type Action
49 | UpdateField String
110 | UpdateField String
@@ -57,7 +118,6 @@ type Action
57 | ChangeVisibility String
118 | ChangeVisibility String
60−-- How we update our Model on a given Action?
61update : Action -> Model -> Model
121update : Action -> Model -> Model
62update action model =
122update action model =
63 case action of
123 case action of
@@ -65,68 +125,43 @@ update action model =
67 { model |
127 { model |
68− uid <- model.uid + 1,
128+ uid = model.uid + 1,
69− field <- "",
129+ field = "",
71 if String.isEmpty model.field
131 if String.isEmpty model.field
72 then model.tasks
132 then model.tasks
73 else model.tasks ++ [newTask model.field model.uid]
133 else model.tasks ++ [newTask model.field model.uid]
76 UpdateField str ->
136 UpdateField str ->
77− { model | field <- str }
137+ { model | field = str }
79 EditingTask id isEditing ->
139 EditingTask id isEditing ->
80− let updateTask t = if t.id == id then { t | editing <- isEditing } else t
140+ let updateTask t = if t.id == id then { t | editing = isEditing } else t
82− { model | tasks <- List.map updateTask model.tasks }
142+ { model | tasks = List.map updateTask model.tasks }
84 UpdateTask id task ->
144 UpdateTask id task ->
85− let updateTask t = if t.id == id then { t | description <- task } else t
145+ let updateTask t = if t.id == id then { t | description = task } else t
87− { model | tasks <- List.map updateTask model.tasks }
147+ { model | tasks = List.map updateTask model.tasks }
89 Delete id ->
149 Delete id ->
90− { model | tasks <- List.filter (\t -> t.id /= id) model.tasks }
150+ { model | tasks = List.filter (\t -> t.id /= id) model.tasks }
92 DeleteComplete ->
152 DeleteComplete ->
93− { model | tasks <- List.filter (not << .completed) model.tasks }
153+ { model | tasks = List.filter (not << .completed) model.tasks }
95 Check id isCompleted ->
155 Check id isCompleted ->
96− let updateTask t = if t.id == id then { t | completed <- isCompleted } else t
156+ let updateTask t = if t.id == id then { t | completed = isCompleted } else t
98− { model | tasks <- List.map updateTask model.tasks }
158+ { model | tasks = List.map updateTask model.tasks }
100 CheckAll isCompleted ->
160 CheckAll isCompleted ->
101− let updateTask t = { t | completed <- isCompleted }
161+ let updateTask t = { t | completed = isCompleted }
103− { model | tasks <- List.map updateTask model.tasks }
163+ { model | tasks = List.map updateTask model.tasks }
105 ChangeVisibility visibility ->
165 ChangeVisibility visibility ->
106− { model | visibility <- visibility }
166+ { model | visibility = visibility }
110− [ List.concatMap --Add 50 tasks
111− (\i -> [UpdateField <| "My task " ++ toString i, Add]) [1 .. 50]
112− , [CheckAll True] --Check them all
113− , [CheckAll False] --Uncheck them all
114− , List.concatMap --Alter the text of even numbered tasks
115− (\i -> [EditingTask i True, UpdateTask i "New Task text", Add] ) <| List.filter (\x -> x % 2 == 0 ) [1 .. 50]
116− --Check and uncheck some more tasks
117− , [Check 5 True, Check 7 True, Check 9 True, Check 13 True, Check 17 True, Check 23 True, Check 37 True]
118− , [Check 7 False, Check 13 False, Check 23 False, Check 37 False]
119− , [DeleteComplete] --Delete our checked tasks
120− , [Delete 7, Delete 23] --Delete a few more tasks
121− , List.concatMap --Add a few more tasks
122− (\i -> [UpdateField <| "Second batch task " ++ toString i, Add]) [51 .. 80]
123− , List.map --Check off and delete every other task
124− (\i -> Check i True ) <| List.filter (\x -> x % 2 == 0 ) [1 .. 80]
126− , [CheckAll True, DeleteComplete] --Check and delete the remaining tasks
130−evalActions : List Action -> Model
131−evalActions actions =
132− List.foldr update emptyModel actions