[REBOL] Re: paths & lookups & change
From: joel:neely:fedex at: 19-Oct-2003 22:20
Hi, Robert,
Types are the issue... See below.
Robert M. Münch wrote:
> On Sat, 18 Oct 2003 11:28:07 +0200, Ingo Hohmann <[ingo--2b1--de]> wrote:
>>
>> >> change next pos: find test key pos/2 + 1
>>== []
>> >> test
>>== ["Bug Report" 2]
>>
>>Though I'm sure, someone will come up with a better idea ...
>
> Is it really that complicated? I wanted to avoid this. I thought about it
> too but I can't believe that we need to do all this just to add a value to
> some word stored in a block. Robert
>
Let's look closely at the example:
>> test: ["Bug Report" 2]
== ["Bug Report" 2]
>> key: "Bug Report"
== "Bug Report"
>> select test key
== 2
Note that the result of SELECT is an INTEGER! value, which is immutable.
We can't make a 2 become a different value; we can only have a container
which holds a copy of a 2 and replace that with a different integer.
One way is:
>> if found? pos: find test key [pos/2: pos/2 + 1]
== ["Bug Report" 3]
i.e. we need to change the block TEST so that the 2 following the value
of KEY is replaced with a 3 (since we can't change a 2).
Another way to keep the expression short is to provide an extra layer
of container:
>> test: ["Bug Report" [2]]
== ["Bug Report" [2]]
>> if found? val: select test key [val/1: val/1 + 1]
== [3]
>> test
== ["Bug Report" [3]]
However, the punch line remains the same; mutability requires use of a
container; therefore the mutation must be specified in terms of the
container.
-jn-