Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
11
Task/History-variables/Swift/history-variables-1.swift
Normal file
11
Task/History-variables/Swift/history-variables-1.swift
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
var historyOfHistory = [Int]()
|
||||
var history:Int = 0 {
|
||||
willSet {
|
||||
historyOfHistory.append(history)
|
||||
}
|
||||
}
|
||||
|
||||
history = 2
|
||||
history = 3
|
||||
history = 4
|
||||
println(historyOfHistory)
|
||||
38
Task/History-variables/Swift/history-variables-2.swift
Normal file
38
Task/History-variables/Swift/history-variables-2.swift
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
struct History <T> {
|
||||
|
||||
private var _history = [T]()
|
||||
var history : [T] {
|
||||
return _history
|
||||
}
|
||||
|
||||
var now : T {
|
||||
return history.last!
|
||||
}
|
||||
|
||||
init(_ firstValue:T) {
|
||||
_history = [firstValue]
|
||||
}
|
||||
|
||||
mutating func set(newValue:T) {
|
||||
_history.append(newValue)
|
||||
}
|
||||
|
||||
mutating func undo() -> T {
|
||||
guard _history.count > 1 else { return _history.first! }
|
||||
_history.removeLast()
|
||||
return _history.last!
|
||||
}
|
||||
}
|
||||
|
||||
var h = History("First")
|
||||
h.set("Next")
|
||||
h.set("Then")
|
||||
h.set("Finally")
|
||||
h.history // output ["First", "Next", "Then", "Finally"]
|
||||
|
||||
h.now // outputs "Finally"
|
||||
h.undo() // outputs "Then"
|
||||
h.undo() // outputs "Next"
|
||||
h.undo() // outputs "First"
|
||||
h.undo() // outputs "First", since it can't roll back any further
|
||||
h.undo() // outputs "First"
|
||||
Loading…
Add table
Add a link
Reference in a new issue