1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-27 00:40:26 +00:00

Merge pull request #893 from pyrmont/docs.nested-loops

Clarify nested loop behaviour in loop macro
This commit is contained in:
Calvin Rose 2021-12-09 14:09:59 -06:00 committed by GitHub
commit c48a942d22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -532,16 +532,19 @@
(defmacro loop (defmacro loop
``` ```
A general purpose loop macro. This macro is similar to the Common Lisp A general purpose loop macro. This macro is similar to the Common Lisp loop
loop macro, although intentionally much smaller in scope. macro, although intentionally much smaller in scope. The head of the loop
The head of the loop should be a tuple that contains a sequence of should be a tuple that contains a sequence of either bindings or
either bindings or conditionals. A binding is a sequence of three values conditionals. A binding is a sequence of three values that define something
that define something to loop over. They are formatted like: to loop over. Bindings are written in the format:
binding :verb object/expression binding :verb object/expression
Where `binding` is a binding as passed to def, `:verb` is one of a set of where `binding` is a binding as passed to def, `:verb` is one of a set of
keywords, and `object` is any expression. The available verbs are: keywords, and `object` is any expression. Each subsequent binding creates a
nested loop within the loop created by the previous binding.
The available verbs are:
* `:iterate` - repeatedly evaluate and bind to the expression while it is * `:iterate` - repeatedly evaluate and bind to the expression while it is
truthy. truthy.
@ -565,14 +568,19 @@
where `:modifier` is one of a set of keywords, and `argument` is keyword-dependent. where `:modifier` is one of a set of keywords, and `argument` is keyword-dependent.
`:modifier` can be one of: `:modifier` can be one of:
* `:while expression` - breaks from the loop if `expression` is falsey. * `:while expression` - breaks from the current loop if `expression` is
* `:until expression` - breaks from the loop if `expression` is truthy. falsey.
* `:let bindings` - defines bindings inside the loop as passed to the `let` macro. * `:until expression` - breaks from the current loop if `expression` is
* `:before form` - evaluates a form for a side effect before the next inner loop. truthy.
* `:after form` - same as `:before`, but the side effect happens after the next inner loop. * `:let bindings` - defines bindings inside the current loop as passed to the
`let` macro.
* `:before form` - evaluates a form for a side effect before the next inner
loop.
* `:after form` - same as `:before`, but the side effect happens after the
next inner loop.
* `:repeat n` - repeats the next inner loop `n` times. * `:repeat n` - repeats the next inner loop `n` times.
lets try putting a loop item on multiple lines. * `:when condition` - only evaluates the current loop body when `condition`
* `:when condition` - only evaluates the loop body when condition is true. is true.
The `loop` macro always evaluates to nil. The `loop` macro always evaluates to nil.
``` ```