From 87bbc9326d13c5d93a17f826f5dbeb4b44ae89a3 Mon Sep 17 00:00:00 2001 From: Mack Hartley Date: Fri, 13 Apr 2018 20:02:25 -0500 Subject: [PATCH] Implemented homework changes to elm, ruby and swift --- .DS_Store | Bin 0 -> 6148 bytes elm-todo/Todo.elm | 43 ++------------------- ruby-todo/.DS_Store | Bin 0 -> 6148 bytes ruby-todo/lib/engine.rb | 3 +- ruby-todo/lib/messages.rb | 42 +++++++++++++------- ruby-todo/lib/model.rb | 4 +- ruby-todo/test/.DS_Store | Bin 0 -> 6148 bytes ruby-todo/test/todo_test.rb | 2 +- swift-todo/Sources/Todo/Engine.swift | 5 ++- swift-todo/Sources/Todo/Message.swift | 28 ++++++++------ swift-todo/Sources/Todo/Model.swift | 4 +- swift-todo/Tests/TodoTests/TodoTests.swift | 4 +- 12 files changed, 59 insertions(+), 76 deletions(-) create mode 100644 .DS_Store create mode 100644 ruby-todo/.DS_Store create mode 100644 ruby-todo/test/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..2dbfaac33d2a805f9cc4681958924d7cc4df7c34 GIT binary patch literal 6148 zcmeH~&2H2%5XZ;uLY*MQ9+0-jT#z^vv@1SVRfQCm?V$%GL=YSRwFy~hq%^KJ*+wNS z%7u3T-helOC*W~l#-5_In}r*o$|KqTi9O@Uza(}H0Ae(Y2LN3FkXT4NE>zk?v@mR8x&A{NdM)u$ znICfex9BiU@}l4WE>>1M7cQ^X}iLDBA?M&0CN;fvs7hou#fT_cnsTc zAI9(y^(G>WY3XaA`+`wTbPh~m1`!-DgDrZ0&S>EN`mb^Ks!G!y{+{+noE6$q+VnJ9 z*qq_*LJlP)@ETv#B`>S^fFm`Dx8|Hvf~|8s!;l^W8^8MU(n*6=OTZGa1egG=4;~g$ zpS3osmkzA-5dgJ=&DzkXUk1wYSbf&oDE6QUlZt9m1s^elNym1N<9ycIs7VLGhY!Jb z7JNbx`tCTsr^7+`Mr~~gSOTXBteVR%?f=`$&;O^B?3E>837jheqB9Q0Bb*ZNtqYT* vz1G9BhlPydYNI*@E8dQ^LtF7W7H!z)k%Q{9)<&@f&He~z47Rca{wjf=iv$!S literal 0 HcmV?d00001 diff --git a/elm-todo/Todo.elm b/elm-todo/Todo.elm index 3a07b24..ba6dd21 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,8 +74,6 @@ to them. -} type Msg = UpdateNewEntryField String - | EditingEntry Int Bool - | UpdateEntry Int String | Add | Delete Int | DeleteAllCompleted @@ -102,28 +98,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 - updateEntry t = - if t.id == id then - { t | description = task } - else - t - in - { model | entries = List.map updateEntry model.entries } Check id isCompleted -> let @@ -229,7 +203,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 @@ -240,7 +214,7 @@ viewEntry todo = ] [] , label - [ onDoubleClick (EditingEntry todo.id True) ] + [] [ text todo.description ] , button [ class "destroy" @@ -248,16 +222,6 @@ viewEntry todo = ] [] ] - , input - [ class "edit" - , value todo.description - , name "title" - , id ("todo-" ++ toString todo.id) - , onInput (UpdateEntry todo.id) - , onBlur (EditingEntry todo.id False) - , onEnter (EditingEntry todo.id False) - ] - [] ] @@ -313,8 +277,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/.DS_Store b/ruby-todo/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..2ca3fe38114d84a23fc0a17927bc9e36d3312775 GIT binary patch literal 6148 zcmeHK&2G~`5T0!ebpnVSkkX?sNE|AZh@YrJNLDC9y&;U?0I0PaL#?{rDt3rk6XXl; z0K5Tj0#Cq$@Br}5?h@sf-T=XlH2aNrzFEtky}Mo_61`!+L)0W97tZK3&@3^|voF}1 z>8S&S8Y7{U;+<6PN9h9Fc31_h0)LwVJa^aW0mXDgFKKH2$_N==7Vf~WeOHEv=;AD! zu4B$i+NVQ3?b>p9j8fNwA)|BTFp6ke%*B&ZsStp zMQ|WTK@k?iycZ6h@zukyIEYOASjuz~htZQH9_==-+>==mCRsdC#YsN~<@wVj>&sD3 z4zqr)#tC%7b?a_@x7jMoPJ7e4)$NQoy|UXyeDlWb@wo1`u6FM3>>Zt)mT$&y-?KPa z2(c0>yKL|$d_Z6{cpVI~RA#Tx!)%uxQAme)2TR|8cn8k(8*r!qH>YAw%WtcAQ)+(s z(`s&%<(86j>(_F!T(FgLenfI2#GIh2(l%!)V7^rM!dXO{w+dJV{yPPDeemFnzQIbP zS~}3EBLL7rw=%^1)4(;JLEm7d5gC}!r9fRO%n?KAa`bzK=Nqgv>T(k1@FC2dg*l-J zb$5*KDLM&XqiwAMR)JXsYWlLt`~Usb=l|Ivdu0`{3j9+F2xmLk?%|Ql-n#H`yw`g0 rJvbZlDvhcHjoFS>z+3SKTp4m62SDFor4cY5PhR50*a)x+|o+0wpl_fto;FzGlIpzg=qTg@>~3s`sTxs^AxNsbOvVL ze$3mwd&SN!0GV%BSHKLwl&*-KhOz0q`pAxAL{V&wC7$qz7Hh_znR3tY!u)5(L;ePr z*r2&>yN9Oj8s_OoUehn$%-8}>$?CA8FZZi_#e4n4GjfJhAQeajQh`+9A1c6_tu{Sz z%$N$K0;#}W0X-iIU9krCj&^h~==ELaCyHup+gpNIqF)1hN6yg1sYItrJTb)S950bq z1A9lOLu_ZR6FW=1P{elUc(HUyb>Z8HaibISBA~jYr2@a9z&qAx BD`Wrw literal 0 HcmV?d00001 diff --git a/ruby-todo/test/todo_test.rb b/ruby-todo/test/todo_test.rb index f7436c7..ddd4de3 100644 --- a/ruby-todo/test/todo_test.rb +++ b/ruby-todo/test/todo_test.rb @@ -91,7 +91,7 @@ end it "supports time travel" do - skip "Mutable model does not support time travel" + # skip "Mutable model does not support time travel" actual_history = Engine.run_with_history(Model.new, [ Msg::UpdateNewEntryField.new("go forward in time"), diff --git a/swift-todo/Sources/Todo/Engine.swift b/swift-todo/Sources/Todo/Engine.swift index e3b14a7..1b0f709 100644 --- a/swift-todo/Sources/Todo/Engine.swift +++ b/swift-todo/Sources/Todo/Engine.swift @@ -13,9 +13,10 @@ struct Engine { } static func runWithHistory(on model: Model, applying messages: [Message]) -> [Model] { + var curModel = model return messages.map { message in - message.apply(to: model) - return model + curModel = message.apply(to: curModel) + return curModel } } } diff --git a/swift-todo/Sources/Todo/Message.swift b/swift-todo/Sources/Todo/Message.swift index 92dd868..80d0a0e 100644 --- a/swift-todo/Sources/Todo/Message.swift +++ b/swift-todo/Sources/Todo/Message.swift @@ -14,30 +14,34 @@ enum Message { case delete(Int) case deleteAllCompleted - func apply(to model: Model) { + func apply(to model: Model) -> Model { + var newModel = model switch(self) { case .add: if !model.newEntryField.isBlank() { - model.entries.append(Entry(id: model.nextID, description: model.newEntryField)) + var newEntries = model.entries + newEntries.append(Entry(id: model.nextID, description: model.newEntryField)) + newModel.entries = newEntries } - model.nextID += 1 - model.newEntryField = "" - + newModel.nextID += 1 + newModel.newEntryField = "" + case .updateNewEntryField(let str): - model.newEntryField = str + newModel.newEntryField = str case .check(let id, let isCompleted): - for entry in model.entries { - if(entry.id == id) { - entry.completed = isCompleted + for i in newModel.entries.indices { + if(newModel.entries[i].id == id) { + newModel.entries[i].completed = isCompleted } } - + case .delete(let id): - model.entries.remove { $0.id == id } + newModel.entries.remove { $0.id == id } case .deleteAllCompleted: - model.entries.remove { $0.completed } + newModel.entries.remove { $0.completed } } + return newModel } } 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 diff --git a/swift-todo/Tests/TodoTests/TodoTests.swift b/swift-todo/Tests/TodoTests/TodoTests.swift index 9220517..7346f27 100644 --- a/swift-todo/Tests/TodoTests/TodoTests.swift +++ b/swift-todo/Tests/TodoTests/TodoTests.swift @@ -77,7 +77,7 @@ class TodoTests: XCTestCase { XCTAssertEqual([11], newModel.entries.map { $0.id }) } -/* + func testTimeTravel() { let actualHistory = Engine.runWithHistory(on: Model(), applying: [ .updateNewEntryField("go forward in time"), @@ -145,6 +145,6 @@ class TodoTests: XCTestCase { XCTAssertEqual(expected, actual, "History mismatch at step \(index)") } } -*/ + }