[REBOL] Re: for bug?
From: g:santilli:tiscalinet:it at: 18-Jun-2002 18:33
Hi Joel,
On Tuesday, June 18, 2002, 2:28:42 PM, you wrote:
JN> 1) perform type consistency checks and throw an error if bounds
JN> and increment are not suitable
JN> 2) calculate the number of values that actually satisfy both the
JN> bounds/increment constraints and the domain constraints of
JN> the bounds types
JN> 3) if the number of such values is zero, the range is empty, so
JN> do nothing gracefully and exit
JN> 4) if the number of such value is non-zero, set CURRENT to the
JN> lower bound, then...
JN> 5) use LOOP (controlled by the number of values calculated in
JN> step 2) to perform the body and then increment CURRENT by the
JN> supplied "bump" argument.
Interesting idea... something like:
for: func [
"Repeats a block over a range of values."
[catch throw]
'word [word!] "Variable to hold current value"
start [number! series! money! time! date! char!] "Starting value"
end [number! series! money! time! date! char!] "Ending value"
bump [number! money! time! char!] "Amount to skip each time"
body [block!] "Block to evaluate"
/local result do-body times
][
if (type? start) <> (type? end) [
throw make error! reduce ['script 'expect-arg 'for 'end type? start]
]
do-body: func reduce [[throw] word] body
op: :greater-or-equal?
either series? start [
if not same? head start head end [
throw make error! reduce ['script 'invalid-arg end]
]
times: 1 + to-integer divide subtract index? end index? start bump
loop times [
set/any 'result do-body start
start: skip start bump
]
] [
times: 1 + to-integer divide subtract end start bump
loop times [
set/any 'result do-body start
start: start + bump
]
]
get/any 'result
]
Do you see any problems with it?
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r