Skip to content

Commit a2d54c9

Browse files
committed
Document other useful ways to use template calls
1 parent 043cb03 commit a2d54c9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

docs/language.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,42 @@ calls are appended to the `!canvas` node.
966966
As template function calls convert into regular function calls, templates may
967967
be used recursively.
968968

969+
### Other ways to use template function calls
970+
971+
Although template function calls are designed to allow custom node-like
972+
operations to be defined, they can be used in other ways. A particularly useful
973+
application is debugging node construction by adding a `@debug` call. As the
974+
`debug()` function logs and returns its first argument, it can be inserted at
975+
any point in a node tree to see what has been constructed without altering the
976+
tree. For example:
977+
978+
```flitter
979+
!window
980+
!canvas3d viewpoint=1
981+
!light color=1 direction=-1
982+
@debug
983+
for i in ..10
984+
!sphere position=beta(:position;i)[..3]-0.5 size=0.1
985+
```
986+
987+
Similarly, template function calls can be used in functions to wrap a sequence
988+
of expressions. For example:
989+
990+
```flitter
991+
func onoise(seed, n, k, x, y, z)
992+
let weights=k**i for i in ..n
993+
total=sum(weights)
994+
@sum
995+
for i in ..n
996+
let scale=2**i
997+
noise(seed, x*scale, y*scale, z*scale) * weights[i] / total
998+
```
999+
1000+
This function is (almost) equivalent to the built-in function `octnoise()` and
1001+
makes use of `@sum` to add up all of the calls to `noise()`. Although this
1002+
could have been done with `sum()` and an in-line for loop, this code is more
1003+
readable.
1004+
9691005
## State
9701006

9711007
Any interactive component of **Flitter**, such as a MIDI controller,

0 commit comments

Comments
 (0)