Switch branches/tags
clojure-1.9.0-alpha17 clojure-1.9.0-alpha16 clojure-1.9.0-alpha15 clojure-1.9.0-alpha14 clojure-1.9.0-alpha13 clojure-1.9.0-alpha12 clojure-1.9.0-alpha11 clojure-1.9.0-alpha10 clojure-1.9.0-alpha9 clojure-1.9.0-alpha8 clojure-1.9.0-alpha7 clojure-1.9.0-alpha6 clojure-1.9.0-alpha5 clojure-1.9.0-alpha4 clojure-1.9.0-alpha3 clojure-1.9.0-alpha2 clojure-1.9.0-alpha1 clojure-1.8.0 clojure-1.8.0-beta2 clojure-1.8.0-beta1 clojure-1.8.0-alpha5 clojure-1.8.0-alpha4 clojure-1.8.0-alpha3 clojure-1.8.0-alpha2 clojure-1.8.0-alpha1 clojure-1.8.0-RC5 clojure-1.8.0-RC4 clojure-1.8.0-RC3 clojure-1.8.0-RC2 clojure-1.8.0-RC1 clojure-1.7.0 clojure-1.7.0-beta3 clojure-1.7.0-beta2 clojure-1.7.0-beta1 clojure-1.7.0-alpha6 clojure-1.7.0-alpha5 clojure-1.7.0-alpha4 clojure-1.7.0-alpha3 clojure-1.7.0-alpha2 clojure-1.7.0-alpha1 clojure-1.7.0-RC2 clojure-1.7.0-RC1 clojure-1.6.0 clojure-1.6.0-beta2 clojure-1.6.0-beta1 clojure-1.6.0-alpha3 clojure-1.6.0-alpha2 clojure-1.6.0-alpha1 clojure-1.6.0-RC4 clojure-1.6.0-RC3 clojure-1.6.0-RC2 clojure-1.6.0-RC1 clojure-1.5.1 clojure-1.5.0 clojure-1.5.0-beta13 clojure-1.5.0-beta12 clojure-1.5.0-beta11 clojure-1.5.0-beta10 clojure-1.5.0-beta9 clojure-1.5.0-beta8 clojure-1.5.0-beta7 clojure-1.5.0-beta2 clojure-1.5.0-beta1 clojure-1.5.0-alpha7 clojure-1.5.0-alpha6 clojure-1.5.0-alpha5 clojure-1.5.0-alpha4 clojure-1.5.0-alpha3 clojure-1.5.0-alpha2 clojure-1.5.0-alpha1 clojure-1.5.0-RC17 clojure-1.5.0-RC16 clojure-1.5.0-RC15 clojure-1.5.0-RC14 clojure-1.5.0-RC6 clojure-1.5.0-RC5 clojure-1.5.0-RC4 clojure-1.5.0-RC3 clojure-1.5.0-RC2 clojure-1.5.0-RC1 clojure-1.4.0 clojure-1.4.0-beta7 clojure-1.4.0-beta6 clojure-1.4.0-beta5 clojure-1.4.0-beta4 clojure-1.4.0-beta3 clojure-1.4.0-beta2 clojure-1.4.0-beta1 clojure-1.4.0-alpha5 clojure-1.4.0-alpha4 clojure-1.4.0-alpha3 clojure-1.4.0-alpha2 clojure-1.4.0-alpha1 clojure-1.3.0 clojure-1.3.0-beta3 clojure-1.3.0-beta2 clojure-1.3.0-beta1 clojure-1.3.0-alpha8 clojure-1.3.0-alpha7 clojure-1.3.0-alpha6
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
56 lines (45 sloc) 1.91 KB
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (
; which can be found in the file epl-v10.html 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.
;;; template.clj - anonymous functions that pre-evaluate sub-expressions
;; By Stuart Sierra
;; June 23, 2009
;; June 23, 2009: complete rewrite, eliminated _1,_2,... argument
;; syntax
;; January 20, 2009: added "template?" and checks for valid template
;; expressions.
;; December 15, 2008: first version
(ns ^{:doc "Macros that expand to repeated copies of a template expression."
:author "Stuart Sierra"}
(:require [clojure.walk :as walk]))
(defn apply-template
"For use in macros. argv is an argument list, as in defn. expr is
a quoted expression using the symbols in argv. values is a sequence
of values to be used for the arguments.
apply-template will recursively replace argument symbols in expr
with their corresponding values, returning a modified expr.
Example: (apply-template '[x] '(+ x x) '[2])
;=> (+ 2 2)"
[argv expr values]
(assert (vector? argv))
(assert (every? symbol? argv))
(walk/postwalk-replace (zipmap argv values) expr))
(defmacro do-template
"Repeatedly copies expr (in a do block) for each group of arguments
in values. values are automatically partitioned by the number of
arguments in argv, an argument vector as in defn.
Example: (macroexpand '(do-template [x y] (+ y x) 2 4 3 5))
;=> (do (+ 4 2) (+ 5 3))"
[argv expr & values]
(let [c (count argv)]
`(do ~@(map (fn [a] (apply-template argv expr a))
(partition c values)))))