Back
defmacro (clj)
(source)variable
(defmacro name doc-string? attr-map? [params*] body)
(defmacro name doc-string? attr-map? ([params*] body) + attr-map?)
Like defn, but the resulting function name is declared as a
macro and will be used as a macro by the compiler when it is
called.
Examples
clojure
(ns clojure.test-clojure.reducers
(:require [clojure.core.reducers :as r]
[clojure.test.generative :refer (defspec)]
[clojure.data.generators :as gen])
(:use clojure.test))
(defmacro defequivtest
;; f is the core fn, r is the reducers equivalent, rt is the reducible ->
;; coll transformer
[name [f r rt] fns]
`(deftest ~name
(let [c# (range -100 1000)]
(doseq [fn# ~fns]
(is (= (~f fn# c#)
(~rt (~r fn# c#))))))))
logseq/logseq
(ns frontend.pubsub
"All mults and pubs are collected to this ns.
vars with suffix '-mult' is a/Mult, use a/tap and a/untap on them. used by event subscribers
vars with suffix '-pub' is a/Pub, use a/sub and a/unsub on them. used by event subscribers
vars with suffix '-ch' is chan used by event publishers."
{:clj-kondo/config {:linters {:unresolved-symbol {:level :off}}}}
#?(:cljs (:require-macros [frontend.pubsub :refer [def-mult-or-pub chan-of]]))
(:require [clojure.core.async :as a :refer [chan mult pub]]
[clojure.core.async.impl.protocols :as ap]
[malli.core :as m]
[malli.dev.pretty :as mdp]
[clojure.pprint :as pp]))
;;; helper macro
(defmacro chan-of [malli-schema malli-schema-validator & chan-args]
`(let [ch# (chan ~@chan-args)]
(reify
ap/ReadPort
(~'take! [~'_ fn1-handler#]
(ap/take! ch# fn1-handler#))
ap/WritePort
(~'put! [~'_ val# fn1-handler#]
(if (~malli-schema-validator val#)
(ap/put! ch# val# fn1-handler#)
(do (mdp/explain ~malli-schema val#)
(throw (ex-info "validate chan value failed" {:val val#}))))))))
(defmacro def-mult-or-pub
"define following vars:
- `symbol-name`-ch for event publisher.
- `symbol-name`-mult or `symbol-name`-pub for event subscribers.
- `symbol-name`-validator is malli schema validator
def -pub var when `:topic-fn` exists otherwise -mult var"
[symbol-name doc-string malli-schema & {:keys [ch-buffer topic-fn]
:or {ch-buffer 1}}]
(let [schema-validator-name (symbol (str symbol-name "-validator"))
schema-name (symbol (str symbol-name "-schema"))
ch-name (symbol (str symbol-name "-ch"))
mult-or-pub-name (if topic-fn
(symbol (str symbol-name "-pub"))
(symbol (str symbol-name "-mult")))
doc-string* (str doc-string "\nMalli-schema:\n" (with-out-str (pp/pprint malli-schema)))]
`(do
(def ~schema-name ~malli-schema)
(def ~schema-validator-name (m/validator ~malli-schema))
(def ~ch-name ~doc-string* (chan-of ~malli-schema ~schema-validator-name ~ch-buffer))
~(if topic-fn
`(def ~mult-or-pub-name ~doc-string* (pub ~ch-name ~topic-fn))
`(def ~mult-or-pub-name ~doc-string* (mult ~ch-name))))))
pedestal/pedestal
(ns io.pedestal.interceptor.error
(:require [io.pedestal.interceptor :as interceptor]
[clojure.core.match :as match]))
(defmacro error-dispatch
"Return an interceptor for doing pattern-matched error-dispatch, based on
the ex-data of the exception.
Pedestal wraps *all* exceptions in ex-info on error, providing the following
keys to match on: `:execution-id`, `:stage`, `:interceptor`, `:exception-type`
dundalek/closh
(defmacro def-eval []
(if (System/getenv "__CLOSH_USE_SCI_EVAL__")
`(do (require 'closh.zero.utils.sci)
(def ~'eval closh.zero.utils.sci/sci-eval))
`(def ~'eval clojure.core/eval)))
(defmacro eval-closh-requires []
(when-not (System/getenv "__CLOSH_USE_SCI_EVAL__")
`(eval closh.zero.env/*closh-environment-requires*)))
originrose/cortex
(ns cortex.nn.impl
"Implementation helpers to aid implementing neural network cortex protocols
or specific neural network layers"
(:require [cortex.nn.layers :as layers]
[clojure.core.matrix.macros :refer [c-for]]))
(defmacro convolution-outer-kernel
[conv-desc & body]
`(let [~'conv-desc ~conv-desc
~'output-width (long (:output-width ~'conv-desc))
~'output-height (long (:output-height ~'conv-desc))
~'num-in-channels (long (:input-channels ~'conv-desc))
~'num-out-channels (long (:output-channels ~'conv-desc))
~'input-width (long (:input-width ~'conv-desc))
~'input-height (long (:input-height ~'conv-desc))
~'input-planar-stride (* ~'input-width ~'input-height)
~'output-planar-stride (* ~'output-width ~'output-height)
~'kernel-width (long (:kernel-width ~'conv-desc))
~'kernel-height (long (:kernel-height ~'conv-desc))
~'output-channel-stride (* ~'kernel-width ~'kernel-height)
~'output-column-stride (* ~'output-channel-stride ~'num-in-channels)
~'stride-y (long (:stride-y ~'conv-desc))
~'stride-x (long (:stride-x ~'conv-desc))
~'pad-x (long (:pad-x ~'conv-desc))
~'pad-y (long (:pad-y ~'conv-desc))
~'min-x (- 0 ~'pad-x)
~'min-y (- 0 ~'pad-y)
~'max-x (+ ~'input-width ~'pad-x)
~'max-y (+ ~'input-height ~'pad-y)
~'kernel-num-elems (* ~'kernel-width ~'kernel-height)]
(c-for
[~'chan 0 (< ~'chan ~'num-in-channels) (inc ~'chan)]
(let [~'chan-input-offset (* ~'chan ~'input-planar-stride)
~'chan-output-offset (* ~'chan ~'output-planar-stride)]
(c-for
[~'out-y 0 (< ~'out-y ~'output-height) (inc ~'out-y)]
(let [~'input-rel-y (- (* ~'out-y ~'stride-y) ~'pad-y)]
(c-for
[~'out-x 0 (< ~'out-x ~'output-width) (inc ~'out-x)]
(let [~'input-rel-x (- (* ~'out-x ~'stride-x) ~'pad-x)]
~@body))))))))
(defmacro in-bounds?
"is value within the range of [min-val, max-val)"
[value min-val max-val]
`(and (>= ~value ~min-val)
(< ~value ~max-val)))
(defmacro convolution-roll-unroll-inner-kernel
[& body]
`(let [~'chan-conv-offset (* ~'chan ~'output-channel-stride)
~'output-offset (+ (* ~'out-y ~'output-width)
~'out-x)
;;positive values for how far out we are into the padding
input-over-x# (max 0 (- (+ ~'input-rel-x ~'kernel-width)
~'input-width))
input-over-y# (max 0 (- (+ ~'input-rel-y ~'kernel-height)
~'input-height))
;;Negative values for how far before the 0 idx we are.
input-under-x# (min ~'input-rel-x 0)
input-under-y# (min ~'input-rel-y 0)
;;Width of the kernel excluding padding
~'exc-pad-width (max 0 (+ (- ~'kernel-width input-over-x#)
input-under-x#))
~'exc-pad-height (max 0 (+ (- ~'kernel-height input-over-y#)
input-under-y#))
~'exc-pad-kernel-num-elems (* ~'exc-pad-width ~'exc-pad-height)]
(c-for
[~'k-y 0 (< ~'k-y ~'kernel-height) (inc ~'k-y)]
(c-for
[~'k-x 0 (< ~'k-x ~'kernel-width) (inc ~'k-x)]
(let [~'input-x (+ ~'input-rel-x ~'k-x)
~'input-y (+ ~'input-rel-y ~'k-y)
~'output-conv-addr (+ (* ~'output-offset
~'output-column-stride)
~'chan-conv-offset
(* ~'k-y ~'kernel-width)
~'k-x)
~'input-addr (+ (* ~'input-y ~'input-width)
~'input-x
~'chan-input-offset)
~'input-valid? (and (in-bounds? ~'input-x 0 ~'input-width)
(in-bounds? ~'input-y 0 ~'input-height))
loop-valid?# (and (in-bounds? ~'input-x ~'min-x ~'max-x)
(in-bounds? ~'input-y ~'min-y ~'max-y))]
(when loop-valid?#
~@body))))))
hoplon/hoplon
(ns hoplon.binding
(:refer-clojure :exclude [binding bound-fn])
(:require [clojure.core :as clj]
[cljs.analyzer :as a]))
(defmacro binding
"See clojure.core/binding."
[bindings & body]
(let [env (assoc &env :ns (a/get-namespace a/*cljs-ns*))
value-exprs (take-nth 2 (rest bindings))
bind-syms (map #(:name (a/resolve-existing-var env %)) (take-nth 2 bindings))
bind-syms' (map (partial list 'quote) bind-syms)
set-syms (repeatedly (count bind-syms) gensym)
setfn (fn [x y]
{:push! `(fn []
(let [z# ~x]
(set! ~x ~y)
(fn [] (set! ~x z#))))})
thunkmaps (map setfn bind-syms set-syms)]
(a/confirm-bindings env bind-syms)
`(let [~@(interleave set-syms value-exprs)]
(hoplon.binding/push-thread-bindings ~(zipmap bind-syms' thunkmaps))
(try ~@body (finally (hoplon.binding/pop-thread-bindings))))))
(defmacro bound-fn
"See clojure.core/bound-fn."
[args & body]
`(hoplon.binding/bound-fn* (fn [~@args] ~@body)))