The latest Java Posse episode (<a href="http://javaposse.com/ java_posse_317_newscast_for_july_29th_2010">#317</a>) features a short
segment (start around 51:45) of the posse talking about the <a
href="http://strangeloop2010.com">Strange Loop conference</a>. The
Java Posse are giving away four FREE (as in beer) tickets based on a
contest.
The contest is: "Write the strangest loop" (any language is ok). You
should post your answer here and list the number (out of 4) you
possibly could use. The answer will be judged by the posse (and
me!).
I'm not sure what the deadline for the contest will be but we'll post
that here.
If I were to win, I'd only need one ticket. I'm not sure if this
counts (it might be more appropriate for the Daily WTF), but I once
saw Java code that looked like the following (I don't remember what
was inside the if blocks):
for (int i = 0; i < 4; i++) {
if (i == 0) {
//some code to handle case 0
} else if (i == 1) {
//some code to handle case 1
} else if (i == 2) {
//some code to handle case 2
} else if (i == 3) {
//some code to handle case 3
} else if (i == 4) {
//some code to handle case 4
}
}
I was going to make fun of whoever had written it and ask why on earth
it had been structured as a loop, but I lost my nerve when the version
control system revealed that it had been written by the head of the
engineering team for the small startup I was at. I think I'd be more
courageous about calling him on it at this point. :)
For wacky loop madness, it doesn't get much stranger than loop unrolling in C/C++ So I present to you... <drum roll> ...Duff's Device! (lifted direct from Wikipedia)
send(to, from, count)register short *to, *from;register count;{ register n=(count+7)/8; switch(count%8){ case 0: do{ *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; }while(--n>0); }}
Such elegance! Such conciseness!
Why oh why did I ever allow Scala, with it's complexity and ugly, ugly functional constructs to enter my life? It's nothing but a virtuous life of clean, simple, comprehensible imperative code for me from now on!
On 4 August 2010 01:09, Vince O'Sullivan <vjosulli...@gmail.com> wrote:
> for (Strange loop : loops) > if (loop.isStrange()) > continue;
> On Aug 3, 6:23 pm, Alex <alexdmil...@yahoo.com> wrote: > > The deadline for the contest will be Aug. 31st! Give us your loops!
> > Alex
> -- > You received this message because you are subscribed to the Google Groups > "The Java Posse" group. > To post to this group, send email to javaposse@googlegroups.com. > To unsubscribe from this group, send email to > javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com> > . > For more options, visit this group at > http://groups.google.com/group/javaposse?hl=en.
On Tue, Aug 3, 2010 at 5:09 AM, Alex <alexdmil...@yahoo.com> wrote: > The latest Java Posse episode (<a href="http://javaposse.com/ > java_posse_317_newscast_for_july_29th_2010">#317</a>) features a short > segment (start around 51:45) of the posse talking about the <a > href="http://strangeloop2010.com">Strange Loop conference</a>. The > Java Posse are giving away four FREE (as in beer) tickets based on a > contest.
> The contest is: "Write the strangest loop" (any language is ok). You > should post your answer here and list the number (out of 4) you > possibly could use. The answer will be judged by the posse (and > me!).
As you have the do {} inside the first case statement, and all other case's are inside the do. Since there's not in scope of the case I'd expect this to fail? But then - I'm no C/C++ guy so who knows what wacky hackery they get up to :)
-- Pull me down under...
On Wed, Aug 4, 2010 at 12:25 PM, Kevin Wright <kev.lee.wri...@gmail.com>wrote:
> For wacky loop madness, it doesn't get much stranger than loop unrolling in > C/C++ > So I present to you... <drum roll> ...Duff's Device! > (lifted direct from Wikipedia)
> send(to, from, count)register short *to, *from;register count;{ > register n=(count+7)/8; > switch(count%8){ > case 0: do{ *to = *from++; > case 7: *to = *from++; > case 6: *to = *from++; > case 5: *to = *from++; > case 4: *to = *from++; > case 3: *to = *from++; > case 2: *to = *from++; > case 1: *to = *from++; > }while(--n>0); > }}
> Such elegance! > Such conciseness!
> Why oh why did I ever allow Scala, with it's complexity and ugly, ugly > functional constructs to enter my life? > It's nothing but a virtuous life of clean, simple, > comprehensible imperative code for me from now on!
> On 4 August 2010 01:09, Vince O'Sullivan <vjosulli...@gmail.com> wrote:
>> for (Strange loop : loops) >> if (loop.isStrange()) >> continue;
>> On Aug 3, 6:23 pm, Alex <alexdmil...@yahoo.com> wrote: >> > The deadline for the contest will be Aug. 31st! Give us your loops!
>> > Alex
>> -- >> You received this message because you are subscribed to the Google Groups >> "The Java Posse" group. >> To post to this group, send email to javaposse@googlegroups.com. >> To unsubscribe from this group, send email to >> javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com> >> . >> For more options, visit this group at >> http://groups.google.com/group/javaposse?hl=en.
> -- > You received this message because you are subscribed to the Google Groups > "The Java Posse" group. > To post to this group, send email to javaposse@googlegroups.com. > To unsubscribe from this group, send email to > javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com> > . > For more options, visit this group at > http://groups.google.com/group/javaposse?hl=en.
user=> (trampoline pure-loop 20)
pure 20
danger 19
pure 18
danger 17
pure 16
danger 15
pure 14
danger 13
pure 12
danger 11
pure 10
danger 9
pure 8
danger 7
pure 6
danger 5
pure 4
danger 3
pure 2
danger 1
pure 0
It's strange because, well, to use trampoline to do a decreasing loop
is very strange indeed.
I'll be speaking there so I don't need a pass but thought it might be fun to put in a puzzler.
public class StrangeLoop { public static void main(String[] args) { http://www.thestrangeloop.com do { System.out.println("Strange Loop"); continue http; } while (false); }
`http:` is the label (followed by a comment that the pre-processor strips out) `do { ... } while (...)` is the labelled statement `continue <label>` attempts to transfer control back to the "continue target". In this case, the labelled do/while loop
a `do {statement} while (expression)` loops will check its expression only once the statement has executed normally (which it hasn't here) So... the loop will begin again, and continue infinitely, or until an exception is thrown or the JVM is terminated.
No stack involved, at the level of compiled machine code it'll be jump instructions all the way down...
For anyone still labouring under the delusion that Java is a simple language, it's a heavyweight document; running to 650 pages in almost 8MB but you only really need chapter 14 for this question :)
On 4 August 2010 05:43, Kirk <kirk.pepperd...@gmail.com> wrote:
> I'll be speaking there so I don't need a pass but thought it might be fun > to put in a puzzler.
> public class StrangeLoop { > public static void main(String[] args) { > http://www.thestrangeloop.com > do { > System.out.println("Strange Loop"); > continue http; > } while (false); > } > }
> Puzzler, infinite loop or normal termination?
> Regards, > Kirk
> -- > You received this message because you are subscribed to the Google Groups > "The Java Posse" group. > To post to this group, send email to javaposse@googlegroups.com. > To unsubscribe from this group, send email to > javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com> > . > For more options, visit this group at > http://groups.google.com/group/javaposse?hl=en.
I tried to go for a strange "Strange loop" ( http://en.wikipedia.org/wiki/Strange_loop)...but I ended up with a recursive loop of doom. Please forgive the quick thrown-together implementation =)
(2 passes, please =)
Given a "normal" string, this app would loop through and resolve the acronym with the full break out and put the acronym in parenthesis after it. Given recursive acronyms, all hell breaks loose.
/** * Lets make that String understandable to management.... */ public class Main {
public static void main(String[] args) {
//replace var test with cli/arg input String test = "In addition to java you can write plugins for the " + "GNU tool Nagios using PHP"; while (AcronymFinder.hasAcronym(test)) { test = AcronymFinder.explodeAcronym(test); System.out.println(test); } }
private static class AcronymFinder {
private static Pattern p = Pattern.compile("([a-zA-Z]*)[\\s|\\.]"); /** * Takes in a String and checks it against the acronym dictionary, * resolving any acronyms in the process * * @param raw * @return */ public static String explodeAcronym(String raw) { String out = null;
Matcher m = p.matcher(raw);
StringBuilder sb = new StringBuilder();
while (m.find()) { try { Acronyms a = Acronyms.valueOf(m.group(1)); sb.append(a.resolve()); sb.append(" (" + m.group(1) + ")"); //the parens prevents the acronym from getting picked up twice, due to regex impl } catch (IllegalArgumentException e) { sb.append(m.group(1)); } sb.append(" "); } if (sb.length() > 0) { out = sb.toString(); } else { out = raw; } return out; }
public static boolean hasAcronym(String raw) { Matcher m = p.matcher(raw);
> `http:` is the label (followed by a comment that the pre-processor strips > out) > `do { ... } while (...)` is the labelled statement > `continue <label>` attempts to transfer control back to the "continue > target". In this case, the labelled do/while loop
> a `do {statement} while (expression)` loops will check its expression only > once the statement has executed normally (which it hasn't here) > So... the loop will begin again, and continue infinitely, or until an > exception is thrown or the JVM is terminated.
> No stack involved, at the level of compiled machine code it'll be jump > instructions all the way down...
> For anyone still labouring under the delusion that Java is a simple > language, it's a heavyweight document; running to 650 pages in almost 8MB > but you only really need chapter 14 for this question :)
> On 4 August 2010 05:43, Kirk <kirk.pepperd...@gmail.com> wrote:
>> I'll be speaking there so I don't need a pass but thought it might be fun >> to put in a puzzler.
>> public class StrangeLoop { >> public static void main(String[] args) { >> http://www.thestrangeloop.com >> do { >> System.out.println("Strange Loop"); >> continue http; >> } while (false); >> } >> }
>> Puzzler, infinite loop or normal termination?
>> Regards, >> Kirk
>> -- >> You received this message because you are subscribed to the Google Groups >> "The Java Posse" group. >> To post to this group, send email to javaposse@googlegroups.com. >> To unsubscribe from this group, send email to >> javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com> >> . >> For more options, visit this group at >> http://groups.google.com/group/javaposse?hl=en.
> -- > You received this message because you are subscribed to the Google Groups > "The Java Posse" group. > To post to this group, send email to javaposse@googlegroups.com. > To unsubscribe from this group, send email to > javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com> > . > For more options, visit this group at > http://groups.google.com/group/javaposse?hl=en.
> For anyone still labouring under the delusion that Java is a > simple language, it's a heavyweight document; running to 650 pages > in almost 8MB but you only really need chapter 14 for this question > :)
Absolute figures are mostly worthless... comparisons help. How long are the specs of Scala? Of course, I presume it might also depend on the writing style.
- -- Fabrizio Giudici - Java Architect, Project Manager Tidalwave s.a.s. - "We make Java work. Everywhere." java.net/blog/fabriziogiudici - www.tidalwave.it/people Fabrizio.Giud...@tidalwave.it -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.14 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
Yes, Mark, it does. C works in mysterious ways, and this is just one
in a long, possibly endless, procession of them. Duff's device is one
of those hallowed "WTF? Oh.. Ooooh! I get it! Whoever cooked this up
is an evil genius!!!" optimizations useful in the early days of
computing. The wikipedia page has a lengthy treatment on the topic,
but the basic mechanics behind why this is legal in C is this:
1. A switch statement's body is simply like any other block, with as
any extra that you can shove any number of case statements in it.
2. switch works by jumping to the right case statement.
3. In C, you may jump right into the middle of a loop, without
breaking things.
Most folks would say that in java you can only jump out of LOOPS,
which explains why it doesn't work, but this isn't true. It doesn't
work in java because, switch blocks are defined as containing a 0-n
set of (case/default statement followed by 0-n statements), whereas in
C its defined as containing 0-n statements, and case/default are also
statements. You CAN in fact jump around indiscriminately in java code
and it works just fine. Behold this code:
which will print "Hello, Universe!" if your system clock is set later
than Jan 1st, 1970, and causes no compilation problems at all. This
indiscriminate breaking out of any code block, not just those
representing loops, is in fact a holdover from C, where 'break' in a
switch statement, which was after all defined just as a glorified goto
form, required this. In java this isn't necessary (switch being
differently defined), but nevertheless you can break out of whatever
you want, loop or no.
Which, I guess, is my entry for 'strange loop': It's so strange, one
could argue it's not a loop at all!
NB: I don't need a ticket, I can't make the date, but I couldn't
resist :P
On Aug 4, 3:23 am, Mark Derricutt <m...@talios.com> wrote:
> As you have the do {} inside the first case statement, and all other case's
> are inside the do. Since there's not in scope of the case I'd expect
> this to fail? But then - I'm no C/C++ guy so who knows what wacky hackery
> they get up to :)
> --
> Pull me down under...
> On Wed, Aug 4, 2010 at 12:25 PM, Kevin Wright <kev.lee.wri...@gmail.com>wrote:
> > For wacky loop madness, it doesn't get much stranger than loop unrolling in
> > C/C++
> > So I present to you... <drum roll> ...Duff's Device!
> > (lifted direct from Wikipedia)
> > send(to, from, count)register short *to, *from;register count;{
> > register n=(count+7)/8;
> > switch(count%8){
> > case 0: do{ *to = *from++;
> > case 7: *to = *from++;
> > case 6: *to = *from++;
> > case 5: *to = *from++;
> > case 4: *to = *from++;
> > case 3: *to = *from++;
> > case 2: *to = *from++;
> > case 1: *to = *from++;
> > }while(--n>0);
> > }}
> > Such elegance!
> > Such conciseness!
> > Why oh why did I ever allow Scala, with it's complexity and ugly, ugly
> > functional constructs to enter my life?
> > It's nothing but a virtuous life of clean, simple,
> > comprehensible imperative code for me from now on!
> > On 4 August 2010 01:09, Vince O'Sullivan <vjosulli...@gmail.com> wrote:
> >> for (Strange loop : loops)
> >> if (loop.isStrange())
> >> continue;
> >> On Aug 3, 6:23 pm, Alex <alexdmil...@yahoo.com> wrote:
> >> > The deadline for the contest will be Aug. 31st! Give us your loops!
> >> > Alex
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "The Java Posse" group.
> >> To post to this group, send email to javaposse@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/javaposse?hl=en.
> > --
> > You received this message because you are subscribed to the Google Groups
> > "The Java Posse" group.
> > To post to this group, send email to javaposse@googlegroups.com.
> > To unsubscribe from this group, send email to
> > javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/javaposse?hl=en.
> `http:` is the label (followed by a comment that the pre-processor strips
> out)
> `do { ... } while (...)` is the labelled statement
> `continue <label>` attempts to transfer control back to the "continue
> target". In this case, the labelled do/while loop
> a `do {statement} while (expression)` loops will check its expression only
> once the statement has executed normally (which it hasn't here)
> So... the loop will begin again, and continue infinitely, or until an
> exception is thrown or the JVM is terminated.
> No stack involved, at the level of compiled machine code it'll be jump
> instructions all the way down...
> For anyone still labouring under the delusion that Java is a simple
> language, it's a heavyweight document; running to 650 pages in almost 8MB
> but you only really need chapter 14 for this question :)
> On 4 August 2010 05:43, Kirk <kirk.pepperd...@gmail.com> wrote:
> > I'll be speaking there so I don't need a pass but thought it might be fun
> > to put in a puzzler.
> > public class StrangeLoop {
> > public static void main(String[] args) {
> > http://www.thestrangeloop.com > > do {
> > System.out.println("Strange Loop");
> > continue http;
> > } while (false);
> > }
> > }
> > Puzzler, infinite loop or normal termination?
> > Regards,
> > Kirk
> > --
> > You received this message because you are subscribed to the Google Groups
> > "The Java Posse" group.
> > To post to this group, send email to javaposse@googlegroups.com.
> > To unsubscribe from this group, send email to
> > javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/javaposse?hl=en.
> On Aug 4, 11:01 am, Kevin Wright <kev.lee.wri...@gmail.com> wrote: > > my thinking:
> > `http:` is the label (followed by a comment that the pre-processor strips > > out) > > `do { ... } while (...)` is the labelled statement > > `continue <label>` attempts to transfer control back to the "continue > > target". In this case, the labelled do/while loop
> > a `do {statement} while (expression)` loops will check its expression > only > > once the statement has executed normally (which it hasn't here) > > So... the loop will begin again, and continue infinitely, or until an > > exception is thrown or the JVM is terminated.
> > No stack involved, at the level of compiled machine code it'll be jump > > instructions all the way down...
> > For anyone still labouring under the delusion that Java is a simple > > language, it's a heavyweight document; running to 650 pages in almost 8MB > > but you only really need chapter 14 for this question :)
> > On 4 August 2010 05:43, Kirk <kirk.pepperd...@gmail.com> wrote:
> > > I'll be speaking there so I don't need a pass but thought it might be > fun > > > to put in a puzzler.
> > > public class StrangeLoop { > > > public static void main(String[] args) { > > > http://www.thestrangeloop.com > > > do { > > > System.out.println("Strange Loop"); > > > continue http; > > > } while (false); > > > } > > > }
> > > Puzzler, infinite loop or normal termination?
> > > Regards, > > > Kirk
> > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "The Java Posse" group. > > > To post to this group, send email to javaposse@googlegroups.com. > > > To unsubscribe from this group, send email to > > > javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com> > <javaposse%2Bunsubscribe@googlegroups .com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/javaposse?hl=en.
> -- > You received this message because you are subscribed to the Google Groups > "The Java Posse" group. > To post to this group, send email to javaposse@googlegroups.com. > To unsubscribe from this group, send email to > javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com> > . > For more options, visit this group at > http://groups.google.com/group/javaposse?hl=en.
-- Viktor Klang | "A complex system that works is invariably | found to have evolved from a simple system | that worked." - John Gall
Akka - the Actor Kernel: Akkasource.org Twttr: twitter.com/viktorklang
> On Wed, Aug 4, 2010 at 4:26 PM, Reinier Zwitserloot <reini...@gmail.com>wrote:
>> I really like how folks keep making the argument that java sucks >> because it is *well documented*.
> That made me laugh. What an euphemism! :D
>> Only a scala fanboy would go that far.
>> </trollbait>
>> On Aug 4, 11:01 am, Kevin Wright <kev.lee.wri...@gmail.com> wrote: >> > my thinking:
>> > `http:` is the label (followed by a comment that the pre-processor >> strips >> > out) >> > `do { ... } while (...)` is the labelled statement >> > `continue <label>` attempts to transfer control back to the "continue >> > target". In this case, the labelled do/while loop
>> > a `do {statement} while (expression)` loops will check its expression >> only >> > once the statement has executed normally (which it hasn't here) >> > So... the loop will begin again, and continue infinitely, or until an >> > exception is thrown or the JVM is terminated.
>> > No stack involved, at the level of compiled machine code it'll be jump >> > instructions all the way down...
>> > For anyone still labouring under the delusion that Java is a simple >> > language, it's a heavyweight document; running to 650 pages in almost >> 8MB >> > but you only really need chapter 14 for this question :)
>> > On 4 August 2010 05:43, Kirk <kirk.pepperd...@gmail.com> wrote:
>> > > I'll be speaking there so I don't need a pass but thought it might be >> fun >> > > to put in a puzzler.
>> > > public class StrangeLoop { >> > > public static void main(String[] args) { >> > > http://www.thestrangeloop.com >> > > do { >> > > System.out.println("Strange Loop"); >> > > continue http; >> > > } while (false); >> > > } >> > > }
>> > > Puzzler, infinite loop or normal termination?
>> > > Regards, >> > > Kirk
>> > > -- >> > > You received this message because you are subscribed to the Google >> Groups >> > > "The Java Posse" group. >> > > To post to this group, send email to javaposse@googlegroups.com. >> > > To unsubscribe from this group, send email to >> > > javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com> >> <javaposse%2Bunsubscribe@googlegroups .com> >> > > . >> > > For more options, visit this group at >> > >http://groups.google.com/group/javaposse?hl=en.
>> -- >> You received this message because you are subscribed to the Google Groups >> "The Java Posse" group. >> To post to this group, send email to javaposse@googlegroups.com. >> To unsubscribe from this group, send email to >> javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com> >> . >> For more options, visit this group at >> http://groups.google.com/group/javaposse?hl=en.
> -- > Viktor Klang > | "A complex system that works is invariably > | found to have evolved from a simple system > | that worked." - John Gall
> Akka - the Actor Kernel: Akkasource.org > Twttr: twitter.com/viktorklang
> -- > You received this message because you are subscribed to the Google Groups > "The Java Posse" group. > To post to this group, send email to javaposse@googlegroups.com. > To unsubscribe from this group, send email to > javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com> > . > For more options, visit this group at > http://groups.google.com/group/javaposse?hl=en.
Why would this be so? I can think of a few reasons:
- Java has had longer to discover and document ambiguities - The Java spec contains more "boilerplate": copyright, legal notices, padding, whitespace, etc. - Much of what Java considers to be part of the Language in Java is library in Scala (and so out of scope for the spec) e.g. enums, while loops, break/continue - Java has *lots* of special cases that Scala does away with e.g. autoboxing, + operator on strings, etc.
Truth be told, it's probably all of the above...
On 4 August 2010 14:06, Fabrizio Giudici <fabrizio.giud...@tidalwave.it>wrote:
> > For anyone still labouring under the delusion that Java is a > > simple language, it's a heavyweight document; running to 650 pages8 > > in almost 8MB but you only really need chapter 14 for this question > > :)
> Absolute figures are mostly worthless... comparisons help. How long > are the specs of Scala? Of course, I presume it might also depend on > the writing style.
Discussion subject changed to "Java vs Scala specs was Re: [The Java Posse] Re: Post your strangest loop and win (up to) 4 free passes to Strange Loop!" by Fabrizio Giudici
> I actually got the numbers wrong here. although the final page has > 649 printed on it, acrobat informs me that the document is in fact > 684 pages long,
> Why would this be so? I can think of a few reasons:
> * Java has had longer to discover and document ambiguities * The > Java spec contains more "boilerplate": copyright, legal notices, > padding, whitespace, etc. * Much of what Java considers to be part > of the Language in Java is library in Scala (and so out of scope > for the spec) e.g. enums, while loops, break/continue * Java has > *lots* of special cases that Scala does away with e.g. autoboxing, > + operator on strings, etc.
> Truth be told, it's probably all of the above...
Also, I'd say that Sun intention was to allow others to independently implement the compiler (as IBM did), and in this case I presume you have to be pretty picky. I think there's only a single Scala compiler around, right?
- -- Fabrizio Giudici - Java Architect, Project Manager Tidalwave s.a.s. - "We make Java work. Everywhere." java.net/blog/fabriziogiudici - www.tidalwave.it/people Fabrizio.Giud...@tidalwave.it -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.14 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
Discussion subject changed to "Java vs Scala specs was Re: [The Java Posse] Re: Post your strangest loop and win (up to) 4 free passes to Strange Loop!" by Kevin Wright
Unlike the original Java, it's open source - so if anyone wanted to add features to scalac, they'd be more likely to fork it than to start from scratch working against the spec. Even that is highly unlikely because:
1. scalac has a plugin mechanism for extending/changing functionality 2. Scala offers some quite nice techniques for you to add functionality via libraries, without requiring dedicated compiler support. Just take a look at actors (http://www.scala-lang.org/node/242) or ScalaTest(http://www.scalatest.org/quick_start), that's all library...
On 4 August 2010 18:16, Fabrizio Giudici <fabrizio.giud...@tidalwave.it>wrote:
> On 8/4/10 17:09 , Kevin Wright wrote: > > I actually got the numbers wrong here. although the final page has > > 649 printed on it, acrobat informs me that the document is in fact > > 684 pages long,
> > Why would this be so? I can think of a few reasons:
> > * Java has had longer to discover and document ambiguities * The > > Java spec contains more "boilerplate": copyright, legal notices, > > padding, whitespace, etc. * Much of what Java considers to be part > > of the Language in Java is library in Scala (and so out of scope > > for the spec) e.g. enums, while loops, break/continue * Java has > > *lots* of special cases that Scala does away with e.g. autoboxing, > > + operator on strings, etc.
> > Truth be told, it's probably all of the above...
> Also, I'd say that Sun intention was to allow others to independently > implement the compiler (as IBM did), and in this case I presume you > have to be pretty picky. I think there's only a single Scala compiler > around, right?
> -- > You received this message because you are subscribed to the Google Groups > "The Java Posse" group. > To post to this group, send email to javaposse@googlegroups.com. > To unsubscribe from this group, send email to > javaposse+unsubscribe@googlegroups.com<javaposse%2Bunsubscribe@googlegroups .com> > . > For more options, visit this group at > http://groups.google.com/group/javaposse?hl=en.
Well if "famous" loops that we're not writing ourselves are being
mentioned (eg. Duff's Device) then how could I not mention the loop
written by Mel Kaye on the RPC-4000?
Kaye wrote a loop that had no termination condition, yet it terminated
just fine. He'd taken advantage of the fact that the address and
opcode were both part of the instruction word. His loop executed an
instruction that operated on data pointed to by the address operand,
then incremented the address before looping back to execute the same
instruction again on the new data location. The data had been placed
at the top of memory, so the address would overflow when he did his
final increment. The overflow incremented the opcode, changing it to a
"jump". Since the address operand had just overflowed, it was now set
to 0, and the process jumped to address 0 where it continued
execution. (This was obviously before page guards) :-)
I'd love to write something that could challenge this piece of insane
genius, but modern languages tend to protect us from ourselves. It's
not impossible to see crazy stuff like this, but it's much rarer.
for(byte a=Byte.MIN_VALUE; a<=Byte.MAX_VALUE; a++) {
for(byte b=Byte.MIN_VALUE; b<=Byte.MAX_VALUE; b++) {
byte[] test = new byte[] { a, b};
//do something...
}
}
When I ran this, CPU shot to 100% and I had to take down Eclipse with
extreme prejudice. Can you spot the problem?
The problem is, neither of the loops can terminate. Neither for
condition will ever become false and cause the loop to terminate,
because neither a nor b can ever grow larger than Byte.MAX_VALUE. Duh.
Here is a very handy loop that *everyone* should use. ha ha.
import java.io.*;
import java.util.logging.*;
public class AddTabs {
public static void main(String[] args){
try {
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new
OutputStreamWriter(System.out));
String s = in.readLine();
while (s != null){
out.println(s.replaceAll(" ", "\t"));
s = in.readLine();
}
in.close();
out.close();
}
catch (IOException ex) {
> for(byte a=Byte.MIN_VALUE; a<=Byte.MAX_VALUE; a++) {
> for(byte b=Byte.MIN_VALUE; b<=Byte.MAX_VALUE; b++) {
> byte[] test = new byte[] { a, b};
> //do something...
> }
> }
> When I ran this, CPU shot to 100% and I had to take down Eclipse with
> extreme prejudice. Can you spot the problem?
> The problem is, neither of the loops can terminate. Neither for
> condition will ever become false and cause the loop to terminate,
> because neither a nor b can ever grow larger than Byte.MAX_VALUE. Duh.
Ouch, that brings back an old memory too. In my case I was using an
int, but casting to a byte (for bit-banging purposes) and failed to
see that my comparison was occurring while in byte form... leading to
exactly this problem. I thought I was safe by using an int, but
obviously incompetence can overcome any level of protection. (I mean
mine here, not yours!)