Skip to content
Open

HW 3 #15

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 6 additions & 28 deletions elm-todo/Todo.elm
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
port module Todo exposing (..)

{-| TodoMVC implemented in Elm, using plain HTML and CSS for rendering.

This application is broken up into three key parts:

1. Model - a full definition of the application's state
2. Update - a way to step the application state forward
3. View - a way to visualize our application state with HTML

This clean division of concerns is a core part of Elm. You can read more about
this in <http://guide.elm-lang.org/architecture/index.html>
-}

import Dom
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
Expand Down Expand Up @@ -45,7 +41,6 @@ type alias Model =
type alias Entry =
{ description : String
, completed : Bool
, editing : Bool
, id : Int
}

Expand All @@ -62,7 +57,6 @@ newEntry : String -> Int -> Entry
newEntry desc id =
{ description = desc
, completed = False
, editing = False
, id = id
}

Expand All @@ -76,7 +70,6 @@ to them.
-}
type Msg
= UpdateNewEntryField String
| EditingEntry Int Bool
| UpdateEntry Int String
| Add
| Delete Int
Expand All @@ -102,19 +95,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 =
Expand Down Expand Up @@ -229,7 +209,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
Expand All @@ -240,22 +220,21 @@ viewEntry todo =
]
[]
, label
[ onDoubleClick (EditingEntry todo.id True) ]
[]
[ text todo.description ]
, button
[ class "destroy"
, onClick (Delete todo.id)
]
[]
]
, input
,
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)
]
[]
]
Expand Down Expand Up @@ -313,13 +292,12 @@ 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" ]
]
, p []
[ text "Part of "
, a [ href "http://todomvc.com" ] [ text "TodoMVC" ]
]
]
]
5 changes: 3 additions & 2 deletions ruby-todo/lib/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ def self.run(*args)
end

def self.run_with_history(model, messages)
new_model = model
messages.map do |msg|
msg.apply_to(model)
model
new_model = msg.apply_to(new_model)
new_model
end
end
end
30 changes: 19 additions & 11 deletions ruby-todo/lib/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module Msg

class Add
def apply_to(model)
new_entries = model.entries.dup
unless model.new_entry_field.blank?
model.entries << Entry.new(
new_entries << Entry.new(
description: model.new_entry_field,
id: model.next_id)
end
model.next_id += 1
model.new_entry_field = ""
Model.new(entries: new_entries, new_entry_field: "", next_id: model.next_id + 1)
end
end

Expand All @@ -17,10 +17,10 @@ def initialize(str)
@str = str
end

attr_reader :str
attr_reader :str

def apply_to(model)
model.new_entry_field = str
Model.new(entries: model.entries, new_entry_field: str, next_id: model.next_id)
end
end

Expand All @@ -29,14 +29,18 @@ def initialize(id, is_completed)
@id, @is_completed = id, is_completed
end

attr_reader :id, :is_completed
attr_reader :id, :is_completed

def apply_to(model)
new_entries = []
model.entries.each do |entry|
if entry.id == id
entry.completed = is_completed
new_entries << Entry.new(id: entry.id, description: entry.description, completed: is_completed)
else
new_entries << Entry.new(id: entry.id, description: entry.description, completed: entry.completed)
end
end
Model.new(entries: new_entries, new_entry_field: model.new_entry_field, next_id: model.next_id)
end
end

Expand All @@ -45,17 +49,21 @@ def initialize(id)
@id = id
end

attr_reader :id
attr_reader :id

def apply_to(model)
model.entries.reject! { |e| e.id == id }
new_entries = model.entries.dup
new_entries.reject! { |e| e.id == id }
Model.new(entries: new_entries, new_entry_field: model.new_entry_field, next_id: model.next_id)
end
end

class DeleteAllCompleted
def apply_to(model)
model.entries.reject!(&:completed)
new_entries = model.entries.dup
new_entries.reject!(&:completed)
Model.new(entries: new_entries, new_entry_field: model.new_entry_field, next_id: model.next_id)
end
end

end
end
6 changes: 3 additions & 3 deletions ruby-todo/lib/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def initialize(entries: [], new_entry_field: "", next_id: nil)
(entries.lazy.map(&:id).max || -1) + 1
end

attr_accessor :entries, :new_entry_field, :next_id
attr_reader :entries, :new_entry_field, :next_id

def ==(other)
!other.nil? &&
Expand All @@ -20,12 +20,12 @@ def initialize(id:, description:, completed: false)
@description, @id, @completed = description, id, completed
end

attr_accessor :description, :completed, :id
attr_reader :description, :completed, :id

def ==(other)
!other.nil? &&
self.id == other.id &&
self.description == other.description &&
self.completed == other.completed
end
end
end
16 changes: 9 additions & 7 deletions ruby-todo/test/todo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -121,7 +121,7 @@
Entry.new(id: 0, description: "go forward in time", completed: false)
]),
Model.new(next_id: 3, new_entry_field: "", entries: [
Entry.new(id: 0, description: "go forward in time", completed: false),
Entry.new(id: 0, description: "go forward in time", completed: false),
Entry.new(id: 2, description: "delete this item", completed: false)
]),
Model.new(next_id: 3, new_entry_field: "", entries: [
Expand All @@ -134,19 +134,19 @@
Entry.new(id: 0, description: "go forward in time", completed: false)
]),
Model.new(next_id: 4, new_entry_field: "", entries: [
Entry.new(id: 0, description: "go forward in time", completed: false),
Entry.new(id: 0, description: "go forward in time", completed: false),
Entry.new(id: 3, description: "go backward in time", completed: false)
]),
Model.new(next_id: 4, new_entry_field: "", entries: [
Entry.new(id: 0, description: "go forward in time", completed: true),
Entry.new(id: 0, description: "go forward in time", completed: true),
Entry.new(id: 3, description: "go backward in time", completed: false)
]),
Model.new(next_id: 4, new_entry_field: "", entries: [
Entry.new(id: 0, description: "go forward in time", completed: true),
Entry.new(id: 0, description: "go forward in time", completed: true),
Entry.new(id: 3, description: "go backward in time", completed: true)
]),
Model.new(next_id: 4, new_entry_field: "", entries: [
Entry.new(id: 0, description: "go forward in time", completed: true),
Entry.new(id: 0, description: "go forward in time", completed: true),
Entry.new(id: 3, description: "go backward in time", completed: false)
]),
Model.new(next_id: 4, new_entry_field: "", entries: [
Expand All @@ -158,9 +158,11 @@
# but comparing one step at a time gives more readable error messages:

assert_equal expected_history.size, actual_history.size
assert_equal expected_history, actual_history

expected_history.zip(actual_history).each.with_index do |(expected, actual), index|
assert_equal expected, actual, "History mismatch at step #{index}"
end
end

end
end
8 changes: 4 additions & 4 deletions swift-todo/Sources/Todo/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//
// Created by Paul on 2018/4/4.
//

import Foundation

struct Engine {
Expand All @@ -13,9 +12,10 @@ struct Engine {
}

static func runWithHistory(on model: Model, applying messages: [Message]) -> [Model] {
var newModel = model
return messages.map { message in
message.apply(to: model)
return model
newModel = message.apply(to: newModel)
return newModel
}
}
}
}
31 changes: 19 additions & 12 deletions swift-todo/Sources/Todo/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//
// Created by Paul on 2018/4/4.
//

import Foundation

enum Message {
Expand All @@ -14,30 +13,38 @@ enum Message {
case delete(Int)
case deleteAllCompleted

func apply(to model: Model) {
switch(self) {
func apply(to model: Model) -> Model {
switch (self) {
case .add:
var newEntries = model.entries
if !model.newEntryField.isBlank() {
model.entries.append(Entry(id: model.nextID, description: model.newEntryField))
newEntries.append(Entry(id: model.nextID, description: model.newEntryField))
}
model.nextID += 1
model.newEntryField = ""
return Model(nextID: model.nextID + 1, entries: newEntries)

case .updateNewEntryField(let str):
model.newEntryField = str
return Model(nextID: model.nextID, newEntryField: str, entries: model.entries)

case .check(let id, let isCompleted):
var newEntries = [Entry]()
for entry in model.entries {
if(entry.id == id) {
entry.completed = isCompleted
if entry.id == id {
newEntries.append(Entry(id: entry.id, description: entry.description, completed: isCompleted))
} else {
newEntries.append(Entry(id: entry.id, description: entry.description, completed: entry.completed))
}
}
return Model(nextID: model.nextID, newEntryField: model.newEntryField, entries: newEntries)

case .delete(let id):
model.entries.remove { $0.id == id }
var newEntries = model.entries
newEntries.remove { $0.id == id }
return Model(nextID: model.nextID, newEntryField: model.newEntryField, entries: newEntries)

case .deleteAllCompleted:
model.entries.remove { $0.completed }
var newEntries = model.entries
newEntries.remove { $0.completed }
return Model(nextID: model.nextID, newEntryField: model.newEntryField, entries: newEntries)
}
}
}
}
6 changes: 3 additions & 3 deletions swift-todo/Sources/Todo/Model.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Model {
struct Model {
var entries: [Entry]
var newEntryField: String
var nextID: Int
Expand All @@ -11,7 +11,7 @@ class Model {
}
}

class Entry {
struct Entry {
var id: Int
var description: String
var completed: Bool
Expand All @@ -37,4 +37,4 @@ extension Entry: Equatable {
&& lhs.description == rhs.description
&& lhs.completed == rhs.completed
}
}
}
Loading