[REBOL] Re: foreach with block question
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/ -><- -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
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 <[EMAIL PROTECTED]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/ -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
> 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 -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
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/ -><- -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
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 <[EMAIL PROTECTED]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/ -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
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 <[EMAIL PROTECTED]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/ -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
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" ; -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
Hi, Chris, chris palu wrote: > > > 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? > 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" ; -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
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 - Original Message - From: "Andrew Martin" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Saturday, October 13, 2001 8:49 PM Subject: [REBOL] Re: foreach with block question > 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/ > -><- > > > > -- > To unsubscribe from this list, please send an email to > [EMAIL PROTECTED] with "unsubscribe" in the > subject, without the quotes. > > -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
> 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 -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
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/ -><- -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
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 -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
Hi, Miquel, Miquel Gaya wrote: > > 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 ... > DDD > VVV > 2 > 1 > >> > > 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" ; -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.
[REBOL] Re: foreach with block question
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/ -><- -- To unsubscribe from this list, please send an email to [EMAIL PROTECTED] with "unsubscribe" in the subject, without the quotes.