Re: feature request : for
- Posted by useless_ Oct 15, 2012
- 1094 views
Please elaborate your points to show me why adding "each" is a good idea.
It is not so much that "each" is a good idea per se, but more that the form ...
and
for X = A to B do
But we already have that syntax without the "each". In replacing only the "A to B" in that example with "{A..B}", or the expanded functionality of "{A..Z,a..z}" (or worse(?): {ABNMWEPORLTYOIUGHBDHA}), there is a lot of visual difference without the "each", primarily in hitting a '{' where an atom is expected, and the lack of spaces around a "to".
for X = {A..Z,a..z} do
for X = {ABNMWEPORLTYOIUGHBDHA} do
I was thinking that the 'each' qualifier would simply be syntax sugar ...
for each char in txtline do if char .... end for -- The above construct would be equivalent to ... for i = 1 to length(X) do object char char = X[i] if char ... end for
I understand that, and in that example, the use of "each" makes the sentence read more easily, but it's also closer to Ruby than Euphoria there. I'm not objecting to it smelling like Ruby, just saying it doesn't smell like Euphoria, and maybe mixing the two styles isn't all good. I might have simplified the top line to:
for loop = length(textline) do
as if all loops default to starting at 1 and length() is type atom, but this is a trivial example i won't argue for. My example of the text file was
for csvlineptr = 1 to length(csv_txt) do for fieldptr = {3,4,8} do -- list of atoms, step thru them -- code to munge csv_txt fields 3, 4, and 8 in all lines end for end for
which is a little different, in that i was showing the flexability of picking the fields to munge with the existing syntax and without jumping thru any hoops. The keyword "for" signals a loop, and i merely provide the list of values to iterate thru. I'm sure we can easily think of five ways to make that happen using different words and structures, but this seems easy to write and understand, and a minimalist elegant addition to an existing Euphoria function.
To me, the 'each' idea does roll off the tongue quite well. May its a difference in how we view the world.
I'm not sure why you think that continue or the by phrase would not be possible with the each idea.
Because both are words to skip processing "each" value of the loop variable. The use of "by 2" skips every other one, and a "continue" prevents the rest of the code in that scope from executing on that value. Both are preventing "each" value from being processed. In a way, it's like taking a great sandwich and adding extra unnecessary toppings. I seem to recall that Euphoria has a dearth of space for new keywords, and i wouldn't impose on that space with this word in this use.
useless