[array] [patch] Some other questions
[1/1] from: AJMartin::orcon::net::nz at: 24-Dec-2003 22:49
Anton wrote:
> Watch out though, each item in chickenfarm is the same chicken object:
Yes! You've found a bug in 'array!
> I recommend doing this instead:
>
> chickenfarm: copy [] ; make a new block
> loop 10 [append chickenfarm make chicken []]
>
> Now they are all cloned from the original chicken.
Here's a fix for 'array:
; Replacement 'array function that deep copies objects.
array: func [
"Makes and initializes a series of a given size."
size [integer! block!] "Size or block of sizes for each dimension"
/initial "Specify an initial value for all elements"
value "Initial value"
/local block rest
][
if not initial [value: none]
if block? size [
rest: next size
if tail? rest [rest: none]
size: first size
if not integer? size [make error! "Integer size required"]
]
block: make block! size
either not rest [
any [ ; I've noticed there's duplication here, but I haven't yet
thought of a solution
if series? value [
loop size [insert/only block copy/deep value]
]
if object? value [
loop size [insert/only block make value []]
]
insert/dup block value size
]
] [
loop size [
any [
if series? value [
value: copy/deep value
]
if object? value [
value: make value []
]
]
block: insert/only block array/initial rest value
]
]
head block
]
>> chicken: make object! [tasty: "wings"
[ ]
>> chickenfarm: array/initial 10 chicken
== [
make object! [
tasty: "wings"
]
make object! [
tasty: "wings"
]
make object! [
t...
>> chickenfarm/1/tasty: "drumsticks"
== "drumsticks"
>> chickenfarm
== [
make object! [
tasty: "drumsticks"
]
make object! [
tasty: "wings"
]
make object! [
...
A true 'chickenfarm with more than one chicken (object)! :)
Thanks, Anton for finding that out!
Andrew J Martin
Speaking in tongues and performing miracles.
ICQ: 26227169
http://www.rebol.it/Valley/
http://valley.orcon.net.nz/
http://Valley.150m.com/