A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
20
Task/History-variables/Scala/history-variables-1.scala
Normal file
20
Task/History-variables/Scala/history-variables-1.scala
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
class HVar[A](initialValue: A) extends Proxy {
|
||||
override def self = !this
|
||||
override def toString = "HVar(" + !this + ")"
|
||||
|
||||
def history = _history
|
||||
|
||||
private var _history = List(initialValue)
|
||||
def unary_! = _history.head
|
||||
def :=(newValue: A): Unit = {
|
||||
_history = newValue :: _history
|
||||
}
|
||||
def modify(f: A => A): Unit = {
|
||||
_history = f(!this) :: _history
|
||||
}
|
||||
def undo: A = {
|
||||
val v = !this
|
||||
_history = _history.tail
|
||||
v
|
||||
}
|
||||
}
|
||||
21
Task/History-variables/Scala/history-variables-2.scala
Normal file
21
Task/History-variables/Scala/history-variables-2.scala
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
scala> val h = new HVar(3)
|
||||
h: HVar[Int] = HVar(3)
|
||||
|
||||
scala> h := 11
|
||||
|
||||
scala> h := 90
|
||||
|
||||
scala> !h
|
||||
res32: Int = 90
|
||||
|
||||
scala> h.history
|
||||
res33: List[Int] = List(90, 11, 3)
|
||||
|
||||
scala> h.undo
|
||||
res34: Int = 90
|
||||
|
||||
scala> h.undo
|
||||
res35: Int = 11
|
||||
|
||||
scala> h.undo
|
||||
res36: Int = 3
|
||||
Loading…
Add table
Add a link
Reference in a new issue