Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Block building

 [1/6] from: landi:mac at: 24-Nov-2002 14:28


Hi all, I am relatively new to REBOL and would like to know what is considered best practice in the following situation: Given the following object:
>> probe Object1
make object! [ String1: "ABC" Block1: ["DEF" "GHI"] Block2: ["JKL" "MNO" "PQRSTUVWXYZ"] ] I need to build a block containing all of the string fields: ["ABC" "DEF" "GHI" "JKL" "MNO" "PQRSTUVWXYZ"] I have found two ways to do it:
>> compose [(Object1/String1) (Object1/Block1) (Object1/Block2)]
and
>> head insert (union Object1/Block1 Object1/Block2) Object1/String1
Comments? Suggestions? Thanks, Ted

 [2/6] from: gscottjones:mchsi at: 24-Nov-2002 14:51


Hi, Ted, From: Ted Landis
> I am relatively new to REBOL and would like to know what is considered > best practice in the following situation:
<<quoted lines omitted: 12>>
> >> head insert (union Object1/Block1 Object1/Block2) Object1/String1 > Comments? Suggestions?
I think that the expression, "There is more than one way to do it" is apropos. The best way is the way that works for you and makes sense when you look at it later. :-) Another variation on what you have already discovered includes: object1: make object! [ String1: "ABC" Block1: ["DEF" "GHI"] Block2: ["JKL" "MNO" "PQRSTUVWXYZ"] ] blk: copy [] append blk object1/string1 append blk object1/block1 append blk object1/block2 If you have really dynamic code, and therefore don't know in advance what components are in the object, you can take advantage of getting 'first on the object:
>> first object1
== [self String1 Block1 Block2]
>From the second position and on, you will have the names of the various
components. So, the following iterates through each of these, getting the value and appending to a blk (watch for line breaks): object1: make object! [ String1: "ABC" Block1: ["DEF" "GHI"] Block2: ["JKL" "MNO" "PQRSTUVWXYZ"] ] blk: copy [] foreach component next first object1 [ append blk get in object1 component] I'm sure that there will be other responses that show other ways. HTH --Scott Jones

 [3/6] from: ingo:2b1 at: 24-Nov-2002 23:13


Hi Ted and Scott, G. Scott Jones wrote:
> Hi, Ted, > > From: Ted Landis > > <...> > If you have really dynamic code, and therefore don't know in advance > what components are in the object, you can take advantage of getting > 'first on the object:
Or you could use 'second
>> probe second object1
[ make object! [ String1: "ABC" Block1: ["DEF" "GHI"] Block2: ["JKL" "MNO" "PQRSTUVWXYZ"] ] "ABC" ["DEF" "GHI"] ["JKL" "MNO" "PQRSTUVWXYZ"]] == [ ... As you see, 'second object! returns the object! itself, followed by a list of all values. So, you could use something like this:
>> block: copy []
== []
>> repeat value next second object1 [ insert block value ]
== ["DEF" "GHI" "ABC"]
>> block
== ["JKL" "MNO" "PQRSTUVWXYZ" "DEF" "GHI" "ABC"] Kind regards, Ingo

 [4/6] from: landi::mac::com at: 24-Nov-2002 22:00


Thanks Scott and Ingo, That was exactly the feedback I was looking for. I look forward to the day when all possible solutions come to my mind as easily as they seem to come to yours. My actual object has more values in it than the Object1 example I gave, but I could see using the dynamic content discovery techniques you suggested in another area. Ted On Sunday, Nov 24, 2002, at 17:13 Canada/Eastern, Ingo Hohmann wrote:
> Hi Ted and Scott, > G. Scott Jones wrote:
<<quoted lines omitted: 29>>
> [rebol-request--rebol--com] with "unsubscribe" in the subject, without the > quotes.
Ted Without deviation from the norm, progress is not possible. -- Frank Zappa

 [5/6] from: carl:cybercraft at: 25-Nov-2002 17:53


On 25-Nov-02, Ted and Suzanne Landis wrote:
> Hi all, > I am relatively new to REBOL and would like to know what is
<<quoted lines omitted: 14>>
> Object1/String1 > Comments? Suggestions?
As someone said, there's lots of ways to choose from. Here's a couple more...
>> head insert join object1/block1 object1/block2 object1/string1
== ["ABC" "DEF" "GHI" "JKL" "MNO" "PQRSTUVWXYZ"] That works, but isn't easily modified. This one's better I think...
>> extract reduce [Object1/String1 Object1/Block1 Object1/Block2] 1
== ["ABC" "DEF" "GHI" "JKL" "MNO" "PQRSTUVWXYZ"] Even so, your compose version is hard to beat, assuming you've no need for a general-purpose routine to extract arbitary strings and blocks of strings from an object. -- Carl Read

 [6/6] from: lennart:nylen:biz at: 25-Nov-2002 7:25


2002-11-24 20:28:00, Ted and Suzanne Landis <[landi--mac--com]> wrote:
>Given the following object: > >> probe Object1
<<quoted lines omitted: 10>>
> >> head insert (union Object1/Block1 Object1/Block2) Object1/String1 >Comments? Suggestions?
Out of those two, I'd say go with the first. a) It's simpler and cleaner (good rule of thumb when REBOLing). b) It's more generic (thus more reusable). Be adviced, you're about to learn about 500 other ways to do it...;-) Cheers! /Lennart Fridén ([lennart--nylen--biz]) The World is not sane so why should I be?

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted