Update.
This commit is contained in:
+50
@@ -0,0 +1,50 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "scheduler" for Scheduler, Future
|
||||
|
||||
class Counter {
|
||||
construct new(start) {
|
||||
_value = start
|
||||
}
|
||||
|
||||
value { _value }
|
||||
|
||||
add(n) {
|
||||
var op = async { |x|
|
||||
_value = _value + x
|
||||
return _value
|
||||
}
|
||||
return await op(n)
|
||||
}
|
||||
|
||||
subtract(n) {
|
||||
var op = async { |x|
|
||||
_value = _value - x
|
||||
return _value
|
||||
}
|
||||
return await op(n)
|
||||
}
|
||||
|
||||
reset() {
|
||||
var op = async {
|
||||
_value = 0
|
||||
return _value
|
||||
}
|
||||
return await op()
|
||||
}
|
||||
}
|
||||
|
||||
var counter = Counter.new(10)
|
||||
System.print(counter.value) // expect: 10
|
||||
|
||||
System.print(counter.add(5)) // expect: 15
|
||||
System.print(counter.value) // expect: 15
|
||||
|
||||
System.print(counter.subtract(3)) // expect: 12
|
||||
System.print(counter.value) // expect: 12
|
||||
|
||||
System.print(counter.reset()) // expect: 0
|
||||
System.print(counter.value) // expect: 0
|
||||
|
||||
System.print(counter.add(10)) // expect: 10
|
||||
System.print(counter.subtract(3)) // expect: 7
|
||||
Reference in New Issue
Block a user