1. a question about foreach construct
- Posted by SunPsych08 Oct 15, 2015
- 1510 views
Hello,
I'd like to ask about the foreach construct.
In general, what advantage does it have over for...end for, aside from the obvious one of making it unnecessary to specify the counter?
And specifically for oE, would there be any particular advantage in having this construct in the language? Is there anything we can't do (or can't do conveniently) as a result of not having it in the language?
Thank you,
Alex
2. Re: a question about foreach construct
- Posted by ghaberek (admin) Oct 15, 2015
- 1476 views
Adding a new keyword syntax like this is kind of hard. There was a pretty decent discussion about this back in 2008.
I think there are a few small hurdles to get over, namely in the way of expected syntax and behavior. We have to answer questions like:
Can we modify the inner variable from within the block?
foreach item in s do if item = something then item += value -- is this legal? end if end foreach
Can we modify the outer sequence from within the block?
foreach item in s do if item = something then s = append( s, new_item ) -- is this legal? end if end foreach
Should foreach act like a for loop?
for i = 1 to length( s ) do object item = s[i] end for
Should foreach act like a while loop?
integer i = 1 while i <= length( s ) do object item = s[i] i += 1 end while
-Greg
3. Re: a question about foreach construct
- Posted by jmduro Oct 16, 2015
- 1455 views
Can we modify the outer sequence from within the block?
foreach item in s do if item = something then s = append( s, new_item ) -- is this legal? end if end foreach
-Greg
s being defined outside of the block, I don't see any reason to protect it against modification.
A loop is not a routine.
Jean-Marc
4. Re: a question about foreach construct
- Posted by SDPringle Oct 17, 2015
- 1439 views
Your last two examples, working like a for-loop or a while-loop seem identical.
foreach item in s do -- user code end foreach
would be equivalent to
i = 1 while i <= length(s) do object item = s[i] -- user code s[i] = item end for
You can add this by creating a preprocessor. The preprocessor will have to use automatic variables other than i, ideally ensuring the i counter does not already exist and is a very long identifier like i21312101240124012414.
Instead of assigning item to s[i], the preprocessor thing could substitute in s[i] for item. It should be done in a way that preserves lines
5. Re: a question about foreach construct
- Posted by ghaberek (admin) Oct 17, 2015
- 1440 views
You can add this by creating a preprocessor.
That would be great, but without a way to easily chew up the language and spit it back out, the preprocessor is effectively broken.
I've attempted write my own preprocessors for a few things and the smaller ones work well enough, but once it reaches a certain level of complexity I basically end up trying to write my own recursive-descent parser based on the included Euphoria tokenizer.
I don't think I should have to go through that much effort to write a preprocessor. I don't think the preprocessor should rely on ad hoc string parsing solutions to do its job. We should have a way to cleanly encounter bits of the language and interrupt or override the token stream with our own pieces.
I'm not trying to poo-poo your idea. It's a great idea. We just don't have a less-than-painfully-difficult way to implement it right now.
-Greg
6. Re: a question about foreach construct
- Posted by SDPringle Oct 17, 2015
- 1408 views
That's fair. And just an observation about this, it isn't very nice that if an error gets caught by the EUPHORIA parser, or by the backend, the user sees the error in what the preprocessor outputs rather than the user's own code.
Shawn
7. Re: a question about foreach construct
- Posted by elias_maluko Oct 18, 2015
- 1364 views
Foreach in euphoria will work only to read itens from a sequence. Because Euphoria's copy on change, we cant modify the fields of a loop variable:
foreach monster in monsters do monster[X] += 1; --will not work end foreach
So foreach in euphoria is a bit useless...