feat: Add additional ignore macros (#1300)

* feat: Add additional ignore macros

This commit adds two new ignore macros, ignore*, which wraps an
arbitrary number of forms in calls to `ignore` and ignore-do, which
wraps an arbitrary number of forms in ignore, then bundles the whole in
a do call, effectively executing each form only for side effects and
ignoring all results.

* docs: Update ignore* docs

Link to `ignore` doc

Co-authored-by: Veit Heller <veit@veitheller.de>

* fix: Call ignore* in ignore-do

ignore-all was an old name that no longer exists!

* test: Add test for ignore-do

Co-authored-by: Veit Heller <veit@veitheller.de>
This commit is contained in:
Scott Olsen 2021-08-23 14:31:10 -04:00 committed by GitHub
parent 0c9c475e6c
commit c9967ddf6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View File

@ -133,6 +133,13 @@ Example:
(defmacro forever-do [:rest forms]
(list 'while true (cons 'do forms)))
(doc ignore-do
("Wraps side-effecting `forms` in a `do`, " false)
"ignoring all of their results."
"In other words, executes `forms` only for their side effects.")
(defmacro ignore-do [:rest forms]
(cons 'do (expand (apply ignore* forms))))
(doc when "is an `if` without an else branch.")
(defmacro when [condition form]
(list 'if condition form (list)))

View File

@ -258,6 +258,10 @@ the filename and module name are the same.")
(defmacro ignore [form]
(list 'let (array '_ form) (list)))
(doc ignore* "Wraps all forms passed as an argument in a call to [`ignore`](#ignore).")
(defmacro ignore* [:rest forms]
(map (fn [x] (cons-last x (list 'ignore))) forms))
(doc const-assert
"Asserts that the expression `expr` is true at compile time."
"Otherwise it will fail with the message `msg`."

15
test/control_macros.carp Normal file
View File

@ -0,0 +1,15 @@
(load "Test.carp")
(use Test)
(def test-string @"")
(defn test-ignore-do []
(ignore-do (+ 2 2) ;; ignored
(set! test-string @"new-string") ;; ignored, but side-effect performed
(- 4 4)))
(deftest test
(assert-true test
(and (= () (test-ignore-do)) (= &test-string "new-string"))
"ignore-do performs side effects and ignores all results")
)