15c0569404
FossilOrigin-Name: c3d30eebc6f43188cd59c30aa4e339e4186bf457d1f7de0d46a165618d73df21
31 lines
795 B
Text
31 lines
795 B
Text
This implements some words to return a range of numbers on the stack.
|
|
It's probably best to capture these values in an array.
|
|
|
|
First is a word to return values from a lower to upper limit, incrementing
|
|
upwards. The returned values are inclusive of the limits.
|
|
|
|
~~~
|
|
:range<inc> (lh-a)
|
|
over - n:inc [ I over + swap ] times<with-index> drop ;
|
|
~~~
|
|
|
|
Next is a word to return values from an upper to lower limit, decrementing
|
|
upwards. The returned values are inclusive of the limits.
|
|
|
|
~~~
|
|
:range<dec> (hl-a)
|
|
over &- dip swap n:inc [ I over swap - swap ] times<with-index> drop ;
|
|
~~~
|
|
|
|
The last word takes the limits and calls the appropriate word.
|
|
|
|
~~~
|
|
:range (nn-a)
|
|
dup-pair gt? [ range<dec> ] [ range<inc> ] choose ;
|
|
~~~
|
|
|
|
As a simple test case:
|
|
|
|
```
|
|
{ #1 #5 range } [ n:put sp ] a:for-each
|
|
```
|