World: r3wp
[Core] Discuss core issues
older newer | first last |
Sunanda 18-Dec-2008 [11463] | EFishAnt -- use a loop 1 [...] inside the while to simuate a "continue" n: 1 while [n <> 10][ loop 1 [ n: n + 1 if n = 5 [break] print n ] ] |
sqlab 18-Dec-2008 [11464] | forever [ catch [ wait [events] do-some-thing if want-no-more [ throw ] do-more ] ] |
Steeve 18-Dec-2008 [11465] | generaly, i like to use a case structure inside my loop. while [n <> 10][ n: n + 1 case [ n = 5 [] ; break other-case [...] true [print n] ] ] |
Sunanda 18-Dec-2008 [11466] | With my suggestion, it would look like this (Sqlab's may be more elegant) n: 0 while [n <> 10][ n: n + 1 loop 1 [ case [ n = 5 [break] ; break n = 7 [print "its seven"] ; example of another case true [print n] ] ] ] |
BrianH 18-Dec-2008 [11467] | R3 has a CONTINUE native. Not much help now I guess... |
Pekr 18-Dec-2008 [11468x2] | How does 'continue in R3 work? I can't see it on wiki. Console help shows it, so yes, it does exist :-) |
It would be good to mentione it eventually in Function notes section - there are new usefull R3 funcs .... well, if this one is generally usefull :-) | |
Steeve 18-Dec-2008 [11470] | i have done this too for more clarity: continue: [] while [n <> 10][ n: n + 1 case [ n = 5 continue other-case [...] true [print n] ] ] |
BrianH 18-Dec-2008 [11471x3] | The R3 continue works the way it does in any structured language with a continue operation. It's basically goto the end of the loop, while break is goto the point right after the loop. |
R3's functions are still in flux. People often expect stuff put into the documentation to exist as documented. | |
Still, CONTINUE is likely to remain as it is. | |
Sunanda 18-Dec-2008 [11474] | That works. Steeve -- unless there is termination code between the end of the CASE and the end of the WHILE. That code does not get skipped: continue: [] n: 0 while [n <> 10][ n: n + 1 case [ n = 5 continue n = 7 [print "it's seven"] true [print n] ] print ["this gets printed even for n=5...." n] ] |
BrianH 18-Dec-2008 [11475] | Sorry, my mention of R3 is probably off topic for this group. Nevermind. |
Steeve 18-Dec-2008 [11476] | yes Sunanda no code must be inserted between the end of the case and the end of the loop. If you can't refactoring your code to follow this rule, it's useless |
BrianH 18-Dec-2008 [11477] | Sqlab's solution will still work though. |
eFishAnt 18-Dec-2008 [11478x2] | Thanks, those are clever. Muchos Appprechioto |
I'm sure if I asked for Goto hacks, they must abound as well, or perhaps not be bound. | |
BrianH 18-Dec-2008 [11480] | Outside of rebcode, you can't goto down, but you can goto up with CATCH/name and THROW/name. Of course the structured goto replacements still work (RETURN, EXIT, BREAK). |
Steeve 18-Dec-2008 [11481] | in fact i remember having to simulate a goto command when i tried to develop a Z80 emulator. i had build a simple engine working like that: code: [ [ some code....] [ some code...] [ some code ... GOTO: 1] ; goto the first line of code ] GOTO: 0 forever [ GOTO: GOTO + 1 do pick code goto ] |
BrianH 18-Dec-2008 [11482] | That's a pretty standard switch interpreter model. |
Steeve 18-Dec-2008 [11483] | yep, even if there is an error in my code, it's very easy to do |
eFishAnt 18-Dec-2008 [11484x2] | I was tongue in cheek on that, but then I'm not surprised there is an "elegant" goto in REBOL. Thanks, I am out of the woods with my problem, thanks to the best help-desk in the world! |
print "Continue hack for a forever loop using Catch and Throw" n: 0 forever [ ;from sqlab n: n + 1 print join "top of FOREVER LN:" [n] catch [ print "top of CATCH" if n > 100 [ break ] if random 10 > 5 [ throw ] print "BOTTOM BELOW THROW CONDITIONAL" ] print "BOTTOM BELOW CATCH" ] print "BREAK forever loop and halt" halt ;this is what I did to mess with it, sqlab | |
BrianH 18-Dec-2008 [11486] | continuing: func [code [block!]] [catch/name code 'continue] continue: func [/return value [any-type!]] [throw/name get/any 'value 'continue] |
Steeve 18-Dec-2008 [11487] | in that case a simple exit or return is enough: continue: :exit continuing: func [code][do code] |
BrianH 18-Dec-2008 [11488] | No, that would capture real exits and returns. |
Steeve 18-Dec-2008 [11489] | like in your case |
BrianH 18-Dec-2008 [11490] | Then you add the appropriate function attributes to my case to make sure that exits and returns aren't captured ([throw]?). |
Steeve 18-Dec-2008 [11491x2] | that should be ok |
according to forskip source, perhaps a dual [throw catch] attribute is required, i'm not well versed in using such tricks | |
BrianH 18-Dec-2008 [11493x2] | The [catch] attribute is like a CATCH block for errors in the function, in this case used to catch the error! thrown in the source on the first and fifth lines. There is no need for the [catch] attribute in the CONTINUING function above, or for any function attribute in the CONTINUE function above. |
By the way, R3 doesn't need anything like the [catch] attribute because it does stack traces on errors. | |
Steeve 18-Dec-2008 [11495] | BrianH, in R3 some functions are not any more natives (ie minimum-of). Is that meaning that they will be slower ? |
BrianH 18-Dec-2008 [11496] | In some cases, yes. In other cases, changing the functions to mezzanines has allowed us to get algorithmic speedups. In more critical cases (like loops), functions that used to be mezzanine are now native. The overall speed of R3 is greater. |
sqlab 18-Dec-2008 [11497] | repeat n 100 [ print join "top of loop LN:" [n] catch [ print "top of CATCH" if random 10 > 5 [ throw ] print "BOTTOM BELOW THROW CONDITIONAL" ] print "BOTTOM BELOW CATCH" ] |
Steeve 18-Dec-2008 [11498] | regarding the source of minimum-of and maximum-of, i assume they fall in the wrong side |
BrianH 18-Dec-2008 [11499x2] | They are infrequently used functions, and as such are a good candidate to be mezzanines, for 2 reasons: - Frees up some native space for more important functions. - Easier to modify or replace with better functions. |
The really interesting thing is the intrinsics - REBOL code called internally by natives. | |
Steeve 18-Dec-2008 [11501] | i see, sort of hooks, but currently there's only 3 intrinsics func, is that enough ? |
BrianH 18-Dec-2008 [11502] | For now, there are 5 intrinsic functions. There can be more later. |
Steeve 18-Dec-2008 [11503] | it could be amazing if we have an intrinsic func for each type of value (when they are evaluated) but i assume it would be very slow |
BrianH 18-Dec-2008 [11504] | You are thinking of actions. |
Steeve 18-Dec-2008 [11505] | (seems devbase is not up to date, i see only 3 functions) |
BrianH 18-Dec-2008 [11506] | There are several dozen actions. |
Steeve 18-Dec-2008 [11507] | in which group of devbase are they filled (actions) ? |
BrianH 18-Dec-2008 [11508x2] | DevBase is up to date (as of this week, except for the View stuff). The other intrinsics are defined elsewhere in the source. |
Actions are native code, which is not in DevBase yet. | |
Steeve 18-Dec-2008 [11510] | ok, i see the actions now, do u say that we can redefine actions ? |
[unknown: 5] 18-Dec-2008 [11511] | efishant - for what you want to do with the loop just put the while loop inside a function (assuming you manipulation a series) and then have it break out on a conidtion is met and then change the series to the desires position and pass back to the function if needed. Not sure exactly what your trying to accomplish. |
BrianH 18-Dec-2008 [11512] | No, but actions are handled on a type-specific basis, and R3 will have user-defined types. |
older newer | first last |