Skip to content

Instantly share code, notes, and snippets.

@rascio
Last active November 27, 2019 09:33
Show Gist options
  • Save rascio/1664be2fb57b1ec60242f4c8b9533b1d to your computer and use it in GitHub Desktop.
Save rascio/1664be2fb57b1ec60242f4c8b9533b1d to your computer and use it in GitHub Desktop.
nil
"Hello World!"
:hello-world!
:what-is-the-difference-with-a-string?
:this-is-like-an-infinite-enum
:can-be/namespaced
:so-not-the-same-as/namespaced
0
1
2
true
false
[1 1 3 5 8 13 21 34 55]
#{1 1 3 5 8 13 21 34 55} ;throws error
{:name "Boris"
:year "2007"
:characters [{:name "Renè Ferretti"
:role :director}
{:name "Corinna Negri"
:role :main-actress}]
:rate 9/10}
(println "Ok, Hello World")
(println "Ok," (str "Nested forms are evaluated"))
'(What kind of sorcery is this?)
(quote (it is like writing this))
['or 'this (+ 1 1)]
(+ 1 1)
(or true false)
(or false false false false true false)
(and 1 true "something" nil false)
(or :false nil false 0)
(or (not false) false)
(and (and true true) (not false))
(and (and true
true)
(not (or false
nil)))
(println "Programmer joke"
'(not false)
"it's funny because it's"
(not false))
((or false println) "Was it fun?")
(* 5 2)
(* 5 2 (+ 1 1))
(def first-variable true)
(println "First variable is" first-variable)
(if first-variable
("3rd argument is the expression when it's true")
("4th when is false"))
(if (not first-variable)
(println "mmmm")
(do (println "With 'do'")
(println "More expressions can be written")
(println "To be executed in sequence")
(println "Last form will be the returned value")
"so, it's me!"))
(first [1 2 3])
(second [1 2 3])
(nth [1 2 3] 2)
(inc 1)
(fn [x] (+ 1 x))
((fn [x] (+ 1 x)) 1)
(def increment-number (fn [n] (+ 1 n)))
;think of:
;(def define-function
; [name arguments & body]
; (def name (fn arguments body)))
;(define-function increment-number
; [n]
; (+ 1 n))
(defn increment-number
[n]
(+ 1 n))
(increment-number 1)
(map inc [1 2 3])
(map (partial + 1) [1 2 3])
(map #(+ 1 %) [1 2 3])
(map (fn [x] (+ 1 x)) [1 2 3])
(def valid-color? #{:red
:green
:blue})
(filter valid-color?
[:brown :magenta :green :yellow :blue])
(defn 💩
[what]
(println what "ènammerda!"))
(💩 "Il cloud")
(when (not false)
(println "Not false!")
(+ 1 1))
(macroexpand '(when (not false)
(println "Not false!")
(+ 1 1)))
(defn debug-fn [f & args]
(println "f:" f " args:" args)
(cons f args))
(debug-fn + 1 1); -> (cons + '(1 1)) -> '(+ 1 1)
;(quote (1)) = '(1)
;(quote (1 2)) = '(1 2)
;(quote (1 (2 3))) = '(1 (2 3))
;(quote (1 (+ 2 3))) = '(1 (+ 2 3))
;(second (quote (1 (+ 2 3)))) = (second '(1 (+ 2 3))) -> '(+ 2 3)
;(first (second '(1 (+ 2 3)))) -> +
;(eval '(+ 1 2)) -> 3
;(eval (list + 1 2)) -> (eval '(+ 1 2)) -> 3
;(type 'n) -> Symbol
;(type (quote 5)) -> Number
;(type (first (quote + 1 2))) -> (type (first '(+ 1 2))) -> (type '+) -> Symbol
; -> evaluation
; => macro evaluation
(defmacro debug-v0 [a b]
(println "args:" a b)
(list '+ a b))
(debug-v0 1 2); -> (list '+ 1 2) -> '(+ 1 2) => 3
;args: 1 2
(defmacro debug-v1 [& args]
(println "args:" args "type of first:" (type (first args)))
args)
(debug-v1 + 1 1); -> '(+ 1 1) => 2
;args: (+ 1 1) type of first: clojure.lang.Symbol
(defmacro debug-v2 [f & args]
(println "f:" f " args:" args)
(cons f args))
(debug-v2 + 1 2); -> (cons '+ '(1 2)) -> '(+ 1 2) => 3
;f: + args: (1 2)
(def n 5)
(debug-v2 + 1 n); -> (cons '+ '(1 n)) -> '(+ 1 n) => (+ 1 5) -> 6
;f: + args: (1 n)
(defmacro debug-v3 [f & args]
(println "f:" f " args:" args)
(list 'apply f (vec args)))
(debug-v3 + 1 2); -> (list 'apply '+ (vec '(1 2)))
; -> (list 'apply '+ [1 2])
; -> '(apply + [1 2]))
; => (apply + [1 2])
; -> 3)
(debug-v3 + 1 (+ 1 1)); -> (list 'apply '+ (vec '(1 (+ 1 1))))
; -> (list 'apply '+ [1 '(+ 1 1)]))
; -> '(apply + [1 (+ 1 1)]))
; => (apply + [1 (+ 1 1)])
; -> (apply + [1 2])
; -> 3
;f: + args: (1 (+ 1 1))
(defmacro debug-v4 "Filters out odd numbers in args [BUG]" [f & args]
(println "f:" f " args:" args "filtered:" (filter even? args))
(cons f (filter even? args)))
(debug-v4 + 1 2 3 4); -> (cons + (filter even? '(1 2 3 4))) -> (cons '+ '(2 4)) -> '(+ 2 4) => 6
; |- (even? 1)
; |- (even? 2)
; |- (even? 3)
; |- (even? 4)
(debug-v4 + 1 n); -> (cons '+ (filter even? '(1 n)))
; |- (even? 1)
; |- (even? 'n)
; Syntax error macroexpanding debug-v3 at...
; Argument must be an integer: n
; class clojure.lang.Compiler$CompilerException
;Predicate for *numbers* that are even
(defn- even-number? [n] (and (number? n) (even? n)))
(defmacro debug-v5 "Filters out odd numbers in args" [f & args]
(println "f:" f " args:" args)
(list 'apply f (list 'filter 'even? (vec args))))
(debug-v5 + 1 2 3 4); -> (list 'apply '+ (list 'filter 'even? (vec '(1 2 3 4)))
; -> (list 'apply '+ (list 'filter 'even? [1 2 3 4]))
; -> (list 'apply '+ '(filter even? [1 2 3 4]))
; -> '(apply + (filter even? [1 2 3 4]))
; => (apply + '(2 4)) = (+ 2 4)
; -> 6
(debug-v5 + 1 n); -> (list 'apply '+ (list 'filter 'even? (vec '(1 n)))
; -> (list 'apply '+ (list 'filter 'even? [1 'n]))
; -> (list 'apply '+ '(filter even? [1 n]))
; -> '(apply + (filter even? [1 n]))
; => (apply + (filter even? [1 5]))
; -> (apply + '()) = (+)
; -> 0
(defmacro debug-v6 [f & args]
(println "f:" f " args:" args)
(let [a (vec args)]
`(apply ~f ~a)))
(debug-v6 + 1 2); -> (list 'clojure.core/apply '+ (vec args))
; -> (list 'clojure.core/apply '+ (vec '(1 2)))
; -> (list 'clojure.core/apply '+ [1 2])
; -> '(clojure.core/apply + [1 2])
; => 3
;f: + args: (1 2)
(debug-v6 + 1 n); -> (list 'clojure.core/apply '+ (vec args))
; -> (list 'clojure.core/apply '+ (vec '(1 n)))
; -> (list 'clojure.core/apply '+ [1 'n])
; -> '(clojure.core/apply + [1 n])
; => (clojure.core/apply + [1 5])
; -> 3
;f: + args: (1 n)
(defmacro debug-v7 "Add 1 in the f args" [f & args]
(println "f:" f " args:" args)
`(~f 1 ~@args))
(debug-v7 + 1 2); -> (list '+ 1 ~@(1 2)) = (concat (list '+ 1) '(1 2))
; -> (list '+ 1 1 2)
; -> '(+ 1 1 2)
; => 4
;f: + args: (1 2)
(defmacro debug-v8 "Add some inputs" [f & args]
(println "f:" f " args:" args)
`(~f (+ 1 2) ~(+ 2 3) ~@args))
(debug-v8 + 1 2 3 4); -> (list '+ (list '+ 1 2) (+ 2 3) ~@(1 2 3 4)))
; -> (list '+ (list '+ 1 2) (+ 2 3) 1 2 3 4)
; -> (list '+ '(+ 1 2) (+ 2 3) 1 2 3 4)
; -> (list '+ '(+ 1 2) 5 1 2 3 4)
; -> '(+ (+ 1 2) 5 1 2 3 4)
; => (+ 3 5 1 2 3 4)
; -> 18
;f: + a: 1 b: 2
(defmacro debug-v9 "Filters out odd java.lang.Number in args" [f & args]
(println "f:" f " args:" args)
`(~f ~@(filter even-number? args)))
(debug-v9 + 1 2 n); -> (list '+ ~@(filter even-number? '(1 2 n)))
; | (even-number? 1)
; | (even-number? 2)
; | (even-number? 'n)
; -> (list '+ ~@(2))
; -> (list '+ 2)
; -> '(+ 2)
; => 2
;f: + args: (1 2 n)
(defmacro debug-v10 "Filters out odd java.lang.Number in args" [f & args]
(println "f:" f " args:" args)
`(apply ~f (filter even? ~(vec args))))
(debug-v10 + 1 2 n); -> (list 'apply '+ (list 'filter 'even? (vec '(1 2 n)))
; -> (list 'apply '+ (list 'filter 'even? [1 2 'n]))
; -> (list 'apply '+ '(filter even? [1 2 n]))
; -> '(apply + (filter even? [1 2 n]))
; => (apply + (filter even? [1 2 5]))
; -> (apply + [2])
; -> 2
;f: + args: (1 2 n)
(defmacro debug-v11 [f & args]
`(do
(println ~f (quote ~args) ~@args)
(~f ~@args)))
(debug-v11 + 1 n (+ 1 1)); -> (list 'do
; (list 'println '+ '(quote (1 n)) ~@(1 n))
; (list '+ ~@(1 n))
; -> (list 'do
; '(println + (quote (1 n)) 1 n)
; '(+ 1 n)
; -> '(do
; (println + (quote (1 n)) 1 n)
; (+ 1 n))
; => (do
; (println + '(1 n) 1 5)
; (+ 1 5))
; -> (do
; nil
; (+ 1 5))
; -> (do nil 6)
; -> 6
;#object[clojure.core$_PLUS_ ...] (1 n) 1 5
(defmacro debug-v12 [f & args]
`(let [res# ~(cons f args)]
(println "f:" (quote ~f) "args:" ~@args "res:" res#)
res#))
(debug-v12 + 1 2 3 4);-> (list 'let ['res (cons '+ '(1 2 3 4))])
; '(println "f:" (quote +) "args:" 1 2 3 4 "res" res)
; res)
; -> (list let [res '(+ 1 2 3 4)])
; '(println "f:" (quote +) "args:" 1 2 3 4 "res" res)
; res)
; -> '(let [res (+ 1 2 3 4)]
; (println "f:" (quote +) "args:" 1 2 3 4 "res" res)
; res)
; => (let [res 10]
; (println "f:" (quote +) "args:" 1 2 3 4 "res" 10)
; 10)
; -> (let [res 10]
; nil
; 10)
; -> 10
;f: #object[clojure.core$_PLUS_ ...] args: 1 2 3 4 res: 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment