1. feature request : for


I have been duplicating loops when i need only one, if the following were possible:

for dirloop = {0..9,A..Z,a..z} do -- lists of atoms, use directly 
 if dir(rootdir & dirloop) then 
    -- code 
 end if 
end for 
-- 
for looptr = {animals[1..$],bacteria[1..$]} do -- lists of ranges, index to each one 
 if -- etc, process each animal and bacteria 
end for 
-- 
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 


Basically, if the [START to END] is replaced with a sequence, or a series of atoms, or a list of sequences, step thru them (instead of stepping thru the default list of integers 1,2,3,4,5,6,7,8,9 etc). Yes, i realise this would be window dressing (eye candy) on other ways of doing it. Your mileage may vary.

useless

new topic     » topic index » view message » categorize

2. Re: feature request : for

Kat said...

I have been duplicating loops when i need only one, if the following were possible:

Something like this has been suggested but with different syntax to avoid accidental coding mistakes ...

for each dirloop in {0..9,A..Z,a..z} do -- lists of atoms, use directly 
 if dir(rootdir & dirloop) then 
    -- code 
 end if 
end for 
-- 
for each looptr in {animals[1..$],bacteria[1..$]} do -- lists of ranges, index to each one 
 if -- etc, process each animal and bacteria 
end for 
-- 
for csvlineptr = 1 to length(csv_txt) do 
  for each fieldptr in {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 
-- 
object txtline 
while sequence(txtline) with entry do 
     for each char in textline do 
         ... 
     end for 
entry 
    txtline = gets(fileid) 
end while 

This construct could also lead to some better optimised intermediate code in some circumstances.

new topic     » goto parent     » topic index » view message » categorize

3. Re: feature request : for

DerekParnell said...
Kat said...

I have been duplicating loops when i need only one, if the following were possible:

Something like this has been suggested but with different syntax to avoid accidental coding mistakes ...

for dirloop = 'A' to 'Z' do -- existing syntax 
 
---- vs ---- 
 
for dirloop = {0..9,A..Z,a..z} do -- my suggestion  
 
---- vs ---- 
 
for each dirloop in {0..9,A..Z,a..z} do -- your suggestion 

This construct could also lead to some better optimised intermediate code in some circumstances.


I don't think "for each" flows off the tongue easily, and i don't see how a coding mistake could be made in that example. It's merely specifying a start and end, and a list of in-between alternatives to incrementing a counter of integers. It also introduces no new keywords or syntax (really). The loop processes each value in the range in each case, adding the words "each" to the new form confuses me, because it could just as validly be applied to the current syntax. Adding "each" would negate the use of the "continue" keyword or the current "by delta" syntax.

Please elaborate your points to show me why adding "each" is a good idea.

useless

new topic     » goto parent     » topic index » view message » categorize

4. Re: feature request : for

Kat said...

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 ...

for X = A do
and
for X = A to B do

are visually similar and the reader of the code won't see the difference until reaching the to keyword. Whereas using a qualifier next to the for keyword quickly signals a special case of the for to the code reader.

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 

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.

for each char in txtline do 
    if char = WHATEVER then 
        continue 
    end if 
    ... 
end for 
 
Y = 0 
stuff = {1,3,6,9,12,25,39} 
for each X in stuff by 2 do 
    Y += X 
end for 
? Y --> 58 
new topic     » goto parent     » topic index » view message » categorize

5. Re: feature request : for

DerekParnell said...
Kat said...

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 ...

for X = A do
and
for X = A to B do

are visually similar and the reader of the code won't see the difference until reaching the to keyword. Whereas using a qualifier next to the for keyword quickly signals a special case of the for to the code reader.


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 to B do
for X = {A..Z,a..z} do
for X = {ABNMWEPORLTYOIUGHBDHA} do


DerekParnell said...

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.

DerekParnell said...

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

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu