Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 6-Clojure/collection-type.clj @ 84:920b50be0fe5
Add notes (and failed code) for blocking queue
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 09 Jun 2019 12:07:10 +0100 |
parents | 29025305bbb7 |
children |
rev | line source |
---|---|
79
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 (def classmap { |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 "class clojure.lang.PersistentArrayMap" :map, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 "class clojure.lang.PersistentTreeMap" :map, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 "class clojure.lang.PersistentList$EmptyList" :list, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 "class clojure.lang.PersistentList" :list, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 "class clojure.lang.PersistentVector" :vector, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 "class clojure.lang.PersistentTreeSet" :set, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 "class clojure.lang.PersistentHashSet" :set |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 }) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 (defn collection-type [col] |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 "Writes :list, :map, set or :vector based on the collection's type" |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 ; There's probably a better way that accounts for inheritance, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 ; but this works and uses the "map is a function with a lookup" approach!! |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 ; |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 ; We can't do this with classes directly, because of "java.lang.IllegalArgumentException: Duplicate key: (class ())" |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 ; because of a clash with (class []), even though they're different and (hash …) gives different answers! |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 (classmap (str (class col)))) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 (println (collection-type ())) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 (println (collection-type (list 1 2 3))) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 (println (collection-type [])) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 (println (collection-type [1 2 3])) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 (println (collection-type #{})) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 (println (collection-type (sorted-set))) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 (println (collection-type (sorted-map))) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 (println (collection-type {})) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 (println (collection-type "not a collection")) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 (println) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 (def classmap2 { |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 (class {}) :map, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 (class (sorted-map)) :map, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 (class ()) :list, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
35 (class (list 1)) :list, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
36 (class #{}) :set, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
37 (class (sorted-set)) :set |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
38 }) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
39 (defn collection-type2 [col] |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
40 "Writes :list, :map, set or :vector based on the collection's type" |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
41 ; There's probably a better way that accounts for inheritance, |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 ; but this works and uses the "map is a function with a lookup" approach!! |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 ; |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
44 ; The first IF works around the duplicate key issue (at least with this implementation of maps) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 (if (= (class []) (class col)) :vector (classmap2 (class col)))) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 (println (collection-type2 ())) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
48 (println (collection-type2 (list 1 2 3))) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 (println (collection-type2 [])) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 (println (collection-type2 [1 2 3])) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 (println (collection-type2 #{})) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 (println (collection-type2 (sorted-set))) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 (println (collection-type2 (sorted-map))) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 (println (collection-type2 {})) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 (println (collection-type2 "not a collection")) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 (println) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 ; Use (bases <class>) to find the parent classes |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
59 (defn collection-type3 [col] |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
60 "Writes :list, :map, set or :vector based on the collection's type" |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
61 (let [col_class (class col)] |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
62 (if (isa? col_class clojure.lang.IPersistentList) :list |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
63 (if (isa? col_class clojure.lang.APersistentVector) :vector |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
64 (if (isa? col_class clojure.lang.APersistentMap) :map |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
65 (if (isa? col_class clojure.lang.APersistentSet) :set) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
66 ) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
67 ) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
68 ) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
69 ) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
70 ) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
71 |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
72 (println (collection-type3 ())) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
73 (println (collection-type3 (list 1 2 3))) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
74 (println (collection-type3 [])) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
75 (println (collection-type3 [1 2 3])) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
76 (println (collection-type3 #{})) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
77 (println (collection-type3 (sorted-set))) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
78 (println (collection-type3 (sorted-map))) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
79 (println (collection-type3 {})) |
29025305bbb7
Add notes and exercises from Day 1 of Clojure
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
80 (println (collection-type3 "not a collection")) |