for (initialization; condition; increment) {
statement(s)
}
for (double x = 0.0; x < r; x += step) {for (double y = 0.0; x*x + y*y < r*r; y += step) {// do something inside circle, including (possibly) modifying step.}}
for (int i = 0; i < args.length; i++) {if (arg[i].equals("-h") {printHelp()break;}if (arg[i].equals("-f") {i++; // grab next arg after "-f" as filenamefilename = arg[i]}}
--
You received this message because you are subscribed to the Google Groups "gosu-lang" group.
To post to this group, send email to gosu...@googlegroups.com.
To unsubscribe from this group, send email to gosu-lang+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gosu-lang?hl=en.
var list = { "one", "two", "three" } for ( num in list iterator iter ) { iter.remove() }
More recently we added support for interval iteration. This style of
looping hammers another nail in the C-style looping coffin:
Problem:function countUpTo(n : int) {var interval = 1..nprint(interval)for (i in interval) {}print(i)}countUpTo(0)Possible solutions:(1) The interval of 1 to 0 is an empty iterable, so it just prints out the interval:1..0 step 1(2) The interval of 1 to 0 includes the startpoint (1), but not the endpoint (0) since 1 > 0.1..0 step 11(3) The code counts from 1 to 0, but identifies at runtime that 0 is less than 1 and decides to count backwards with step -1:1..0 step -110(4) The code cannot create an interval because the start (1) is greater than the end (0), so it throws a runtime exception.(5) None of the above
for (var word in {"foo", "bar", "baz", "qux"} index i) {print(word)i = i+1 // skip next word}
for (var word in {"foo", "bar", "baz", "qux"} iterator i) {print(word)i.next() // skip next word}
for (r in 0.0 .. Math.PI step 0.01) {print(r)}
1..0 step 1
1
What makes this even more unexpected is that you can ask the interval for its step, and it'll *still* tell you that it's going in the positive direction. For example:0
print((1..0).Step)
for (int i = 0; i < 10; i++) { // do stuff }
Doing:
for (i in 0..|10) { // do stuff }
is not at all intuitive or easy to read, and it's easy to forget if
you should do 0..10 or 0..|10 and to do the wrong one.
I don't find myself doing that all that often, since usually I just
want to iterate over the elements in some array/collection/whatever,
but when I do really just want to loop a fixed number of times for
some reason, I find the Gosu syntax harder to get right than the Java
syntax. Perhaps if Gosu was the only language I knew, that would be
one thing, but for anyone coming from a Java background (which is
pretty much everyone that we'd want to have use Gosu), using intervals
in the for loops is pretty confusing, and I'd much rather just have a
Java/C-style for loop available.
-Alan
(0..10) and using one of the collection looping methods (each,
eachWithIndex, filter,etc..)
But I like blocks and chaining. At least with the block there's less
things I need to think about - What happens if I increment the index
in my block - nothing it doesn't affect the looping at all. Granted I
can't break out of the loop, but if I need to do things like that, a
for loop is the wrong construct anyways and a while() loop would be
better (IMO in most cases).
In the few cases working with Gosu where I wasn't looping over an
iterator and I wanted a for loop, I got frustrated for 10 seconds and
figured out a more 'Gosu' way of writing it, either by converting the
'thing' into some sort of iterator or using a while loop.
My opinion is that instead of trying to write Gosu the Java or C way,
we should write Gosu the Gosu way. I guess its up to us (or the
language designers) to really figure out what that way is.
So, that's a long winded response to "I don't miss for loops in Gosu
and would rather leave them on the never-do pile."
Chris
I wasn't considering participating in the Google Code Jam. See projects above :)
Chris
here are the docs on the getStep() method on IIterableInterval, where
step() and getStep() (i.e. the Step property in Gosu) are defined:
/**
* . . .
* Note if non-null, the step is a <i>positive</i> (or absolute)
increment. To iterate the interval
* in reverse order use iterateFromRight().
*/
Since intervals can be reversed and iterated in either direction, it
makes sense for step to be an absolute value so that the interval can
function properly when going either direction.
However, there's an implementation bug, such that step(-1) neither
converts the int to an absolute value nor throws an exception; as a
result, the runtime behavior is incorrect: the implementation assumes
the step is an absolute value, and since 2..0 is a reversed interval,
it subtracts the step every time. 2..0.step(1) should work as you
expect, and count down 2 1 0. step(-1) should either throw or behave
the same as step(1), and we just need to fix the implementation there.
So chalk this particular puzzle up as a bug (and please go ahead and
file it as such)
-Alan
On Wed, Apr 11, 2012 at 2:59 PM, Mick Killianey
<mickey.k...@gmail.com> wrote: