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

foreach with block question

 [1/15] from: m_gaya_t::hotmail::com at: 13-Oct-2001 19:18


foreach can be used with a block of words. for example:
>> data: [ 1 AAA BBB 3 0
[ 2 MMM XXX 1 1 [ 3 DDD VVV 2 1 ] == [1 AAA BBB 3 0 2 MMM XXX 1 1 3 DDD VVV 2 1]
>> foreach [ a b c d e ] data [ print [ a b c d e ]]
1 AAA BBB 3 0 2 MMM XXX 1 1 3 DDD VVV 2 1 but if I want generalize the foreach expression...
>> row: [ a b c d e ]
== [a b c d e]
>> foreach row data [ print row ]
1 AAA BBB 3 0 2 MMM XXX 1 1 3 DDD VVV 2 1
>>
row is considered a word, not a block. How can I use foreach with a block ? thanks, Miquel.

 [2/15] from: al:bri:xtra at: 14-Oct-2001 7:49


Miquel Gaya asked:
> How can I use foreach with a block ? >> do compose/deep [foreach [(row)] data [print [(row)]]]
1 AAA BBB 3 0 2 MMM XXX 1 1 3 DDD VVV 2 1 Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [3/15] from: joel:neely:fedex at: 13-Oct-2001 17:06


Hi, Miquel, Miquel Gaya wrote:
> but if I want generalize the foreach expression... > >> row: [ a b c d e ]
<<quoted lines omitted: 3>>
> AAA > BBB
...
> DDD > VVV
<<quoted lines omitted: 3>>
> row is considered a word, not a block. > How can I use foreach with a block ?
Depending on what you want to do inside the repeated block...
>> rowlength: 5
== 5
>> s: data
== [1 AAA BBB 3 0 2 MMM XXX 1 1 3 DDD VVV 2 1]
>> forskip s rowlength [print [copy/part s rowlength]]
1 AAA BBB 3 0 2 MMM XXX 1 1 3 DDD VVV 2 1 == false -jn- -- ; sub REBOL {}; sub head ($) {@_[0]} REBOL [] # despam: func [e] [replace replace/all e ":" "." "#" "@"] ; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"} print head reverse despam "moc:xedef#yleen:leoj" ;

 [4/15] from: jeff:rebol at: 13-Oct-2001 16:32


Howdy, Miquel:
> >> data: [ > 1 AAA BBB 3 0 > 2 MMM XXX 1 1 > 3 DDD VVV 2 1 > ]
. . .
> but if I want generalize the foreach expression... > > >> row: [ a b c d e ] > == [a b c d e] > >> foreach row data [ print row ]
...produces one item per line. Try: foreach :row data [print bind row 'a] -jeff

 [5/15] from: al:bri:xtra at: 14-Oct-2001 19:54


Jeff's neat solution:
> foreach :row data [print bind row 'a]
but if your 'foreach block doesn't want to know what's in 'row, then:
>> foreach :row data [print bind row first row]
3 DDD VVV 2 1 3 DDD VVV 2 1 3 DDD VVV 2 1 Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [6/15] from: chris:zaeon at: 14-Oct-2001 7:14


> Try: > > foreach :row data [print bind row 'a] > > -jeff
Hi. I've looked up bind in both the dictionary and user's guide, and I'm still no wiser as to how and why bind works here. Anyone care to pound some understanding into my skull? -- chris

 [7/15] from: al:bri:xtra at: 16-Oct-2001 15:57


Gabriele pointed out:
> ...you see that you got 3 identical rows.
Ack! I must be going blind! :-) Thanks, Gabriele for pointing it out. Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [8/15] from: chris:zaeon at: 15-Oct-2001 1:10


> Hi, Chris, > > chris palu wrote: > > > > Anyone care to pound some understanding into my skull? > > > > I left my big hammer at work, but I'll try to offer some ideas! ;-)
Joel, Many thanks for the explanation. Very illuminating. Your chisel work is impressive! thanks, chris

 [9/15] from: g:santilli:tiscalinet:it at: 15-Oct-2001 22:05


Hello Andrew! On 14-Ott-01, you wrote: AM> That's funny, it worked on my Rebol console? That was a AM> direct copy from my Rebol console?! Probably you had a global 'A (as well as 'B, etc.), or your ROW block was bound to some context where those word had some meaning. Indeed, if you look at your output (copying from your post):
>> foreach :row data [print bind row first row]
3 DDD VVV 2 1 3 DDD VVV 2 1 3 DDD VVV 2 1 ...you see that you got 3 identical rows. Regards, Gabriele. -- Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/

 [10/15] from: m_gaya_t:hotmai:l at: 14-Oct-2001 14:26


Hi all, no one, but three solutions! (1) From Andrew, with compose/deep. Compose is the rebol pre-processor. I need to re-read the rebol-zine about compose. (2) From Joel, using forskip. It's a more direct one. I like this solution but it's need copy/part. (3) From Jeff, with bind!. I just can say: it's works!!. I agree with Chris Palu about its esotherical look :) It seems you can subtitute 'a with 'b or 'c etc. but no with 'f or another letter. ( really esotherical to me), (4) Andrew observe the dependency of first word of row in the Jeff's solution, but fail in yours correction. I can't correct Andrew's intent myself. Are there an independent of 'a Jeff's solution type of solution ? Thanks you all! I enjoy reading the rebol-list. Miquel

 [11/15] from: joel:neely:fedex at: 14-Oct-2001 8:27


Hi, Chris, chris palu wrote:
> > Try: > >
<<quoted lines omitted: 4>>
> I'm still no wiser as to how and why bind works here. > Anyone care to pound some understanding into my skull?
I left my big hammer at work, but I'll try to offer some ideas! ;-) A REBOL word is just a name or symbol. Its value comes from the context that gives it meaning. Words in different contexts can have totally unrelated values, even when the words' names are spelled the same. Consider this example:
>> glorp: "I'm global!"
== "I'm global!"
>> wordblock: [glorp]
== [glorp]
>> some-fun: func [b [block!] /local glorp] [
[ glorp: "I'm local!" [ print [glorp] [ print b [ ]
>> some-fun wordblock
I'm local! I'm global! The word named "glorp" in the block is the externally-defined word. That fact doesn't change just because the block is handed in to the function. Therefore the second line of output is still referring to the value of the global "glorp" and not the local one. The role of BIND is to change the context of words in its first argument (if possible) to be the context of its second (sample) argument. Therefore, we can do this:
>> glorp: "I'm global!"
== "I'm global!"
>> glunk: "I'm global, too!"
== "I'm global, too!"
>> wordblock: [glorp glunk]
== [glorp glunk]
>> some-fun: func [b [block!] /local glorp foo] [
[ glorp: "I'm local!" [ foo: 42 [ print [glorp] [ print b [ bind b 'foo [ print b [ ] Similar exercise, but I added another global word to the block. In addition, I added an irrelevant local named FOO, just to show that the second argument to BIND is simply a *sample* word which indicates which context to use -- you don't need to worry about making names match.
>> print wordblock
I'm global! I'm global, too! Just what we expect.
>> some-fun wordblock
I'm local! I'm global! I'm global, too! I'm local! I'm global, too! Notice that after BIND does its thing, the first word in the block has been changed (bound) to the function's context. That's why the last output line is different from the second.
>> print wordblock
I'm local! I'm global, too! Notice too, that the change is permanent. Now the "glorp" in the first position of WORDBLOCK is the one in the function's context and not the global one. If this kind of aliasing is a problem, you could rewrite the demo function as:
>> some-fun: func [b [block!] /local glorp foo] [
[ glorp: "I'm local!" [ foo: 42 [ print [glorp] [ print b [ b: bind copy b 'foo [ print b [ ] (We also have to re-create WORDBLOCK...)
>> wordblock: [glorp glunk]
== [glorp glunk] Now we can do this:
>> some-fun wordblock
I'm local! I'm global! I'm global, too! I'm local! I'm global, too!
>> print wordblock
I'm global! I'm global, too! Since SOME-FUN modified a copy of the argument, the "glorp" in the original WORDBLOCK is not rebound. Hope this helps! -jn- -- ; sub REBOL {}; sub head ($) {@_[0]} REBOL [] # despam: func [e] [replace replace/all e ":" "." "#" "@"] ; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"} print head reverse despam "moc:xedef#yleen:leoj" ;

 [12/15] from: joel:neely:fedex at: 14-Oct-2001 8:30


Hi, Miquel, Miquel Gaya wrote:
> Are there an independent of 'a Jeff's solution type of solution ? >
Here's a variation that favors readability over esoterica. This way you can use whatever names you like, and do other things with them than just print the (sub-)block of values.
>> rowlength: 5
== 5
>> s: data
== [1 AAA BBB 3 0 2 MMM XXX 1 1 3 DDD VVV 2 1]
>> forskip s rowlength [
[ use [u v x y z] [ [ set [u v x y z] copy/part s rowlength [ print [v x y z y + z "(" u ")"] [ ] [ ] AAA BBB 3 0 3 ( 1 ) MMM XXX 1 1 2 ( 2 ) DDD VVV 2 1 3 ( 3 ) As far as BIND itself, please see my note to Chris about how that one works. That might explain why "a" through "e" are the only names which work at the end of the BIND phrase. HTH! -jn- -- ; sub REBOL {}; sub head ($) {@_[0]} REBOL [] # despam: func [e] [replace replace/all e ":" "." "#" "@"] ; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"} print head reverse despam "moc:xedef#yleen:leoj" ;

 [13/15] from: g:santilli:tiscalinet:it at: 14-Oct-2001 11:35


Hello Miquel! On 13-Ott-01, you wrote: MG> row is considered a word, not a block. MG> How can I use foreach with a block ?
>> data: [ 1 AAA BBB 3 0
[ 2 MMM XXX 1 1 [ 3 DDD VVV 2 1 ] == [1 AAA BBB 3 0 2 MMM XXX 1 1 3 DDD VVV 2 1]
>> row: [ a b c d e ]
== [a b c d e]
>> foreach :row data [ print bind row 'a ]
1 AAA BBB 3 0 2 MMM XXX 1 1 3 DDD VVV 2 1 or, faster:
>> foreach :row data compose/deep [ print [(row)] ]
1 AAA BBB 3 0 2 MMM XXX 1 1 3 DDD VVV 2 1 Regards, Gabriele. -- Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/

 [14/15] from: g:santilli:tiscalinet:it at: 14-Oct-2001 11:41


Hello Andrew! On 14-Ott-01, you wrote: AM>>> foreach :row data [print bind row first row] Won't work because FIRST ROW is not bound to FOREACH's context. Need compose here (see my other post)... Regards, Gabriele. -- Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/

 [15/15] from: al:bri:xtra at: 15-Oct-2001 7:14


Gabriele wrote:
> AM>>> foreach :row data [print bind row first row] > > Won't work because FIRST ROW is not bound to FOREACH's context. > Need compose here (see my other post)...
That's funny, it worked on my Rebol console? That was a direct copy from my Rebol console?! Andrew Martin Stunned... ICQ: 26227169 http://valley.150m.com/

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