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

IDIOMS: setting multiple words

 [1/12] from: greggirwin::mindspring::com at: 9-Oct-2003 13:08


Hi All, I had a thought as I was tinkering with different REBOL constructs, to see which one I liked best, for a particular bit of functionality. Sometimes, as you know, a very REBOLish idiom can make all the difference in the world. Here's the idea; you pose a scenario and people can post different approaches they might use to handle it. It's not a contest for the smallest solution, but rather to show different *ways* things can be done. ;---------------------------------------------------------------- Scenario: You have a number of words that you want initialized to "copy []" or some other value. Do you do it like this? foo: copy [] bar: copy [] baz: copy [] Or like this? foreach word [foo bar baz][set word copy []] Or something else. Do you use a different approach for more or less words? Do you do things differently if you're setting words in a object? Do you do something different based on the initial value? Is there anything else you take into consideration when you do this kind of thing? Thanks for playing! --Gregg

 [2/12] from: Steven:White:ci:bloomington:mn:us at: 9-Oct-2003 14:38


>>> [greggirwin--mindspring--com] 10/09/03 02:08PM >>> >>>Is there anything else you take into consideration when you do this >>>kind of thing? >>>Thanks for playing!
I would vote for the first way. My primary consideration in all coding is ease of understanding for someone reading the code, even if it means extra words or a performance hit. I think there is a lot of programming genius wasted because people can't figure out how things work. The job of a programmer is not just to produce programs, but to produce programs that can be understood. Especially in a business, source code is a corporate asset (versus a personal work of art), and lack of understandability diminishes its value. Steven White City of Bloomington 1800 W Old Shakopee Rd Bloomington MN 55431-3096 USA 952-563-4882 (voice) 952-563-4672 (fax) [steven--white--ci--bloomington--mn--us]

 [3/12] from: joel:neely:fedex at: 9-Oct-2003 15:04


Hi, Greg, Gregg Irwin wrote:
> Scenario: > You have a number of words that you want initialized to "copy []" or
<<quoted lines omitted: 5>>
> foreach word [foo bar baz][set word copy []] > Or something else.
Occasionally I might write foo: copy bar: copy baz: copy [] to emphasize that they're all "alike" in some way. That also scales well if the intial value is more complex than [] (especially if it must be computed, but that value -- or a copy -- is needed for more than one variable). Otherwise, I'd probably use your first case.
> Do you use a different approach for more or less words? >
If I had more words than the above, I'd start looking at whether I needed to redesign so that they all were parts of a larger data structure, rather than individual words.
> Do you do things differently if you're setting words in a object? >
When setting up an object, I usually prefer the foo: ... bar: ... baz: ... format to make the members of the object very clearly visible.
> Do you do something different based on the initial value? > > Is there anything else you take into consideration when you do this > kind of thing? >
For both of these, see above comments re scaling and number of distinct copies.
> Thanks for playing! >
Thanks for asking! ;-) -- ---------------------------------------------------------------------- Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446 Counting lines of code is to software development as counting bricks is to urban development.

 [4/12] from: SunandaDH:aol at: 9-Oct-2003 16:05


Hi Gregg,
> foo: copy [] > bar: copy [] > baz: copy [] > > Or like this? > > foreach word [foo bar baz][set word copy []] > > Or something else. >
I'd go for the first way -- that way I can easily change my mind if I need to set 'bar to something else. If you are going to use set, you could use a shorter method: set [foo bar baz] copy [] Another method you will see around is: foo: bar: baz: copy [] To all your other questions: I'd take a tactical approach if I had lots more fields, or something special was indicated. I've very occasionally had to extend a function that returned a single value to return more than one value. Set is a quick hack in that case: my-func: func [] [return [1 2]] set [a b] my-func Sunanda

 [5/12] from: rebol:techscribe at: 9-Oct-2003 13:57


Hi Sunanda:
>If you are going to use set, you could use a shorter method: > > set [foo bar baz] copy [] >
You will find that only foo is set set to a block, whereas bar and baz are initialized to the value none. This is unlike using set [foo bar baz[] 3 ;- (i.e. some non-series value where all words will be initialized to the same value.
>Another method you will see around is: > > foo: bar: baz: copy [] >
Note that, unlike in Greg's example, here all three words will be set to one and the same block. I.e. any modifications made to the block baz will also affect the blocks foo and bar, because they are one and the same block.

 [6/12] from: greggirwin:mindspring at: 9-Oct-2003 15:10


Hi Sunanda, Sac> If you are going to use set, you could use a shorter method: Sac> set [foo bar baz] copy [] Sac> Another method you will see around is: Sac> foo: bar: baz: copy [] You have to be watch, though, for the distinction between setting each word to refer to its *own* copy of [] and setting them all to refer to the *same* copy of []. Inlining set-word!s is very handy, and safe, for non-series values, but I don't use it very often for series values. Sac> I've very occasionally had to extend a function that returned a single value Sac> to return more than one value... Yes, another idiom worth discussing. :) -- Gregg

 [7/12] from: greggirwin:mindspring at: 9-Oct-2003 15:26


>> set [foo bar baz] copy [] >>
E> You will find that only foo is set set to a block, whereas bar and baz E> are initialized to the value none. This is unlike using E> set [foo bar baz[] 3 ;- (i.e. some non-series value E> where all words will be initialized to the same value. Excellent point Elan! And a great example of how what we *think* is just a different idiom might actually behave differently. -- Gregg

 [8/12] from: rebol:techscribe at: 9-Oct-2003 14:26


Hi Sunanda. Incidentally,
> set [foo bar baz] copy []
will not set any of the words to a value (because REBOL attempts to set foo, bar, and baz to the value value(s) contained IN the block) . What I described in my previous email would be true for set [foo bar baz] reduce [ copy [] ]

 [9/12] from: tomc:darkwing:uoregon at: 9-Oct-2003 15:23


I also stick the first more verbose method. the others will eventually turn around and bite you (they have me) On Thu, 9 Oct 2003, Gregg Irwin wrote:

 [10/12] from: atruter:labyrinth:au at: 10-Oct-2003 11:21


Hi Gregg, Like others I tend to prefer the: foo: copy [] bar: copy [] baz: copy [] idiom for most cases. I often find my style changes depending upon chosen word size: variable-number-1: none variable-number-2: none variable-number-3: none for long words, and: v1: v2: v3: none for shorter ones. Also, I tend to use set when the values are different and the word names short: set [v1 v2 v3] [1 2 3] It's very subjective, but I tend to go for the idiom that looks compact while not taking up "too much" space on a single line. This "too much" might be absolute (ie. no more than 40 characters) or relative to the length of the preceeding and / or following line(s). How's that for obscure! ;) Regards, Ashley

 [11/12] from: SunandaDH:aol at: 10-Oct-2003 2:32


Thanks Elan and Gregg for the corrections to my attempted examples. Just goes to show that REBOL is subtle, and proves the old mxim: "if you ain't tested it, it don't work" Sunanda

 [12/12] from: joel:neely:fedex at: 10-Oct-2003 7:37


Hi, Elan and all, Elan wrote:
> Hi Sunanda: >>
<<quoted lines omitted: 3>>
> set [foo bar baz[] 3 ;- (i.e. some non-series value > where all words will be initialized to the same value.
Actually, all of FOO BAR and BAZ will be set to NONE, as the empty list is the source of *all* values for the three words:
>> set [foo bar baz] copy []
== []
>> foo
== none
>> bar
== none
>> baz
== none
>>
-jn-

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