Replies: 1 comment 2 replies
-
I do not know of any construct similar to DO-LOOP with a floating-point index. To to this, you can use the BEGIN-WHILE-REPEAT loop and float variables for the index and limit. Here is an example of a function that takes a callback and executes it in a loop while the index is within a given range. 0e fvalue dfw.r.lower
0e fvalue dfw.r.upper
' abort value dfw.callback
: do-float-within ( any xt.callback -- any ; F: any r.index.start r.lower r.upper -- any r.index.out-of-the-range )
\ Execute the callback while the index in the range [ r.lower , r.upper )
\ xt.callback => ( F: any r.index1 -- any r.index2 ; S: any -- any )
dfw.r.upper f>r dfw.r.lower f>r dfw.callback >r
to dfw.r.upper to dfw.r.lower to dfw.callback
begin ( F: r.index1 )
fdup dfw.r.upper f< while
fdup dfw.r.lower f< 0= while
dfw.callback execute ( F: r.index2 )
repeat then ( F: r )
r> to dfw.callback fr> to dfw.r.lower fr> to dfw.r.upper
; Usage example: 0e 0e 10e [: ( F: r -- r ) fdup f. 0.7e f+ ;] do-float-within fdrop cr
\ output: 0. 0.7 1.4 2.1 2.8 3.5 4.2 4.9 5.6 6.3 7. 7.7 8.4 9.1 9.8 This function requires the words NB: this function will not work correct if a callback does not return control in a recursive call of this function (due to catch/throw). To fix that, |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I just realised there does not appear to be any
DO
..+LOOP
equivalents using floating pointlimit
andindex
. Is there some extension for this or suggested technique I'm not grokking this moment?Beta Was this translation helpful? Give feedback.
All reactions