From ab9aec910dc357e76bd6d5cab2391b465588a8ec Mon Sep 17 00:00:00 2001 From: angelicalin Date: Mon, 16 Apr 2018 21:53:47 -0500 Subject: [PATCH] homework done --- elm-todo/Todo.elm | 25 +++------------------ ruby-todo/lib/engine.rb | 4 ++-- ruby-todo/lib/messages.rb | 29 +++++++++++++++++-------- ruby-todo/lib/model.rb | 4 ++-- ruby-todo/test/todo_test.rb | 2 +- swift-todo/Sources/Todo/Engine.swift | 5 +++-- swift-todo/Sources/Todo/Message.swift | 31 ++++++++++++++++++--------- swift-todo/Sources/Todo/Model.swift | 4 ++-- 8 files changed, 54 insertions(+), 50 deletions(-) diff --git a/elm-todo/Todo.elm b/elm-todo/Todo.elm index 3a07b24..c50ea29 100755 --- a/elm-todo/Todo.elm +++ b/elm-todo/Todo.elm @@ -45,7 +45,6 @@ type alias Model = type alias Entry = { description : String , completed : Bool - , editing : Bool , id : Int } @@ -62,7 +61,6 @@ newEntry : String -> Int -> Entry newEntry desc id = { description = desc , completed = False - , editing = False , id = id } @@ -76,7 +74,6 @@ to them. -} type Msg = UpdateNewEntryField String - | EditingEntry Int Bool | UpdateEntry Int String | Add | Delete Int @@ -102,18 +99,6 @@ update msg model = UpdateNewEntryField str -> { model | newEntryField = str } - EditingEntry id isEditing -> - let - updateEntry t = - if t.id == id then - { t | editing = isEditing } - else - t - - focus = - Dom.focus ("todo-" ++ toString id) - in - { model | entries = List.map updateEntry model.entries } UpdateEntry id task -> let @@ -229,7 +214,7 @@ viewKeyedEntry todo = viewEntry : Entry -> Html Msg viewEntry todo = li - [ classList [ ( "completed", todo.completed ), ( "editing", todo.editing ) ] ] + [ classList [ ( "completed", todo.completed )] ] [ div [ class "view" ] [ input @@ -239,8 +224,7 @@ viewEntry todo = , onClick (Check todo.id (not todo.completed)) ] [] - , label - [ onDoubleClick (EditingEntry todo.id True) ] + , label[] [ text todo.description ] , button [ class "destroy" @@ -254,8 +238,6 @@ viewEntry todo = , name "title" , id ("todo-" ++ toString todo.id) , onInput (UpdateEntry todo.id) - , onBlur (EditingEntry todo.id False) - , onEnter (EditingEntry todo.id False) ] [] ] @@ -313,8 +295,7 @@ viewControlsClear entriesCompleted = infoFooter : Html msg infoFooter = footer [ class "info" ] - [ p [] [ text "Double-click to edit a todo" ] - , p [] + [ p [] [ text "Written by " , a [ href "https://github.com/evancz" ] [ text "Evan Czaplicki" ] ] diff --git a/ruby-todo/lib/engine.rb b/ruby-todo/lib/engine.rb index a257c20..4d990d9 100644 --- a/ruby-todo/lib/engine.rb +++ b/ruby-todo/lib/engine.rb @@ -5,8 +5,8 @@ def self.run(*args) def self.run_with_history(model, messages) messages.map do |msg| - msg.apply_to(model) - model + model = msg.apply_to(model) + end end end diff --git a/ruby-todo/lib/messages.rb b/ruby-todo/lib/messages.rb index abe85de..a8d21cc 100644 --- a/ruby-todo/lib/messages.rb +++ b/ruby-todo/lib/messages.rb @@ -2,13 +2,15 @@ module Msg class Add def apply_to(model) + list = model.entries.dup unless model.new_entry_field.blank? - model.entries << Entry.new( - description: model.new_entry_field, - id: model.next_id) + list< [Model] { + var newM = model return messages.map { message in - message.apply(to: model) - return model + newM = message.apply(to: newM) + return newM } } } diff --git a/swift-todo/Sources/Todo/Message.swift b/swift-todo/Sources/Todo/Message.swift index 92dd868..f45aabe 100644 --- a/swift-todo/Sources/Todo/Message.swift +++ b/swift-todo/Sources/Todo/Message.swift @@ -14,30 +14,41 @@ enum Message { case delete(Int) case deleteAllCompleted - func apply(to model: Model) { + func apply(to model: Model) ->Model{ switch(self) { case .add: + var list = model.entries if !model.newEntryField.isBlank() { - model.entries.append(Entry(id: model.nextID, description: model.newEntryField)) + list.append(Entry(id: model.nextID, description: model.newEntryField)) } - model.nextID += 1 - model.newEntryField = "" - + return Model(nextID: model.nextID+1, newEntryField: "", entries: list) + case .updateNewEntryField(let str): - model.newEntryField = str - + + return Model(nextID:model.nextID, newEntryField: str, entries: model.entries) + case .check(let id, let isCompleted): + var list = [Entry]() for entry in model.entries { if(entry.id == id) { - entry.completed = isCompleted + list.append(Entry(id: entry.id, description: entry.description, completed: isCompleted)) + } + else{ + list.append(Entry(id: entry.id, description: entry.description, completed: entry.completed)) } } + return Model(nextID:model.nextID, newEntryField: model.newEntryField, entries: list) case .delete(let id): - model.entries.remove { $0.id == id } + var list = model.entries + list.remove { $0.id == id } + return Model(nextID:model.nextID, newEntryField: model.newEntryField, entries: list) case .deleteAllCompleted: - model.entries.remove { $0.completed } + var list = model.entries + list.remove { $0.completed } + return Model(nextID:model.nextID, newEntryField: model.newEntryField, entries: list) + } } } diff --git a/swift-todo/Sources/Todo/Model.swift b/swift-todo/Sources/Todo/Model.swift index 1cca801..d50d863 100644 --- a/swift-todo/Sources/Todo/Model.swift +++ b/swift-todo/Sources/Todo/Model.swift @@ -1,4 +1,4 @@ -class Model { +struct Model { var entries: [Entry] var newEntryField: String var nextID: Int @@ -11,7 +11,7 @@ class Model { } } -class Entry { +struct Entry { var id: Int var description: String var completed: Bool