forked from bitsai/book-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch3.clj
135 lines (132 loc) · 3.8 KB
/
ch3.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
(new java.util.Random)
(def rnd (new java.util.Random))
(. rnd nextInt)
(. rnd nextInt 10)
(. Math PI)
(import '(java.util Random Locale)
'(java.text MessageFormat))
Random
Locale
MessageFormat
(Random.)
Math/PI
(System/currentTimeMillis)
(.nextInt rnd)
(.getLocation (.getCodeSource (.getProtectionDomain (.getClass '(1 2)))))
(.. '(1 2) getClass getProtectionDomain getCodeSource getLocation)
(doto (System/getProperties)
(.setProperty "name" "Stuart")
(.setProperty "favoriteColor" "blue"))
(make-array String 5)
(seq (make-array String 5))
(find-doc "-array")
(defn painful-array []
(let [arr (make-array String 5)]
(aset arr 0 "pain")
(aset arr 0 "to")
(aset arr 0 "fill")
(aset arr 0 "in")
(aset arr 0 "array")
arr))
(aget (painful-array) 0)
(alength (painful-array))
(to-array ["Easier" "array"])
(String/format "Week: %s Mileage: %d"
(to-array [2 26]))
(into-array String ["Easier" "array"])
(into-array ["Easier" "array"])
(def strs (into-array ["some" "strings"]))
(seq (amap strs idx _ (.toUpperCase (aget strs idx))))
(areduce strs idx ret 0 (max ret (.length (aget strs idx))))
(map .toUpperCase ["a" "short" "message"])
(map (memfn toUpperCase) ["a" "short" "message"])
(map #(.toUpperCase %) ["a" "short" "message"])
(instance? Integer 10)
(instance? Comparable 10)
(instance? String 10)
(format "%s ran %d miles today" "Stu" 8)
(import '(java.security MessageDigest))
(bean (MessageDigest/getInstance "SHA"))
(:digestLength (bean (MessageDigest/getInstance "SHA")))
(defn sum-to [n]
(loop [i 1
sum 0]
(if (<= i n)
(recur (inc i) (+ i sum))
sum)))
(sum-to 10)
(dotimes [_ 5] (time (sum-to 10)))
(defn integer-sum-to [n]
(let [n (int n)]
(loop [i (int 1)
sum (int 0)]
(if (<= i n)
(recur (inc i) (+ i sum))
sum))))
(dotimes [_ 5] (time (integer-sum-to 10)))
(defn unchecked-sum-to [n]
(let [n (int n)]
(loop [i (int 1)
sum (int 0)]
(if (<= i n)
(recur (inc i) (unchecked-add i sum))
sum))))
(dotimes [_ 5] (time (unchecked-sum-to 10)))
(sum-to 100000)
(integer-sum-to 100000)
(unchecked-sum-to 100000)
(defn better-sum-to [n]
(reduce + (range 1 (inc n))))
(defn best-sum-to [n]
(/ (* n (inc n)) 2))
(defn describe-class [c]
{:name (.getName c)
:final (java.lang.reflect.Modifier/isFinal (.getModifiers c))})
(set! *warn-on-reflection* true)
(defn describe-class [^Class c]
{:name (.getName c)
:final (java.lang.reflect.Modifier/isFinal (.getModifiers c))})
(describe-class StringBuffer)
(describe-class "foo")
(defn wants-a-string [^String s] (println s))
(wants-a-string "foo")
(wants-a-string 0)
(import '(org.xml.sax InputSource)
'(org.xml.sax.helpers DefaultHandler)
'(java.io StringReader)
'(javax.xml.parsers SAXParserFactory))
(def print-elt-handler
(proxy [DefaultHandler] []
(startElement [uri local qname atts]
(println (format "Saw: %s" qname)))))
(defn demo-sax-parse [src handler]
(.. SAXParserFactory newInstance newSAXParser
(parse (InputSource. (StringReader. src))
handler)))
(demo-sax-parse "<foo><bar>Body of bar</bar></foo>" print-elt-handler)
(.start (Thread. (proxy [Runnable] [] (run [] (println "I ran!")))))
(proxy [Callable] [])
(.call (proxy [Callable] []))
(#(println "foo"))
(.run #(println "foo"))
(.call #(println "foo"))
(dotimes [i 5]
(.start
(Thread.
(fn []
(Thread/sleep (rand 500))
(println (format "Finished %d on %s" i (Thread/currentThread)))))))
(spit "hello.out" "hello, world")
(try
(throw (Exception. "failed"))
(finally
(println "clean up")))
(defn class-available? [class-name]
(Class/forName class-name))
(class-available? "borg.util.Assimilate")
(defn class-available? [class-name]
(try
(Class/forName class-name) true
(catch ClassNotFoundException _ false)))
(class-available? "borg.util.Assimilate")
(class-available? "java.lang.String")