w***@gmail.com
2014-11-23 00:53:28 UTC
I encountered this code:
BlockClosure extend [
times: count [
(count = 0)
ifTrue: [^self value]
ifFalse: [self value. ^self time: count - 1]
]
].
x := 5.
[x := x - 1] time: 3 .
x printNl.
This gives me 1. I know this uses recursion to achieve iteration. But I wonder why the count which is a constant (3) here, can be decremented each time of iteration?
Another question is why do we need the "self value" in the "ifFalse" block. If I remove it, it then seems that there is no iteration. The block [x := x-1] executed one time and then stopped. Do anyone know the answers to these questions? Thanks
BlockClosure extend [
times: count [
(count = 0)
ifTrue: [^self value]
ifFalse: [self value. ^self time: count - 1]
]
].
x := 5.
[x := x - 1] time: 3 .
x printNl.
This gives me 1. I know this uses recursion to achieve iteration. But I wonder why the count which is a constant (3) here, can be decremented each time of iteration?
Another question is why do we need the "self value" in the "ifFalse" block. If I remove it, it then seems that there is no iteration. The block [x := x-1] executed one time and then stopped. Do anyone know the answers to these questions? Thanks