-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathcode.clj
More file actions
182 lines (127 loc) · 2.7 KB
/
code.clj
File metadata and controls
182 lines (127 loc) · 2.7 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
(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
(str "To understand what recursion is," " you must first understand recursion.")
"To understand what recursion is, you must first understand recursion."
(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
(#(+ 1 %) 3)
; => 4
(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)
true
; => true
false
; => false
{}
; => {}
:huzzah
; => :huzzah
()
; => ()
(if true :a :b)
; => :a
if
; => CompilerException java.lang.RuntimeException: Unable to resolve symbol: if in this context, compiling:(NO_SOURCE_PATH:0:0)
(let [x 5]
(+ x 3))
; => 8
(def x 15)
(+ x 3)
; => 18
(def x 15)
(let [x 5]
(+ x 3))
; => 8
(let [x 5]
(let [x 6]
(+ x 3)))
; => 9
(defn exclaim
[exclamation]
(str exclamation "!"))
(exclaim "Hadoken")
; => "Hadoken!"
(map inc [1 2 3])
; => (2 3 4)
(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 "()"))
; => ()
(+ 1 2)
; => 3
(+ 1 (+ 2 3))
; => 6
(if true 1 2)
; => 1
'(a b c)
(quote (a b c))
(read-string "(1 + 1)")
; => (1 + 1)
(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
(defmacro ignore-last-operand
[function-call]
(butlast function-call))
(ignore-last-operand (+ 1 2 10))
; => 3
;; This will not print anything
(ignore-last-operand (+ 1 2 (println "look at me!!!")))
; => 3
(macroexpand '(ignore-last-operand (+ 1 2 10)))
; => (+ 1 2)
(macroexpand '(ignore-last-operand (+ 1 2 (println "look at me!!!"))))
; => (+ 1 2)
(defmacro infix
[infixed]
(list (second infixed)
(first infixed)
(last infixed)))
(infix (1 + 2))
; => 3
(defn read-resource
"Read a resource into a string"
[path]
(read-string (slurp (clojure.java.io/resource path))))
(defn read-resource
[path]
(-> path
clojure.java.io/resource
slurp
read-string))