Re: 2.6 feature request: foreach
- Posted by Jason Gade <jaygade at yahoo.com> Jul 13, 2005
- 448 views
D. Newhall wrote: > > I think that the next release should have something new and useful to the > language. > Sure, you could do "continue" or "next" or something but will everyone use > them? When > I was looking at all the code I've written I realized that maybe around half > of all > my "for" loops were being used to reference each element in a sequence one > after another. > Due to this I think that a "foreach" statement would be a VERY useful addition > to the > language that I'm sure everyone would use. > > It's syntax could go like so: > > Instead of > > sequence line > line = gets(0) > for i=1 to length(line) > if line[i] = -- something > -- code goes here > end if > end for > > One would type > > sequence line > line = gets(0) > foreach element in line do > if element = -- something > -- code goes here > end if > end foreach > > Here "element" would implicitly be declared as an object and act just like the > current > loop variable in "for". However, after every iteration of the loop "element" > will be > set to the next next element in "line". A consideration would be to optimize > expressions > of the type "foreach obj in reverse(seq)do" > In the context of a language that has a sequence as its major selling point, foreach makes a lot of sense. ===================================== Too many freaks, not enough circuses. j.