summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2022-10-22 18:11:52 +0200
committerLeah Neukirchen <leah@vuxu.org>2022-10-22 18:11:52 +0200
commite64cd9e4524440e29dda141693fbadd99d20e4fd (patch)
tree7a20491f5f41b95f6f41391012a67975ee36417b
parente96132476e29db4883fe8e0d6befc8dbc2c36fb3 (diff)
downloadmew-e64cd9e4524440e29dda141693fbadd99d20e4fd.tar.gz
mew-e64cd9e4524440e29dda141693fbadd99d20e4fd.tar.xz
mew-e64cd9e4524440e29dda141693fbadd99d20e4fd.zip
add given
-rw-r--r--mew.scm19
-rw-r--r--mew.svnwiki6
2 files changed, 24 insertions, 1 deletions
diff --git a/mew.scm b/mew.scm
index 6ee7924..83aeef8 100644
--- a/mew.scm
+++ b/mew.scm
@@ -4,7 +4,7 @@
      dec def div
      empty? eof esc
      fin final for generic-for-each
-     get gen genumerate gfix giterate gmatch gsplit gwindow
+     get gen genumerate gfix given giterate gmatch gsplit gwindow
      inc into
      keys
      len loc
@@ -346,6 +346,23 @@
       ((_ location rest ...)
        (set! location (->> location ->> rest ...)))))
 
+  (define-syntax given
+    (syntax-rules ()
+      ((_ expr bool (then . then-rest) (else . else-rest))
+       (let ((val expr))
+         (if bool
+           (then val . then-rest)
+           (else val . else-rest))))
+      ((_ expr bool (then . then-rest) else)
+       (given expr bool (then . then-rest) (else)))
+      ((_ expr bool then (else . else-rest))
+       (given expr bool (then) (else . else-rest)))
+      ((_ expr bool then else)
+       (given expr bool (then) (else)))
+      ((_ expr bool then)
+       (given expr bool then ((op))))
+      ))
+
   (define (~? str pat)
     (let ((data (irregex-search pat str)))
       (if data
diff --git a/mew.svnwiki b/mew.svnwiki
index b65d38f..5984123 100644
--- a/mew.svnwiki
+++ b/mew.svnwiki
@@ -274,3 +274,9 @@ of the lambda.
 <syntax>(set->> loc ->> ...)</syntax>
 
 Mutation with nesting macros: shortcut for {{(set loc (-> loc ...))}}.
+
+<syntax>(given <val> <cond> <then> <else>?)<syntax>
+
+Evaluate {{<val>}}.  Then, when {{<cond>}} is truthy, behaves like
+{{(-> <val> -> <then>}}, otherwise like {{(-> <val> -> <else>)}}
+(or just {{<val>}} if no {{<else>}} was passed).