Skip to content

Instantly share code, notes, and snippets.

@making
Created March 10, 2010 16:37
Show Gist options
  • Save making/328042 to your computer and use it in GitHub Desktop.
Save making/328042 to your computer and use it in GitHub Desktop.
(defmacro definc [x]
`(do ~@(map (fn [i] `(defn ~(symbol (str "inc" i)) [y#] (+ ~i y#))) (range x))))
(defmacro cond10 [x]
`(cond ~@(reduce into [] (map (fn [i] `((= ~x ~i) ~(str "hoge" i))) (range 10)))))
(defmacro def-with-log [fname args & body]
`(defn ~fname ~args
(println "begin" '~fname ~@args)
(let [ret# (do ~@body)]
(println "end" '~fname ret#)
ret#)))
(comment
(definc 10)
-->
(do
(defn inc0 [y__3444__auto__] (+ 0 y__3444__auto__))
(defn inc1 [y__3444__auto__] (+ 1 y__3444__auto__))
(defn inc2 [y__3444__auto__] (+ 2 y__3444__auto__))
(defn inc3 [y__3444__auto__] (+ 3 y__3444__auto__))
(defn inc4 [y__3444__auto__] (+ 4 y__3444__auto__))
(defn inc5 [y__3444__auto__] (+ 5 y__3444__auto__))
(defn inc6 [y__3444__auto__] (+ 6 y__3444__auto__))
(defn inc7 [y__3444__auto__] (+ 7 y__3444__auto__))
(defn inc8 [y__3444__auto__] (+ 8 y__3444__auto__))
(defn inc9 [y__3444__auto__] (+ 9 y__3444__auto__)))
)
(comment
(cond10 a)
-->
(cond
(= a 0) "hoge0"
(= a 1) "hoge1"
(= a 2) "hoge2"
(= a 3) "hoge3"
(= a 4) "hoge4"
(= a 5) "hoge5"
(= a 6) "hoge6"
(= a 7) "hoge7"
(= a 8) "hoge8"
(= a 9) "hoge9")
)
(comment
(def-with-log hello [x y]
(println "hello world")
(+ x y))
-->
(defn hello [x y]
(println "begin" 'hello x y)
(let [ret__5030__auto__ (do (println "hello world") (+ x y))]
(println "end" 'hello ret__5030__auto__)
ret__5030__auto__))
;; user> (hello 100 200)
;; begin hello 100 200
;; hello world
;; end hello 300
;; 300
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment