diff --git a/ch1-examples.txt b/ch1-examples.txt new file mode 100644 index 0000000..a2b13a1 --- /dev/null +++ b/ch1-examples.txt @@ -0,0 +1,358 @@ +(defn average + [numbers] + (/ (apply + numbers) (count numbers))) + +(average [60 80 100 400]) +(println (average [60 80 100 400])) + +60; +[60 80 100 400]; +(average [60 80 100 400]); +(+ 1 2) +(not 1) +(Math/pow 2 10) +(instance? java.util.List [1 2 3]) + +(read-string "42") +(read-string "(+ 1 2)") +[60 80 100 400] +(pr-str [1 2 3]) +(read-string "[1 2 3]") + +"Hello there" +"multiline strings +are very handy" +(class \c) +\u00ff +\o41 +\space; +\newline; +\formfeed; +\backspace; +\tab; + +(def person {:name "Sandra Cruz" + :city "Portland, ME"}) +(:city person) + +(def pizza {:name "Ramunto's" + :location "Claremont, NH" + ::location "43.3734 -72.3365"}) +(:user/location pizza) + +(name :user/location) +(namespace :user/location) +(namespace :location) + +42 +0xff +2r111 +040 +3.14 +6.0221415e23 +42N +0.01M +22/7 + +(class #"(p|h)ail") + +(re-seq #"(\d+)-(\d+)" "1-3") +(read-string "(+ 1 2 #_(* 2 2) 8)") +(when true + (comment (print "hello"))) +(+ 1 2 (comment (* 2 2)) 8) +(defn silly-adder + [x, y] + (+, x, y)) + +(= [1 2 3][1, 2, 3]) +(create-user {:name new-username, :email email}) +*ns* +(ns foo) +(def x "hello") +user/x +(ns user) +clojure.core$filter +(average [60 80 10]) +(class average) +(type average) + +(quote x) +(symbol? (quote x)) +'x +'(+ x x) +(list? '(+ x x)) +(list '+ 'x 'x) +''x +'@x +'#(+ % %) +''(a b ~c) +(do + (println "hi") + (apply * [4 5 6])) +(do + (println "hi") + (apply * [4 5 6])) + +(let [a (inc (rand-int 6)) + b (inc (rand-int 6))] + (println (format "You rolled a %s and a %s" a b)) + (+ a b)) + + (def p "foo") + p +(defn hypot + + [x y] + (let [x2 (* x y) + y2 (* y y)] + (Math/sqrt (+ x2 y2)))) + +(def v [42 "foo" 99.2 [5 12]]) +(first v) +(second v) +(last v) +(nth v 2) +(v 2) +(.get v 2) +(+ (first v) (v 2)) +(+ (first v) (first (last v))) + +(let [[x y z] v] + (+ x z)) +(let [x (nth v 0) + y (nth v 1) + z (nth v 2)] + (+ x z)) + +(let [[x _ _ [y z]] v] + (+ x y z)) + +(let [[x & rest] v] +rest) + +(let [[x _ z :as original-vector] v] + (conj original-vector (+ x z))) + +(def m {:a 5 :b 6 + :c [7 8 9] + :d {:e 10 :f 11} + "foo" 88 + 42 false}) + +(let [{a :a b :b} m] + (+ a b)) +(let [{f "foo"} m] + (+ f 12)) +(let [{v 42} m] + (if v 1 0)) +(let [{a 42} m] + (if a 1 0)) +(let [{a 42} m] + 42) +(let [{x 3 y 8} [12 0 0 -18 44 6 0 0 1]] + (+ x y)) +(let [{x 0 y 1} [12 0 0 -18 44 6 0 0 1]] + (+ x y)) + +(let [{{e :e} :d} m] + (* 2 e)) + +(let [{{e :f} :d} m] + (* 2 e)) + +(let [{[x _ y] :c} m] + (+ x y)) + +(def map-in-vector ["James" {:birthday (java.util.Date. 73 1 6)}]) + +(let [[name {bd :birthday}] map-in-vector] + (str name " was born on " bd)) + +(let [{r1 :x r2 :y :as randoms} + (zipmap[:x :y :z] (repeatedly (partial rand-int 10)))] + (assoc randoms :sum (+ r1 r2))) + +(let [{k :unknown x :a + :or {k 50}} m] + (+ k x)) + +(let [{k :unknown x :a} m + k (or k 50)] + (+ k x)) + +(let [{opt1 :option} {:option false} + opt1 (or opt1 true) + {opt2 :option :or {opt2 true}} {:option false}] + {:opt1 opt1 :opt2 opt2}) + +(def chas {:name "Chas" :age 31 :location "Massachusetts"}) + +(let [{name :name age :age location :location} chas] + (format "%s is %s years old and lives in %s." name age location)) + +(let [{:keys [name age location]} chas] + (format "%s is %s years old and lives in %s." name age location)) + +(def brain {"name" "Brain" "age" 31 "location" "British Columbia"}) + +(let [{:strs [name age location]} brain] + (format "%s is %s years old and lives in %s." name age location)) + +(def christophe {'name "Christophe" 'age 33 'location "Rhone-Alpes"}) + +(let [{:syms [name age location]} christophe] + (format "%s is %s years old and lives in %s." name age location)) + +(def user-info ["robert8990" 2011 :name "Bob" :city "Boston"]) + +(let [[username account-year & extra-info] user-info + {:keys [name city]} (apply hash-map extra-info)] + (format "%s is in %s" name city)) + +(let [[username account-year & {:keys [name city]}] user-info] + (format "%s is in %s" name city)) + +(fn [x] + (+ 10 x)) + +((fn [x] (+ 10 x)) 8) + +(let[x 8] + (+ 10 x)) + +((fn [x y z] (+ x y z)) + 3 4 12) + +(let [x 3 + y 4 + z 12] + (+ x y z)) + +(def strange-adder (fn adder-self-reference + ([x] (adder-self-reference x 1)) + ([x y] (+ x y)))) + +(strange-adder 10) +(strange-adder 10 50) + +(letfn [(odd? [n] + (even? (dec n))) + (even? [n] + (or (zero? n) + (odd? (dec n))))] + (odd? 11)) + +(def strange-adder (fn strange-adder + ([x] (strange-adder x 1)) + ([x y] (+ x y)))) + +(defn strange-adder + ([x] (strange-adder x 1)) + ([x y] (+ x y))) + +(def redundant-adder (fn redundant-adder + [x y z] + (+ x y z))) + +(defn redundant-adder + [x y z] + (+ x y z)) + +(defn concat-rest + [x & rest] + (apply str (butlast rest))) + +(concat-rest 0 1 2 3 4) + +(defn make-user + [& [user-id]] + {:user-id (or user-id + (str (java.util.UUID/randomUUID)))}) + +(make-user) +(make-user "Bobby") + +(defn make-user + [username & {:keys [email join-date] + :or {join-date (java.util.Date.)}}] + {:username username + :join-date join-date + :email email + ;; 2.592e9 -> один месяц в мсек + :exp-date (java.util.Date. (long (+ 2.592e9 (.getTime join-date))))}) + +(make-user "Bobby") +(make-user "Bobby" + :join-date (java.util.Date. 111 0 1) + :email "bobby@example.com") + + (fn [x & rest] + (- x (apply + rest))) + +#(- % (apply + %&)) + +(fn [x] + (fn [y] + (+ x y))) + +;illegal expression +#(#(+ % %)) + +;if +(if "hi" \t) +(if 42 \t) +(if nil "unevalueted" \f) +(if false "unevalueted" \f) +(if (not true) \t) + +(true? "string") +(false? "string") +(false? false) +(false? nil) +(true? true) +(if "string" \t \f) + +(loop [x 5] + (if (neg? x) + x + (recur (dec x)))) + +(defn countdown + [x] + (if (zero? x) + :blastoff! + (do (println x) + (recur (dec x))))) +(countdown 5) + +(def x 5) + +x + +(var x) + +(java.util.ArrayList. 100) +(Math/pow 2 10) +(.substring "hello" 1 3) +Integer/MAX_VALUE + +(eval :foo) +(eval [1 2 3]) +(eval "text") +(eval '(average [60 80 100 400])) +(eval (read-string "(average [60 80 100 400])")) + +(defn embedded-repl + "A naive Clojure REPL implementation . Enter ':quit' + to exit." + [] + (print (str (ns-name *ns*) ">>> ")) + (flush) + (let [expr (read) + value (eval expr)] + (when (not= :quit value) + (println value) + (recur)))) + +(embedded-repl) \ No newline at end of file