copy with object words and values
[1/5] from: greggirwin::starband::net at: 13-Sep-2001 13:36
Ingo Hohmann wrote a couple nifty functions for retrieving the words and
values from an object. In his routines, he uses copy when returning the
data.
; Ingo Hohmann
values-of: func [
"Returns block of object word values next to self directive"
object[object!] "Object words of which its values should be returned"
][
copy next second object
]
Q. Should I even bother worrying about efficiency? Should I just use copy,
as Ingo does, to be on the safe side or can I safely return the data without
using copy? I played around a bit and it doesn't appear that the object data
is at risk of modification, via the result, even without copy. I'm very
likely missing something though.
Q. If there is a difference, and I add a /copy refinement to the routine, is
there a way to "inline" copy so I don't have to have two separate blocks
that are identical except for the copy call?
; Gregg Irwin
values-of: func [
"Returns block of object word values next to self directive"
object[object!] "Object words of which its values should be returned"
/copy "Use the copy refinement if you want a copy of the data"
][
either copy [copy next second object][next second object]
]
Thanks!
--Gregg
[2/5] from: ryanc:iesco-dms at: 13-Sep-2001 15:18
Gregg Irwin wrote:
> Ingo Hohmann wrote a couple nifty functions for retrieving the words and
> values from an object. In his routines, he uses copy when returning the
<<quoted lines omitted: 11>>
> is at risk of modification, via the result, even without copy. I'm very
> likely missing something though.
Copying anything really large is slowish, like a block of 20000 words or so, and
should be avoided without reason. But in my view, with small portions of data,
I think about it from a desired functionality perspective. Sometimes a copy is
prefered, sometimes you want to work with the whole original. When writing
function "libraries", often it is good to return the original, so as the
programmer can decide whether a copy is warranted. On the other hand, the above
code represents an instance where you want to use 'copy, reminiscent of copying
the contents section from a book and excluding the first heading.
> Q. If there is a difference, and I add a /copy refinement to the routine, is
> there a way to "inline" copy so I don't have to have two separate blocks
<<quoted lines omitted: 7>>
> either copy [copy next second object][next second object]
> ]
First you have defined 'Copy to mean something else in the body of your
function. No problem though, it just has to be account for by refering to the
definition of copy in system/words.
You probably wont like the "inline" version as much, but here it is:
do either copy [get in system/words 'copy][none] next second object
This modification to what you have should work:
either copy [system/words/copy next second object][next second object]
But personally I would just use copy in all of them, leaving the refinement out.
--Ryan
> Thanks!
>
> --Gregg
>
> --
> To unsubscribe from this list, please send an email to
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
--
Ryan Cole
Programmer Analyst
www.iesco-dms.com
707-468-5400
[3/5] from: greggirwin:starband at: 13-Sep-2001 17:05
Hi Ryan,
<< When writing
function "libraries", often it is good to return the original, so as the
programmer can decide whether a copy is warranted. On the other hand, the
above
code represents an instance where you want to use 'copy, reminiscent of
copying
the contents section from a book and excluding the first heading. >>
Good point. I hadn't thought about the callers ability to move back to
someplace
you maybe don't want them to (e.g. back values-of object). OTOH, you can do
back copy values-of object
and be OK, so the caller is in control. Hmmm.
I'm
not worried so much about large objects but multiple hits on small objects.
<< First you have defined 'Copy to mean something else in the body of your
function. No problem though, it just has to be account for by refering to
the
definition of copy in system/words. >>
ACK! Obviously I hadn't tried this code yet.<g> Man, I love the flexibility
that
REBOL gives me but I'm sure I'll be shooting myself in the foot quite a lot
as I work with it. Thanks for catching that.
<< You probably wont like the "inline" version as much, but here it is:
do either copy [get in system/words 'copy][none] next second object >>
Yeah, that's kind of ugly.<g>
Thanks!
--Gregg
[4/5] from: allenk:powerup:au at: 15-Sep-2001 7:40
----- Original Message -----
From: "Ryan Cole" <[ryanc--iesco-dms--com]>
To: <[rebol-list--rebol--com]>
Sent: Friday, September 14, 2001 8:18 AM
Subject: [REBOL] Re: copy with object words and values
> Gregg Irwin wrote:
> > Ingo Hohmann wrote a couple nifty functions for retrieving the words and
<<quoted lines omitted: 5>>
> > "Returns block of object word values next to self directive"
> > object[object!] "Object words of which its values should be
returned"
> > ][
> > copy next second object
> > ]
Have you tried?
third object
(text from http://www.rebol.com/docs/core25.html)
You can use THIRD on the object to get back a block of set-word value pairs
from the object:
z: make object! [a: 99 b: does [a + 1]]
t: third z
== [a: 99 b: func [][a + 1]]
The block returned from THIRD on an object is like an object spec block,
however the set-words are bound to their object. This block is like a
snapshot of the object's values at that time. You can also use the returned
block to set values in the object:
set first t 199
z/b
== 200
Cheers,
Allen K
[5/5] from: greggirwin:starband at: 14-Sep-2001 16:03
Hi Allen,
<< Have you tried?
third object
(text from http://www.rebol.com/docs/core25.html) >>
I just found this when participating in the "This is a bug?" thread, but I
have the Core PDF for 2.3. Gotta upgrade that.<g>
Thanks!
--Gregg
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted