Re: case and foreach
- Posted by Derek Parnell <ddparnell at bigpond.com> Apr 01, 2005
- 571 views
codepilot Gmail Account wrote: > > Pros > I think foreach could be used to add some parallelism to euphoria and > simplify a few things that for loops and subscripting are used for. I can relate some experience here. The D language recently introduced a 'foreach' construct, and I have been using it where ever I can. It has made a huge difference by making code more easy to create, read and maintain. The syntax it uses is (in Euphoria-like code) ... for each [idx,] elem in seq do . . . end for where the 'elem' is an object whose scope is restricted to the 'for' loop and the optional 'idx' is an integer that contains the index number for 'elem' - its scope is also the 'for' loop. The code inside the loop uses 'elem' as if it was 'seq[idx]'. You can read 'elem' *and* write to 'elem'. For example: for each Line_Cnt, Line in TextFile do if Line_Cnt = 1 then doPrintHeading() end if if length(Line) != 0 then if Line[1] = '*' then Line = "(comment) " & Line[2..$] end if end if PrintLine(LineCnt, Line) if Line_Cnt = length(TextFile) then doPrintTrailer() end if end for Altering the length of the source sequence is permitted, but you are just asking for trouble if you start that sort of thing. > Case would be good for simplifying if/elsif/else blocks to a more > manageable level and could also help in optimization. Of course a 'case' construct would made code more readable and easier to maintain, but when has *that* ever been a priority. If fact, 'while' and 'for' are for wimps and coding gromets > Cons > Does anyone have something against case or foreach(element in a > sequence) in euphoria? Well, it does mean more work for anyone trying to implement the Euphoria language. So maybe we should all just forget about beating this dead horse; it ain't ever going to get up. (Did that come over as a little on the bitter side? Sorry if it did.) -- Derek Parnell Melbourne, Australia irc://irc.sorcery.net:9000/euphoria