About Monkey 2 › Forums › Monkey 2 Development › (Bug) For..Step negative values not working.
This topic contains 8 replies, has 6 voices, and was last updated by
nobuyuki
2 years, 10 months ago.
-
AuthorPosts
-
June 4, 2016 at 1:02 am #886
For..Step negative values not working. Example:
Monkey123456Function Main()For Local x:Int = 5 To 1 Step -1Print xNextEndJune 4, 2016 at 7:18 pm #899Function Main is reserved I think? No?
Tested this though and it works fine for me but I didn’t have the code within a Function Main().
June 4, 2016 at 11:27 pm #900Function Main is the starting point for all MonkeyX’s (1 & 2) application. You can put anything in there you like (as long as its valid MX2).
When I tested the negative step on v0.008 it doesnt do anything…
June 5, 2016 at 12:32 am #901That snippet should print out the numbers 5 to 1.
I’m actually shocked this bug has remained hidden this far into development (or maybe it got introduced recently?). Reverse cycling through a loop is a fundamental programming task – or at least it used to be.
I only found it because I’ve ported some old BlitzMax code that showed bizarre results in MX2 for no obvious reason. Stumped me for *hours*… Kinda relieved the problem isn’t in my code after all.
Edit:
@amon
Where did you have the code? Reverse For loop iteration fails for me in class Methods too.June 5, 2016 at 1:56 am #902Still fails in v0.009:
Monkey12345678910Function Main()Print "Step 2:"For Local x:Int = 1 To 10 Step 2Print xNextPrint "Step -1:"For Local x:Int = 5 To 1 Step -1Print xNextEndWhich outputs the following:
Monkey12345678910111213141516171819"H:/Projects/monkey2/bin/mx2cc_windows" makeapp -target=Desktop -config=Debug "H:/Projects/monkey2/hello-world.monkey2"MX2CC V0.009***** Building app 'H:/Projects/monkey2/hello-world.monkey2' *****Parsing...Semanting...Translating...Compiling....Linking H:/Projects/monkey2/hello-world.buildv009/desktop_debug_windows/hello-world.exeRunning H:/Projects/monkey2/hello-world.buildv009/desktop_debug_windows/hello-world.exeStep 2:13579Step -1:Done.June 5, 2016 at 9:52 am #908Hmm my bad. It works but the output is not what it should be. I didn’t check that. Apologies.
After reading both replies I went back and did testing I should have done when first reading the OP. Again, though it works but the output is all wrong much like therevills has shown.
June 6, 2016 at 12:42 am #913What about adding DownTo (additional to ‘Step -1’)?
[/crayon]Monkey1234567891011[crayon-5cb9d7c17e6ed416479593 inline="true" ]Function Main()For Local x:Int = 5 DownTo 1Print xNextFor Local y:Int = 10 DownTo 0 Step 2Print yNextEnd[/crayon]Monkey123456789101112[crayon-5cb9d7c17e6f3034910953 inline="true" ]Function Main()Local:Int x,yFor x = 5 DownTo 1Print xNextFor y = 10 DownTo 0 Step 2Print yNextEndJune 6, 2016 at 8:36 pm #929Negative step should be working now.
I think I will re-add the ‘c’ style ‘for’ that used to be in there though as it’s the most flexible.
June 10, 2016 at 8:33 am #1024Please consider adding c-style loops carefully. Some people (especially c coders) will not take to using basic style loops at all, even if it is the recommended standard convention, and this is one of the cornerstones of what many would consider the readability of basic-style syntax. (I consider Monkey syntax even more readable then standard Basic when the step value is positive if using Until!)
As long as the step value can be arbitrary, it retains most of the flexibility of a c-style for loop. With a custom iterator, it should be able to do just about everything.
Presented for your consideration: https://msdn.microsoft.com/en-us/library/dscyy5s0.aspx
Due to the way iterators were implemented in the different languages (possibly related to foreach syntax), their articles are different, and VB didn’t get custom iterators until 7 years later.
Though I would recommend against reserving a keyword for something as simple and arbitrary as a negative step value, perhaps this might be okay if specifying a custom iterator. (IteratingBy x? Step x if x is a Func? Idk..) in that case, the iterator method would have a standard pattern whereby all for loops invoking one would have the loop bounds available to it as arguments.. maybe a bit clunky, like old Property syntax…
Ideally I’d like to specify an arbitrary step value (positive, negative, and integer runtime variables that aren’t consts) without c-style syntax, but if it’s not possible for whatever reason (and if that’s why you’re leaning towards enabling c-style for loops), please consider this.
-
AuthorPosts
You must be logged in to reply to this topic.