19
3
Let's take a look on a typical loop, which usually performs 8 iterations:
for (int x=0; x<8; ++x);
You have to make it infinite!
It's a popularity-contest for all languages that support such form of for loop. So solution with highest score (upvotes minus downvotes) wins.
If your language has the other form of for loop, but you are sure, you can make something cool with it, feel free to post the answer and mark it as noncompeting. I reserve the right to enlarge scope of available constructions and languages, but it will never be shrinked, so don't be afraid of dropping previously correct solutions.
What is solution?
Solution consists of two programs.
The first program is a clean program. It's the typical program in your language with the for loop making 8 iterations. It should be the normal program, any developer could write. No any special hacks for preparation purposes. For example:
int main()
{
for (int x=0; x<8; ++x);
return 0;
}
The second program is augmented. This program should contain all the code from clean program and some additional code. There are limited number of extension points, see complete rules section for details. An augmented program for the clean one above can be
inline bool operator < (const int &a, const int &b)
{
return true;
}
int main()
{
for (int x=0; x<8; ++x);
return 0;
}
That's just an example (noncompilable in C++) to show an idea. The real correct augmented program have to be compilable, working and having infinite loop.
Complete rules
Both programs:
- Any language with support of such
forloops is ok. - The loop body has to be empty. More precisely, you can place some output or other code into the loop, but loop behavior should be the same in case of empty loop.
Clean program:
Loop uses integer or numeric counter and performs 8 iterations:
for (int x=0; x<8; ++x); // C, C++, C# for (var x=0; x<8; ++x); // C#, Javascript for (auto x=0; x<8; ++x); // C, C++ for (auto signed x=0; x<8; ++x); // C, C++ for (register int x=0; x<8; ++x); // C, C++User-defined types are disallowed.
- Using of property (except of global variable) instead of loop variable is disallowed.
Declaration of variable can be inside or outside of the loop. Following code is ok:
int x; for(x=0; x<8; ++x);Either prefix or postfix increment can be used.
Loop limit
8should be written as a constant literal without saving to named constant or variable. It's made to prevent solutions based on declaring variable or constant equal to 8, and then reassigning, overriding or shadowing it by the other value:const double n = 8; int main() { const double n = 9007199254740992; for (double x=0; x<n; ++x); return 0; }
Augmented program:
- Must contain all the code from the clean one.
- Should extend clean program in limited number of extension points.
- Must execute same
forloop as an infinite loop itself.
Placing of the loop into another infinite construction is not ok. - Runtime or compile-time patching of the code is allowed as long as textual representation of it is unchanged.
- Placing the construction into a string and passing to
evalis disallowed.
Extension points:
- Anywhere outside of the fragment with clean code, including other files or other assemblies.
forstatement (as single piece -forconstruction and its body) must be kept unchanged.- Variable declaration must be kept the same.
- Any place between simple statements can be used as extension point.
- If and only if variable was declared outside of the loop and without immediate assignment of the value, such assignment can be added.
/* extension point here */
int main()
/* extension point here */
{
/* extension point here */
int x /* extension point for assignment here */;
/* extension point here */
for (x=0; x<8; ++x);
/* extension point here */
return 0;
/* extension point here */
}
/* extension point here */
int main()
{
/* BEGIN: No changes allowed */ int x = 0; /* END */
/* extension point here */
/* BEGIN: No changes allowed */ for (x=0; x<8; ++x); /* END */
return 0;
}
PS: If possible, please provide a link to online IDE.
2@Oliver, as I know, "highest score (upvotes minus downvotes)" is exactly the defaults for [tag:popularity-contest], at least it is written in the tag description: "A popularity contest is a competition where the answer with the highest vote tally (upvotes minus downvotes) wins." But I can add it to the question explicitly. – Qwertiy – 2016-11-21T21:19:30.557
@LuisMendo, I expect it to be ok for [tag:restricted-source]. By the way, the first sentence by your link: "Unless you're writing a language-specific challenge" - yes, I am. I'm limiting scope of the languages by this construction, I understand it and I'm making it on purpoise.
– Qwertiy – 2016-11-21T22:44:24.277How about removing the language restriction, and replacing the for loop with something more general (and have the extension points only be before and after the clean code). Maybe something like print something 8 times for the clean task, and have the clean task go on forever, for the augmented task – Maltysen – 2016-11-21T22:49:22.030
1@Maltysen, there is a lot of interesting solutions in languages with these construction. There are C and C++ (with absolutely different solutions), C#, Java, Javascript, php, Perl, Groovy. I'm sure there are much more. Anyway, I'm open to enlarge question and that's specified in rules. If you can make something iteresting in other language - post it. If it will have positive reation, rules can be enlarged. – Qwertiy – 2016-11-21T22:51:20.240
4Doing this as a [tag:popularity-contest] is a little awkward because there's no description of what criteria voters should choose when voting (making the victory condition subjective). I was working on a [tag:code-golf] solution on the basis that many people here find golf solutions interesting and thus it might be popular; that seems like it might be a workable victory condition for the challenge. – None – 2016-11-21T23:26:07.863
@ais523, no [tag:code-golf] doen't suit for this challenge as 1. Interesting solutions with reflection or code patching are not good for golfing, also they are much better in readable state then in golfed. 2. C/C++ solution will win in 13 chars. 3. The other very cool C++ only solution will have 16 chars. – Qwertiy – 2016-11-21T23:37:05.027
@ais523, "there's no description of what criteria voters should choose when voting" let's take a look at [tag:popularity-contest] description: "Qualities which should be AVOIDED in popularity contests ... Rules what people should consider when voting. In the past this has consistently never worked out." – Qwertiy – 2016-11-22T09:11:16.197
2>"integer or numeric counter" is a bit too vague. E.g. does it include
< – Peter Taylor – 2016-11-24T14:08:41.407
java.lang.Integer? 2. This would better with a proper winning criterion.1Yes, it does. 2. What exactly winning creteria? PS: We can continue on meta.
– Qwertiy – 2016-11-24T14:40:50.213
>
"If and only if variable was declared outside of the loop and without immediate assignment of the value, such assignment can be added." So in Perl 5 this is good? Program 1:
my$a;1;$a=8;1while$b<$aProgram 2:my$a;1;$a=8;$a=Inf;1while$b<$a– msh210 – 2016-11-24T15:36:53.227@msh210, it was about loop variable, not about loop limit. loop limit is expected to be constant. – Qwertiy – 2016-11-24T17:39:36.150
@Dennis, why close?
– Qwertiy – 2017-02-06T05:57:29.1001Because the challenge is too broad for our standards. In particular, If your language has the other form of for loop, but you are sure, you can make something cool with it, feel free to post the answer and mark it as noncompeting. I reserve the right to enlarge scope of available constructions and languages, but it will never be shrinked, so don't be afraid of dropping previously correct solutions. That paragraph entirely explains why it's too broad. – Alex A. – 2017-02-06T06:06:58.573
How do noncompiting answers affect how broad the question is? And why not to answer on meta? – Qwertiy – 2017-02-06T06:11:58.547