File tree Expand file tree Collapse file tree 2 files changed +86
-0
lines changed Expand file tree Collapse file tree 2 files changed +86
-0
lines changed Original file line number Diff line number Diff line change
1
+ (defproject otus-25 " 0.1.0-SNAPSHOT"
2
+ :description " FIXME: write description"
3
+ :url " http://example.com/FIXME"
4
+ :license {:name " EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
5
+ :url " https://www.eclipse.org/legal/epl-2.0/" }
6
+ :dependencies [[org.clojure/clojure " 1.11.1" ]]
7
+ :target-path " target/%s"
8
+ :profiles {:uberjar {:aot :all
9
+ :jvm-opts [" -Dclojure.compiler.direct-linking=true" ]}})
Original file line number Diff line number Diff line change
1
+ (ns otus-25.core
2
+ (:require [clojure.walk :refer [macroexpand-all]])
3
+ (:gen-class ))
4
+
5
+ ; ; * Макросы
6
+
7
+ ; ; ** quote и eval
8
+
9
+ (quote (asd qwe hjk [123 :foo (asd )]))
10
+
11
+ (eval (concat (quote (+ 1 ) (list 2 3 ))))
12
+
13
+ ; ; ** defmacro
14
+
15
+ ; ; (append-2-3 (+ 1)) => (+ 1 2 3)
16
+ ; ; (append-2-3 (println)) => (println 2 3)
17
+ (defmacro append-2-3 [expr]
18
+ (concat expr (list 2 3 )))
19
+
20
+ ; ; ** Отладка
21
+
22
+ (macroexpand-1 '(append-2-3 (println )))
23
+ (macroexpand '(append-2-3 (println )))
24
+
25
+ (macroexpand-1 '(append-2-3 (when true )))
26
+ (macroexpand '(append-2-3 (when true )))
27
+
28
+ (macroexpand-all '(cond (< 1 2 ) 42
29
+ nil :foo
30
+ true :oops ))
31
+
32
+ ; ; ** syntax-quote
33
+
34
+ (let [x 42 ] `(+ ~(+ 3 4 ) ~x ~x))
35
+
36
+ (defmacro append-2-3 [expr]
37
+ `(~@expr 2 3 ))
38
+
39
+ (defmacro when? [cond & body]
40
+ `(if ~cond
41
+ (do ~@(apply concat (map (fn [_] body)) [1 2 3 ]))))
42
+
43
+ (macroexpand-1 (when? true
44
+ (println 1 )
45
+ (println 2 )))
46
+
47
+ ; ; ** анафорические макросы
48
+
49
+ (defmacro inject-x [& body]
50
+ `(let [~'x 42 ] ~@body))
51
+
52
+ (comment
53
+ (inject-x (println x)))
54
+
55
+ ; ; ** gensym и #
56
+
57
+ (defmacro tap [expr]
58
+ (let [name (gensym )]
59
+ `(let [~name ~expr]
60
+ (println '~expr " =>" ~name)
61
+ ~name)))
62
+
63
+ (comment
64
+ (tap (+ 1 2 )))
65
+
66
+ (defmacro tap [expr]
67
+ `(let [name# ~expr]
68
+ (println '~expr " =>" name#)
69
+ name#))
70
+
71
+ (macroexpand-1
72
+ '(tap (+ 1 3 )))
73
+
74
+ ; ; ** &env и &form
75
+
76
+ (comment
77
+ (@#some-macro '(some-macro x) {'x nil } 'x))
You can’t perform that action at this time.
0 commit comments