REBOL 3.0

Comments on: FOREACH Series Reference Variable

Carl Sassenrath, CTO
REBOL Technologies
2-May-2006 16:33 GMT

Article #0015
Main page || Index || Prior Article [0014] || Next Article [0016] || 15 Comments || Send feedback

Something I've always wanted in foreach: the ability to access the series, not just its values. In otherwords, kind of a blend of forall and foreach.

One way to do this is to allow the foreach word block to include some kind of notation that a word is meant to refer to the series, not a value of the series. For example:

foreach [name count:] user-data [
    change count length? name
]

This example shows the power of the method. Here the count variable refers to the series at that index, so the example is modifying the actual series block here. The set-word notation is consistent with that used in the parse dialect for a similar purpose.

You can also use such a word to get you the index position without needing to increment a separate variable within your foreach loop.

15 Comments

Comments:

Anton Rolls
2-May-2006 13:21
That is cool. Like it.
Henrik
2-May-2006 13:26
I like the idea very much.

However I've often wondered if it would be possible to have foreach traverse multiple blocks simultaneously?

if I have blocks A and B:

a: ["buck" "rogers"] b: ["is his first name" "is his last name"]

foreach [c1 c2] [a b] [ print [c1 c2] ]

would return:

buck is his first name rogers is his last name

The number of blocks should be arbitrary.

Ryan Cole
2-May-2006 13:26
:) I would use carls suggestion regularly.

I would also use something like henrik's proposed rather frequently as well. Perhaps foreach/with or forwith.

Brian Hawley
2-May-2006 16:02
Sounds great Carl!

Henrik, without a refinement your suggestion would conflict with a lot of code, but with one it might be interesting. I have no idea what to call that refinement though.

Henrik
2-May-2006 16:10
Brian, yes I found out. It would work with a refinement.

Suggestions:

/many /multi /parallel

I don't know. Hard one to name.

Ingo
2-May-2006 16:27
+2 (one for each suggestion, Carls and Henriks :)

Espacially Henriks idea would be _really_ interesting for me.

One thing to think about, what happens if those series have different length?

/parallel sounds best for me

Henrik
2-May-2006 16:52
"One thing to think about, what happens if those series have different length?"

It could stop at the shortest series. A /pad refinement could then return NONE for those shorter series to let it finish with the longest.

I think SET has a /pad refinement which does something similar.

Another thing that would be cool, would be to allow the short series to loop on top of the longer one, so that the short one just starts over again, when it reaches tail. FOREACH would stop when the long one reached tail.

This could be a /loop refinement.

This would be nice to render HTML tables very quickly:

out: copy [] colors: [#cccccc #ffffff] cells: [1 2 3 4 5 6 7 8 9 "Finished!"]

foreach/parallel/loop [c cell] [colors cells] [ append out reduce [ build-tag reduce ['td 'bgcolor c] cell ] ] form append out

Of course untested, but you get the idea.

A problem would be to determine which one should loop when there are more than two blocks. Maybe they could primitively all loop.

Argh! Many ideas. :)

Henrik
2-May-2006 16:55
Reposting the code. The HTML tags were filtered out:

out: copy [<table>] colors: [#cccccc #ffffff] cells: [1 2 3 4 5 6 7 8 9 "Finished!"]

foreach/parallel/loop [c cell] [colors cells] [ append out reduce [ <tr> build-tag reduce ['td 'bgcolor c] cell </td> </tr> ] ] form append out </table>

Brian Hawley
2-May-2006 17:27
Henrik, try to implement your ideas in REBOL code. Post it to AltME if you need help. Let us hash out the details, improve it. If it turns out well, it could become a mezzanine function.
Gregg Irwin
2-May-2006 18:15
I think Cyphre and Gabriele have both posted similar functions for iterating over multiple series. It's something that requires some deep thought to get right I think.

I like Carl's idea; could be very handy.

Henrik
2-May-2006 18:21
Gregg, I've been told by Gabriele that multiple series is already supported in R3's FOREACH function, so I'm not going to waste too much time reinventing more wheels than necessary. :)
Henrik
2-May-2006 18:21
Gregg, I've been told by Gabriele that multiple series is already supported in R3's FOREACH function, so I'm not going to waste too much time reinventing more wheels than necessary. :)
Robert
3-May-2006 5:25
Carl, yes please add this. Very helpful and a common pattern that's just a lot of effort today.
Goldevil
3-May-2006 7:17
And interresting idea from Carl. Maybe, to keep it simple, we need a foreach function syntaxically not very different from the one of rebol 2.

And we create another function (maybe built with foreach) that includes the (great!) idea of Henrik with ... - a syntax that suits more for the common use - the possibility process any number of series - loop is the default behaviour if a serie is shorter than others. Refinement for stopping at the en of the shortest serie.

Proposal:

>>a: [ 1 2 3] >>b: ["a" "b" "c" "d"] >>c: [red blue] >>d: ["Robert" "Bill" "Brian" "Carl" "Ingo" "Gregg" "Goldevil"] >> forevery [ [a seriea] [b serieb ] [c seriec] [d seried] ] [ print [a b c d] ]

1 a red Robert 2 b blue Bill 3 c red Brian 1 d blue Carl 2 a red Ingo 3 b blue Gregg 1 c red Goldevil

PS: that's funny but html table generation was also exactly what I was thinking about when reading Carl post.

Tomc
5-May-2006 5:36
with a foreach you might be able to wean me from

while[not tail? here][ . . . here: next here ]

that comes up endlessly

Post a Comment:

You can post a comment here. Keep it on-topic.

Name:

Blog id:

R3-0015


Comment:


 Note: HTML tags allowed for: b i u li ol ul font span div a p br pre tt blockquote
 
 

This is a technical blog related to the above topic. We reserve the right to remove comments that are off-topic, irrelevant links, advertisements, spams, personal attacks, politics, religion, etc.

REBOL 3.0
Updated 24-Apr-2024 - Edit - Copyright REBOL Technologies - REBOL.net