summaryrefslogtreecommitdiff
path: root/doc/macros.md
diff options
context:
space:
mode:
authorAndrey Listopadov <andreyorst@gmail.com>2021-07-17 17:49:16 +0300
committerAndrey Listopadov <andreyorst@gmail.com>2021-07-17 18:08:02 +0300
commitcc82dd57c87e19439f851c2bfd66834f70276bb8 (patch)
tree6aafcb943cd9ef3e3b47061f4d917f4d7ec0ab5a /doc/macros.md
parente8351c4a47ea0472151b3f5a5ac06a70f86f0d02 (diff)
fix: style changes, and documentation fixes
- provide defn as an alias to fn* and change fn* to defn in all files - fix doc for eq function - fix and generate doc for loop macro
Diffstat (limited to 'doc/macros.md')
-rw-r--r--doc/macros.md39
1 files changed, 38 insertions, 1 deletions
diff --git a/doc/macros.md b/doc/macros.md
index 6df90fd..6f90328 100644
--- a/doc/macros.md
+++ b/doc/macros.md
@@ -1,4 +1,4 @@
-# Macros.fnl (0.5.3)
+# Macros (v0.5.3)
Macros for Cljlib that implement various facilities from Clojure.
**Table of contents**
@@ -18,6 +18,7 @@ Macros for Cljlib that implement various facilities from Clojure.
- [`when-let`](#when-let)
- [`if-some`](#if-some)
- [`when-some`](#when-some)
+- [`loop`](#loop)
## `fn*`
Function signature:
@@ -587,6 +588,42 @@ If `test` sets `binding` to non-`nil`, evaluates `body` in implicit
`do`.
+## `loop`
+Function signature:
+
+```
+(loop binding-vec body*)
+```
+
+Recursive loop macro.
+
+Similar to `let`, but binds a special `recur` call that will reassign the values
+of the `binding-vec` and restart the loop `body*`.
+
+The first argument is a binding table with alternating symbols (or destructure
+forms), and the values to bind to them.
+
+For example:
+
+```fennel
+(loop [[first & rest] [1 2 3 4 5]
+ i 0]
+ (if (= nil first)
+ i
+ (recur rest (+ 1 i))))
+```
+
+This would destructure the first table argument, with the first value inside it
+being assigned to `first` and the remainder of the table being assigned to
+`rest`. `i` simply gets bound to 0.
+
+The body of the form executes for every item in the table, calling `recur` each
+time with the table lacking its head element (thus consuming one element per
+iteration), and with `i` being called with one value greater than the previous.
+
+When the loop terminates (When the user doesn't call `recur`) it will return the
+number of elements in the passed in table. (In this case, 5)
+
---
Copyright (C) 2020-2021 Andrey Listopadov