logo       

RE: standard for loop: msg#00201

lang.groovy.user

Subject: RE: standard for loop



> -----Original Message-----
> From: Shijun He [mailto:hax.sfo-Re5JQEeQqe8AvxtiuMwx3w@xxxxxxxxxxxxxxxx]
> Sent: Tuesday, November 30, 2004 10:44 AM
> To: user-i9PBDF1N6cxnkHa44VUL00B+6BGkLq7r@xxxxxxxxxxxxxxxx
> Subject: Re: [groovy-user] standard for loop
>
> for(x : y){} is the new syntax from jdk 5.0 , i guess groovy will
> support this in next version... btw, i think for(x in y) is better,
> for(x:y) seems strange...

I agree. However, I think they chose not to use "in" because it would
have meant adding a new reserved word. This doesn't matter to groovy
which doesn't have a large base of existing code. However, it is important
to Java. Adding "enum" broke some code I've run across.

Steve

>
>
> On Tue, 30 Nov 2004 08:33:06 -0500, Scott Stirling
> <scottstirling-KealBaEQdz4@xxxxxxxxxxxxxxxx> wrote:
> > Awesome! Thanks to all for the help.
> >
> > Should I add this info to the Wiki somewhere? Just occurred
> to me . . .
> >
> > Scott S.
> >
> >
> >
> > On Nov 30, 2004, at 1:20 AM, Marc Hedlund wrote:
> >
> > >
> > > Scott,
> > >
> > > In addition to the answers you've already gotten, I would also
> > > recommend
> > > using closures where you would have used a for loop. each() and
> > > eachWithIndex() replace most for loops in my scripts.
> > >
> > > Some examples follow...
> > >
> > > /**********************/
> > >
> > > stringList = [ "java", "perl", "python", "ruby", "c#", "cobol",
> > > "groovy", "jython", "smalltalk", "prolog",
> "m", "yacc"
> > > ];
> > >
> > > stringMap = [ "Su" : "Sunday", "Mo" : "Monday", "Tu" : "Tuesday",
> > > "We" : "Wednesday", "Th" : "Thursday",
> "Fr" : "Friday",
> > > "Sa" : "Saturday" ];
> > >
> > > stringList.each() { print " ${it}" }; println "";
> > > // java perl python ruby c# cobol groovy jython smalltalk
> prolog m yacc
> > >
> > > stringMap.each() { key, value | println "${key} == ${value}" };
> > > // Su == Sunday
> > > // We == Wednesday
> > > // Mo == Monday
> > > // Sa == Saturday
> > > // Th == Thursday
> > > // Tu == Tuesday
> > > // Fr == Friday
> > >
> > > stringList.eachWithIndex() { obj, i | println " ${i}: ${obj}" };
> > > // 0: java
> > > // 1: perl
> > > // 2: python
> > > // 3: ruby
> > > // 4: c#
> > > // 5: cobol
> > > // 6: groovy
> > > // 7: jython
> > > // 8: smalltalk
> > > // 9: prolog
> > > // 10: m
> > > // 11: yacc
> > >
> > > stringMap.eachWithIndex() { obj, i | println " ${i}: ${obj}" };
> > > // 0: Su=Sunday
> > > // 1: We=Wednesday
> > > // 2: Mo=Monday
> > > // 3: Sa=Saturday
> > > // 4: Th=Thursday
> > > // 5: Tu=Tuesday
> > > // 6: Fr=Friday
> > >
> > >
> > >
> > > Marc Hedlund
> > > e: marc at precipice dot org
> > >
> > > On Tue, 30 Nov 2004, Scott Stirling wrote:
> > >
> > >> Hi,
> > >>
> > >> Is there a Groovy idiom (with a counter) to use instead
> of a standard
> > >> 'for' loop? I need to use the counter from the loop iterations.
> > >>
> > >> I get this exception when I code a standard 'for' loop
> (i.e., for (int
> > >> i =0; i < foo.size(); i++):
> > >>
> > >> Exception in thread "main" NOT YET IMPLEMENTED: standard for loop
> > >> at
> > >>
> org.codehaus.groovy.syntax.parser.ASTBuilder.forStatement(ASTBuilder.j
> > >> av
> > >> a:1026)
> > >> at
> > >>
> org.codehaus.groovy.syntax.parser.ASTBuilder.statement(ASTBuilder.java
> > >> :
> > >> 773)
> > >> at
> > >>
> org.codehaus.groovy.syntax.parser.ASTBuilder.topLevelStatement(ASTBuil
> > >> de
> > >> r.java:327)
> > >> at
> > >>
> org.codehaus.groovy.syntax.parser.ASTBuilder.build(ASTBuilder.java:
> > >> 198)
> > >> at
> > >>
> org.codehaus.groovy.control.SourceUnit.convert(SourceUnit.java:339)
> > >>
> > >> Thank you,
> > >> Scott Stirling
> > >> Framingham, MA
> > >>
> > >
> >
> >




<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise