Skip to content

Commit e950090

Browse files
committed
add lesson 25
1 parent 3b317a5 commit e950090

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

otus-25/project.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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"]}})

otus-25/src/otus_25/core.clj

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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))

0 commit comments

Comments
 (0)