Files
Cagire/docs/generators.md

66 lines
1.1 KiB
Markdown

# Generators
Create sequences of values on the stack.
## Arithmetic Range
The `..` operator pushes an arithmetic sequence:
```forth
1 4 .. ( pushes 1 2 3 4 )
60 67 .. ( pushes 60 61 62 63 64 65 66 67 )
```
## Geometric Range
The `geom..` operator creates a geometric sequence:
```forth
1 2 4 geom.. ( pushes 1 2 4 8 )
100 0.5 4 geom.. ( pushes 100 50 25 12.5 )
```
Stack: `(start ratio count -- values...)`
## Generate
The `gen` word executes a quotation multiple times:
```forth
{ 1 6 rand } 4 gen ( 4 random values from 1-6 )
{ coin } 8 gen ( 8 random 0/1 values )
```
## Examples
Random melody:
```forth
"pluck" s
{ 48 72 rand } 4 gen 4 choose note
.
```
Chord from range:
```forth
"pad" s
60 67 .. 8 choose note ( random note in octave )
.
```
Euclidean-style rhythm values:
```forth
0 1 0 0 1 0 1 0 ( manual )
{ coin } 8 gen ( random 8-step pattern )
```
## Words
| Word | Stack | Description |
|------|-------|-------------|
| `..` | (start end -- values...) | Arithmetic sequence |
| `geom..` | (start ratio count -- values...) | Geometric sequence |
| `gen` | (quot n -- results...) | Execute quotation n times |