-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch7.scratch.clj
148 lines (106 loc) · 2.77 KB
/
ch7.scratch.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
(defmacro backwards
[form]
(reverse form))
(backwards (" backwards" " am" "I" str))
; => "I am backwards"
(def addition-list (list + 1 2))
(eval addition-list)
; => 3
(eval (concat addition-list [10]))
; => 13
(eval (list 'def 'lucky-number (concat addition-list [10])))
; => #'user/lucky-number
lucky-number
; => 13
;The Reader
;The reader converts the textual source code you save in a file
; or enter in the REPL into Clojure data structures.
(read-string "(+ 1 2)")
; => (+ 1 2)
(list? (read-string "(+ 1 2)"))
; => true
(conj (read-string "(+ 1 2)") :zagglewag)
; => (:zagglewag + 1 2)
(eval (read-string "(+ 1 2)"))
; => 3
(read-string "#(+ 1 %)")
; => (fn* [p1__423#] (+ 1 p1__423#))
(read-string "'(a b c)")
; => (quote (a b c))
(read-string "@var")
; => (clojure.core/deref var)
(read-string "; ignore!\n(+ 1 2)")
; => (+ 1 2)
;The Evaluator
;You can think of Clojure’s evaluator as a function that
; takes a data structure as an argument, processes the data
; structure using rules corresponding to the data structure’s
; type, and returns a result.
;These Things Evaluate to Themselves
true
; => true
false
; => false
{}
; => {}
:huzzah
; => :huzzah
()
; => ()
(read-string ("+"))
; => +
(type (read-string "+"))
; => clojure.lang.Symbol
(list (read-string "+") 1 2)
; => (+ 1 2)
(eval (list (read-string "+") 1 2))
; => 3
(eval (read-string "()"))
; => ()
(read-string "(1 + 1)")
; => (1 + 1)
;Clojure will throw an exception if you try to make it evaluate this list:
(eval (read-string "(1 + 1)"))
; => ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn
(let [infix (read-string "(1 + 1)")]
(list (second infix) (first infix) (last infix)))
; => (+ 1 1)
(eval
(let [infix (read-string "(1 + 1)")]
(list (second infix) (first infix) (last infix))))
; => 2
;Macros give you a convenient way to manipulate lists before Clojure evaluates
; them. Macros are a lot like functions: they take arguments and return a value,
; just like a function would.
(defmacro ignore-last-operand
[function-call]
(butlast function-call))
(ignore-last-operand (+ 1 2 10))
;; This will not print anything
(ignore-last-operand (+ 1 2 (println "look at me!!!")))
; => 3
; Inspect how the macros are expanded
(macroexpand '(ignore-last-operand (+ 1 2 10)))
; => (+ 1 2)
(macroexpand '(ignore-last-operand (+ 1 2 (println "look at me!!!"))))
; => (+ 1 2)
;Infix notation
(defmacro infix
[infixed]
(list (second infixed)
(first infixed)
(last infixed)))
(infix (1 + 2))
; => 3
;macros enable syntactic abstraction.
(defn read-resource
"Read a resource into a string"
[path]
(read-string (slurp (clojure.java.io/resource path))))
; Thread macros
(defn read-resource
[path]
(-> path
clojure.java.io/resource
slurp
read-string))