diff options
author | Stuart Sierra <mail@stuartsierra.com> | 2008-04-07 21:51:25 +0000 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2008-04-07 21:51:25 +0000 |
commit | e2a2099aba91fd83858cdf02e954b550813406d9 (patch) | |
tree | 04764cd1733cfab9436f54fb9702da3d3cae372a | |
parent | 71cebc07111b98797924c0da56077e20e88f819f (diff) |
Added anaphor.clj, simple anaphoric macros (aif, awhen, ...).
-rw-r--r-- | anaphor.clj | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/anaphor.clj b/anaphor.clj new file mode 100644 index 00000000..4ffbde46 --- /dev/null +++ b/anaphor.clj @@ -0,0 +1,55 @@ +;;; anaphor.clj -- Anaphoric macros for Clojure + +;; by Stuart Sierra <mail@stuartsierra.com> +;; April 7, 2008 + +;; Copyright (c) 2008 Stuart Sierra. All rights reserved. The use and +;; distribution terms for this software are covered by the Common +;; Public License 1.0 (http://www.opensource.org/licenses/cpl1.0.php) +;; which can be found in the file CPL.TXT at the root of this +;; distribution. By using this software in any fashion, you are +;; agreeing to be bound by the terms of this license. You must not +;; remove this notice, or any other, from this software. + + +;; This file defines some simple anaphoric macros for Clojure. +;; +;; See "On Lisp" for a description of anaphoric macros: +;; http://www.bookshelf.jp/cgi-bin/goto.cgi?file=onlisp&node=Anaphoric+Macros +;; +;; The macros defined in this file differ slightly from the anaphoric +;; macros in "On Lisp" in that they each take a symbol as their first +;; argument. That symbol is the "anaphor" (for which "On Lisp" always +;; uses "it"). This follows Clojure's preference for non-capturing +;; macros. + + +(clojure/in-ns 'anaphor) +(clojure/refer 'clojure) + +(defmacro aif + "Like 'if', but binds the result of the test to 'symbol' in the + body." + ([symbol test then] + `(let [~symbol ~test] + (if ~symbol ~then))) + ([symbol test then else] + `(let [~symbol ~test] + (if ~symbol ~then ~else)))) + +(defmacro awhen + "Like 'when', but binds the result of the test to 'symbol' in the + body." + [symbol test & body] + `(aif ~symbol ~test + (do ~@body))) + +(defmacro acond + "Like 'cond', but binds the result of each test to 'symbol' in the + expression body." + [symbol & clauses] + (when clauses + (list 'aif symbol (first clauses) + (second clauses) + (cons 'acond (cons symbol (rest (rest clauses))))))) + |