Skip to content
Open
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
25 changes: 3 additions & 22 deletions elm-todo/Todo.elm
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type alias Model =
type alias Entry =
{ description : String
, completed : Bool
, editing : Bool
, id : Int
}

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

Expand All @@ -76,7 +74,6 @@ to them.
-}
type Msg
= UpdateNewEntryField String
| EditingEntry Int Bool
| UpdateEntry Int String
| Add
| Delete Int
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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)
]
[]
]
Expand Down Expand Up @@ -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" ]
]
Expand Down
4 changes: 2 additions & 2 deletions ruby-todo/lib/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 20 additions & 9 deletions ruby-todo/lib/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<<Entry.new(
description: model.new_entry_field, id: model.next_id)
end
model.next_id += 1
model.new_entry_field = ""
#model.next_id += 1
# model.new_entry_field = ""
tempid = model.next_id+1
return Model.new(next_id: tempid, new_entry_field: "", entries: list)
end
end

Expand All @@ -20,7 +22,7 @@ def initialize(str)
attr_reader :str

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

Expand All @@ -32,11 +34,16 @@ def initialize(id, is_completed)
attr_reader :id, :is_completed

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

end
end
return Model.new(next_id: model.next_id, new_entry_field: model.new_entry_field, entries: list)
end
end

Expand All @@ -48,13 +55,17 @@ def initialize(id)
attr_reader :id

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

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

Expand Down
4 changes: 2 additions & 2 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,7 +20,7 @@ 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? &&
Expand Down
2 changes: 1 addition & 1 deletion 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
5 changes: 3 additions & 2 deletions swift-todo/Sources/Todo/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ struct Engine {
}

static func runWithHistory(on model: Model, applying messages: [Message]) -> [Model] {
var newM = model
return messages.map { message in
message.apply(to: model)
return model
newM = message.apply(to: newM)
return newM
}
}
}
31 changes: 21 additions & 10 deletions swift-todo/Sources/Todo/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
}
}
4 changes: 2 additions & 2 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 Down