From 704d96bb21ba1aaadb18d6c7b12fbd6da8554174 Mon Sep 17 00:00:00 2001 From: colin Date: Sun, 9 Jul 2023 13:55:04 -0700 Subject: Add: nicer >> form called >>> --- hyperquirks.lisp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'hyperquirks.lisp') diff --git a/hyperquirks.lisp b/hyperquirks.lisp index d64ce08..7b6da14 100644 --- a/hyperquirks.lisp +++ b/hyperquirks.lisp @@ -150,7 +150,18 @@ form. When they do, the value of the previous form is passed into all instances is substituted in. However, two different substitution variables CANNOT APPEAR in a -single pipe form." +single pipe form. + +This macro accepts additional syntax: the :? form standing on its own +indicates the the next form shold be a predicate that, if it evaluates +to NIL, causes an early exit, with failure state FAIL. + +E.g. + +(>> (:fail :oh-no!) + (random 10) ; start with a random number + :? evenp ; exit with :oh-no! unless even + (+ 10 _)) ; add 10 and return." (let ((prefix (etypecase prefix (string prefix) @@ -158,6 +169,7 @@ single pipe form." (labels ((var-p (v) "A variable is a symbol that starts with PREFIX" (and (symbolp v) + (<= (length prefix) (length (symbol-name v))) (string= prefix (symbol-name v) :end2 (length prefix)))) (pipe-form-p (form) "A pipe form is either a symbol or a tree that contains @@ -211,6 +223,10 @@ single pipe form." (list 'block block (reduce #'folder (escape-early-transform pipe-forms) :initial-value initform))))) + +(defmacro >>> (initform &rest pipe-forms) + `(>> () ,initform ,@pipe-forms)) + (defmacro binding-cond (&body clauses) "Like cond except the first form of every clause is a binding form alá IMPERATIVE. -- cgit v1.2.3