[REBOL] Rolling your own ideal looper: does Repeat still have a bug? Re:(4)
From: galtbarber:mailandnews at: 28-Aug-2000 16:27
The original question, which may have been posted
by Paul Tretter, but I don't remember exactly,
was something like this:
foreach item a [
print index? item
Now, we all know why that doesn't work.
This does, but probably doesn't really
address his concern:
forall a [
print index? a
and now you have to remember to reset a
a: head a
and you have to use "first a"
to get the element everywhere in your code.
Sometimes I myself have wished I could
have my cake and eat it too. I want the index
that forall gives and I want the ease of use
of the assigned word for each element.
I am pretty dang sure lots of other Rebolers
out there beside me have come across the same
So I wrote something that did that, but I couldn't
figure out how to do that simply. Gabriele came
up with a wonderful little gem and Ladislav and
he made some more little improvements.
So, here it is, a kind of foreach with index access:
ideal-looper: func [
/local f i
f: func reduce [[throw] element [any-type!] index] code
while [i <= length? series] [f series/:i i i: i + 1]
And that's why we love Rebol!
And the punchline for Paul would be
ideal-looper item indx a [
print [item indx]
the output is:
And of course, there is no harm in having calls
to "ideal-looper" multiply nested or used inside
recursively called functions. And the words used
for the element and index are also just known
in the "scope" of the code block, and will not
stomp on your global wordspace.
I still don't really understand Ladislav's addition
of Throw and [any-type!]. Would anyone care