Message from discussion
for loops for non-iterables?
Received: by 10.50.6.167 with SMTP id c7mr5949763iga.1.1334181578998;
Wed, 11 Apr 2012 14:59:38 -0700 (PDT)
X-BeenThere: gosu-lang@googlegroups.com
Received: by 10.50.151.199 with SMTP id us7ls5355462igb.1.canary; Wed, 11 Apr
2012 14:59:38 -0700 (PDT)
MIME-Version: 1.0
Received: by 10.68.136.106 with SMTP id pz10mr205188pbb.16.1334181578067; Wed,
11 Apr 2012 14:59:38 -0700 (PDT)
Authentication-Results: ls.google.com; spf=pass (google.com: domain of
mickey.killia...@gmail.com designates internal as permitted sender)
smtp.mail=mickey.killia...@gmail.com; dkim=pass
header...@gmail.com
Received: by w6g2000pbp.googlegroups.com with HTTP; Wed, 11 Apr 2012 14:59:38
-0700 (PDT)
Date: Wed, 11 Apr 2012 14:59:38 -0700 (PDT)
In-Reply-To: <CABQpmAZOG-s64CWRAy+-zEoaWuieEWNp-vJNrF8NFA+yg45Ufg@mail.gmail.com>
References: <CACAUoX69+__H0FO_JMm3+mVGyzNUKrXq3UkXtqG7wNXi7J2etw@mail.gmail.com>
<CAO92UoHSt5RR+6M3N6=SD-e6q8+OUgjivL9P43PM6yoRXHU=dA@mail.gmail.com>
<CAO92UoHAXAk5g5TadCz+yLwtmHcn5SfgnJoqM2iTf5345S2g0w@mail.gmail.com>
<CACAUoX6X+DjJD3GiTkPxgw9LAd8nR3k+Fmm-hTiTzVcdjySPmA@mail.gmail.com>
<b90593cf-f1b5-428d-90ca-1e4b1325ad91@b2g2000yqb.googlegroups.com>
<CACAUoX5P7cfMF6BS6eSTo5aSiiMCj-dokZYWJBgaKpmOo1iMXA@mail.gmail.com>
<CAAsfsNLH9scJ8-AE2KHpgfKtE=W4ax5a9=idBNXJF9wS1n4muQ@mail.gmail.com>
<2d3249a1-48da-469c-868b-12c2d7f4565f@f37g2000yqc.googlegroups.com>
<CACAUoX5hUnti_MDj-GrZ08CNKm-iukt9PmzpZcP3y4MDYuE_kA@mail.gmail.com>
<5f7d81cc-bdee-48e0-a39e-11ab4b0995dd@s10g2000pbc.googlegroups.com> <CABQpmAZOG-s64CWRAy+-zEoaWuieEWNp-vJNrF8NFA+yg45Ufg@mail.gmail.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML,
like Gecko) Ubuntu/11.10 Chromium/18.0.1025.142 Chrome/18.0.1025.142 Safari/535.19,gzip(gfe)
Message-ID: <20c6e8df-357d-4ec4-bab7-3c74c0324409@w6g2000pbp.googlegroups.com>
Subject: Re: for loops for non-iterables?
From: Mick Killianey <mickey.killia...@gmail.com>
To: gosu-lang <gosu-lang@googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Peter
Glad to hear that you enjoyed that puzzler. Now that readers of this
thread are experts on intervals, hopefully everyone will get this
follow-on puzzler correct:
Q: What does the following code print?
var interval =3D (2..0).step(-1)
print(interval)
for (i in interval) {
print(i)
}
Does it...
(1) ...iterate over the interval in steps of -1, containing three
items:
2..0 step -1
2
1
0
(2) ...iterate over three items (as in answer 1), *but* printing the
step in the first line (the toString() representation) as a *positive*
value:
2..0 step 1
2
1
0
(3) ...invert the step, looping in the positive direction.
2..0 step -1
2
3
4
...etc...
(4) ...abort after the first element.
2..0 step -1
2
(5) ...throw an exception in construction at runtime, since you can't
create an interval with a negative step?
As before, answer at the very end of this message.
On Apr 11, 9:25=A0am, Peter Rexer <pre...@alum.mit.edu> wrote:
> Let me throw on the former Product-Manager for Gosu hat and weigh in with
> my admittedly "I wasn't a CS major" opinion.
>
> I applaud your puzzle. =A0 Great puzzler.
> Your example stumped me. =A0Just reading it, I was expecting option 3... =
but
> I was immediately suspicious of you defining a variable called 'interval'
> and of the non-compact syntax. =A0I've grown used to much more compact sy=
ntax
> like:
>
> for (i in 1..n){
> =A0 //something about i and n
>
> }
>
> IMHO, ideally, if the tooling was able to read your mind, you'd have been
> warned that 'interval' wasn't a proper interval to use in the for loop.
> Not sure if the parser could easily detect that, as it looks a lot like
> quasi functional programming to me, and that stuff confuses my old brain.
>
> Regarding whether you should split this into 3 threads.... please.
>
> -Peter
>
> On Tue, Apr 10, 2012 at 4:54 PM, Scott McKinney <rsmckin...@hotmail.com>w=
rote:
>
>
>
>
>
>
>
> > 1. Well, I haven't seen enough of a case establishing the few
> > instances where while loops are used as significant enough to justify
> > bringing back a C-style loop statement; it doesn't really provide much
> > of an edge, if one at all. =A0And while loops aren't that bad
> > (preferable IMO) esp. considering the very few cases that you'll need
> > to fall back on one in Gosu. =A0Moreover, Gosu just isn't Java; it has
> > it's own way of doing things and we tend to lean toward improving on
> > those things.
>
> > 2. I'm sorry you don't like Gosu's intervals. =A0They were vetted
> > internally and reviewed by anyone in our dev org who cared to
> > contribute and so far most people seem to like them.
>
> > 3. While Gosu has a "community" release, there are no plans for a Java-
> > like "community process" aka JCP. =A0If anything the JCP has proven to
> > be a detriment to the advancement of Java, so we're kind of CP-averse
> > over here. This is not to say we don't listen to sincere feedback on
> > the language and tools, we absolutely do, we need it, we count on it,
> > and it's already helped us advance the language.. =A0But like a lot of
> > other programming languages (C# and Python come to mind), there are a
> > few guys who write and maintain its core and one guy who tries to keep
> > it consistent. =A0In the near future we'll be releasing the rest of
> > Gosu's internals as well in hopes that we'll receive more feedback.
> > At some point we'll be in shape so that the whole thing will be OS and
> > forkable. =A0We're just not there yet.
>
> > On Apr 10, 3:50 pm, Mickey Killianey <mic...@killianey.com> wrote:
> > > It sounds like this conversation has reached a branching point, where
> > there
> > > are three topics:
>
> > > =A0 =A01. Discussion of for-loops (specifically, a request for adding=
the
> > > =A0 =A0C-style syntax).
> > > =A0 =A02. Discussion of the design of intervals (specifically, a requ=
est to
> > > =A0 =A0remove them as error-prone).
> > > =A0 =A03. Discussion of the "community" aspect of a "community-releas=
ed"
> > > =A0 =A0language. =A0Specifically, what's the process for auditing Gos=
u language
> > > =A0 =A0features? =A0How is community feedback gathered and reviewed? =
=A0What's
> > the
> > > =A0 =A0process for deciding upon the addition or removal of a languag=
e
> > feature?
> > > =A0 =A0 And where are conversations like this archived, so that when =
someone
> > pipes
> > > =A0 =A0up to say "Hey, why is this feature X done this way?" we can p=
oint to
> > a
> > > =A0 =A0coherent, discussion thread where we weigh pros and cons?
>
> > > Should we start new threads for these? =A0I'm interested in all three=
.
>
> > > mk
>
> > > On Tue, Apr 10, 2012 at 12:33 PM, Scott McKinney <rsmckin...@hotmail.=
com
> > >wrote:
>
> > > > Sorry to hear that.
>
> > > > First, I think you'd write it this way:
>
> > > > for( i in 0..9 ) { ... }
>
> > > > Which seems intuitive regardless of your background. =A0 And really=
, if
> > > > you're honest with yourself, I think you'll admit that's more
> > > > intuitive from first principles than the C-style way. Which is the
> > > > whole point. =A0If Gosu provides the C-style way, Java programmers =
will
> > > > use it like a crutch. =A0Fwiw, I am still a little torn on providin=
g
> > > > it. =A0On the one hand, absolutely, a lot of potential Gosu users a=
re
> > > > current Java programmers and they will feel more at home as Gosu fe=
els
> > > > more like Java. =A0But the point of any new language is to make mar=
ked
> > > > improvements over the status quo and it's kind of difficult to clai=
m
> > > > an improvement when you're carrying the luggage of your forebearer.
> > > > Maybe you don't like the concept of intervals, but that's Gosu's wa=
y.
> > > > I think if you give them a chance, you may change your position, or
> > > > maybe not.
>
> > > > Again, I still there's room for improvement, perhaps building on th=
e
> > > > interval/iterator-style looping. =A0Given you can extend the set of
> > > > iterables and sequenceables programmatically, there's probably
> > > > something that can be done to support a broader set of use-cases.
> > > > Your ideas are welcome.
>
> > > > Scott
>
> > > > On Apr 10, 11:45 am, Alan Keefer <akee...@gmail.com> wrote:
> > > > > I have to agree that I find the interval syntax horrendously
> > > > > confusing; for anyone coming from C/Java, who's used to:
>
> > > > > for (int i =3D 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 ju=
st
> > > > > want to iterate over the elements in some array/collection/whatev=
er,
> > > > > but when I do really just want to loop a fixed number of times fo=
r
> > > > > some reason, I find the Gosu syntax harder to get right than the =
Java
> > > > > syntax. =A0Perhaps if Gosu was the only language I knew, that wou=
ld 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 ha=
ve a
> > > > > Java/C-style for loop available.
>
> > > > > -Alan
>
> > > > > On Tue, Apr 10, 2012 at 11:25 AM, Mickey Killianey <
> > mic...@killianey.com>
> > > > wrote:
> > > > > > Ugh. =A0I spent a few hours last night using the latest Gosu pl=
ugin
> > for
> > > > > > IntelliJ and had an awful, terrible time, so pardon me if I'm
> > really
> > > > down on
> > > > > > Gosu today. =A0(As a warmup, I was trying to solve the Google c=
odejam
> > > > > > problems, and the plugin kept crashing every few minutes. =A0I =
tried
> > to
> > > > use
> > > > > > the built-in graphic editor and it, too, crashed often.
>
> > > > > >> More recently we added support for interval iteration. =A0This
> > style of
> > > > > >> looping hammers another nail in the C-style looping coffin:
>
> > > > > > At this point, that seems like a dramatic overreach. =A0The C-s=
tyle
> > > > looping
> > > > > > syntax isn't dead...it's simple, useful, and easy to understand=
.
>
> > > > > > I experimented with the Gosu interval last night, and my experi=
ence
> > > > with it
> > > > > > was the opposite of simple, useful, and easy to understand. =A0=
The
> > syntax
> > > > > > introduces weird edge cases that are unpredictable, inconsisten=
t,
> > and
> > > > > > error-prone.
>
> > > > > > As an example of how unpredictable Gosu is, here's a puzzler...=
I
> > > > challenge
> > > > > > readers to identify the result *before* running this code snipp=
et:
>
> > > > > > Problem:
>
> > > > > > function countUpTo(n : int) {
>
> > > > > > =A0 var interval =3D 1..n
>
> > > > > > =A0 print(interval)
>
> > > > > > =A0 for (i in interval) {
>
> > > > > > =A0 =A0 print(i)
>
> > > > > > =A0 =A0 =A0 =A0}
>
> > > > > > }
>
> > > > > > countUpTo(0)
>
> > > > > > Possible solutions:
> > > > > > (1) =A0The interval of 1 to 0 is an empty iterable, so it just =
prints
> > > > out the
> > > > > > interval:
>
> > > > > > 1..0 step 1
>
> > > > > > (2) =A0The interval of 1 to 0 includes the startpoint (1), but =
not
> > the
> > > > > > endpoint (0) since 1 > 0.
>
> > > > > > 1..0 step 1
>
> > > > > > 1
>
> > > > > > (3) =A0The code counts from 1 to 0, but identifies at runtime t=
hat 0
> > is
> > > > less
> > > > > > than 1 and decides to count backwards with step -1:
>
> > > > > > 1..0 step -1
>
> > > > > > 1
>
> > > > > > 0
>
> > > > > > (4) =A0The code cannot create an interval because the start (1)=
is
> > > > greater
> > > > > > than the end (0), so it throws a runtime exception.
> > > > > > (5) =A0None of the above
>
> > > > > > (I'll put the answer at the end)
>
> > > > > > And an example of inconsistencies in the design of the index an=
d
> > > > iterator
> > > > > > clauses, this example loops four times:
>
> > > > > > for (var word in {"foo", "bar", "baz", "qux"} index i) {
> > > > > > =A0 print(word)
> > > > > > =A0 i =3D i+1 // skip next word
> > > > > > }
>
> > > > > > While this example loops only twice:
>
> > > > > > for (var word in {"foo", "bar", "baz", "qux"} iterator i) {
> > > > > > =A0 print(word)
> > > > > > =A0 i.next() // skip next word
> > > > > > }
>
> > > > > > Finally, the language support is woefully incomplete, since thi=
s
> > simple
> > > > > > example is a compile error in the latest Gosu:
>
> > > > > > for (r in 0.0 .. Math.PI step 0.01) {
> > > > > > =A0 print(r)
> > > > > > }
>
> > > > > > If Gosu is serious about moving forward as a community-based
> > language,
> > > > we
> > > > > > should have a community-based discussion about language feature=
s to
> > > > see what
> > > > > > the community needs. =A0After playing around with this last nig=
ht, I
> > > > found the
> > > > > > interval syntax to be incredibly error-prone and really, really
> > wanted
> > > > the
> > > > > > simplicity and predictability of C-style loops. =A0Perhaps that
> > would be
> > > > a
> > > > > > good first topic to kickstart the "community" part of "communit=
y
> > Gosu"?
>
> > > > > > And lest we forget...Spoiler alert: =A0Puzzle answer below...
>
> > > > > > The answer for the puzzler is "E: =A0None of the Above." =A0The
> > interval is
> > > > > > constructed from 1..0 with step 1, but when you actually iterat=
e
> > over
> > > > the
> > > > > > interval, in *spite* of the step being positive (1), it counts =
in
> > the
>
> ...
>
SPOILER ALERT!
The answer is (3): Despite calling step(-1), this interval loops in
the *positive* direction, so it loops through values 2, 3, 4, ...
Followup puzzler: How long does it continue to loop? Does it loop
until...
(1) ...it hits Integer.MIN_VALUE.
(2) ...it hits Integer.MAX_VALUE.
(3) ...it overflows the int and wraps around, eventually hitting
zero.
(4) ...you kill the process. The loop index is a Number, which is
represented by a BigDecimal and grows as large as necessary.
(5) ...it runs out of memory. Intervals cache the values they
returns, so running this loop actually runs your JVM out of memory.
(The answer to this puzzler is left as an exercise for the reader.)