|
28 | 28 | [:script "Blockly.defineBlocksWithJsonArray(twotiles.blocks);"]]) |
29 | 29 |
|
30 | 30 | ^:kindly/hide-code |
31 | | -(defn create-ws [name height] |
| 31 | +(defn ws-hiccup [name height] |
32 | 32 | [:div |
33 | 33 | [:div {:id name, :style {:height height}}] |
34 | 34 | [:script (str "var " name " = Blockly.inject('" name "'," |
35 | 35 | "{'sounds': false, 'scrollbars':false, 'trashcan':false})")]]) |
36 | 36 |
|
37 | | -(md "## 4Clojure") |
| 37 | +^:kindly/hide-code |
| 38 | +(def create-ws (comp kindly/hide-code ws-hiccup)) |
| 39 | + |
| 40 | +(md "## 4Clojure Problem 110, Pronunciation") |
| 41 | + |
| 42 | +(md "Produce a \"pronunciation\" of a sequence of numbers. For example, [1 1] is pronounced as [2 1] (\"two ones\"), which in turn is pronounced as [1 2 1 1] (\"one two, one one\"). The solution below is taken from the [4Clojure website](https://4clojure.oxal.org)." ) |
38 | 43 |
|
39 | | -(defn pronounce [s] |
| 44 | +(defn pronounce |
| 45 | + [numbers] |
40 | 46 | (mapcat (juxt count first) |
41 | | - (partition-by identity s))) |
| 47 | + (partition-by identity |
| 48 | + numbers))) |
42 | 49 |
|
43 | 50 | (doall [(pronounce [1]) |
44 | 51 | (pronounce [1 1]) |
|
53 | 60 | ws |
54 | 61 | (str code)))))) |
55 | 62 |
|
| 63 | +(md "To memorize this solution, complete the following set of graphical puzzles. The most fun is on mobile devices. Maybe you want to scroll through the workspaces first, to get the idea.") |
| 64 | + |
56 | 65 | ^:kindly/hide-code |
57 | 66 | (def add-pr1 (add-ws 'js/pr1)) |
58 | | -(add-pr1 's) |
| 67 | +(add-pr1 'numbers) |
59 | 68 | (add-pr1 'identity) |
60 | | -(add-pr1 '(partition-by :tiles/slot |
61 | | - :tiles/slot)) |
| 69 | +(add-pr1 '(:tiles/vert |
| 70 | + (partition-by :tiles/slot |
| 71 | + :tiles/slot))) |
62 | 72 | (kind/hiccup (create-ws "pr1" "300px")) |
63 | 73 |
|
| 74 | + |
| 75 | +(md "Step two") |
64 | 76 | ^:kindly/hide-code |
65 | 77 | (def add-pr2 (add-ws 'js/pr2)) |
66 | | -(add-pr2 '(partition-by identity s)) |
| 78 | +(add-pr2 '(:tiles/vert |
| 79 | + (partition-by identity |
| 80 | + numbers))) |
67 | 81 | (add-pr2 'first) |
68 | 82 | (add-pr2 'count) |
69 | 83 | (add-pr2 '(juxt :tiles/slot :tiles/slot)) |
70 | 84 | (kind/hiccup (create-ws "pr2" "300px")) |
71 | 85 |
|
| 86 | +(md "Step three") |
72 | 87 | ^:kindly/hide-code |
73 | 88 | (def add-pr3 (add-ws 'js/pr3)) |
74 | | -(add-pr3 '(partition-by identity s)) |
| 89 | +(add-pr3 '(:tiles/vert |
| 90 | + (partition-by identity numbers))) |
75 | 91 | (add-pr3 '(juxt count first)) |
76 | 92 | (add-pr3 '(:tiles/vert |
77 | 93 | (mapcat :tiles/slot |
78 | 94 | :tiles/slot))) |
79 | 95 | (kind/hiccup (create-ws "pr3" "300px")) |
80 | 96 |
|
| 97 | + |
| 98 | +(md "Step four") |
81 | 99 | ^:kindly/hide-code |
82 | 100 | (def add-pr4 (add-ws 'js/pr4)) |
83 | 101 | (add-pr4 |
84 | 102 | '(:tiles/vert |
85 | 103 | (mapcat (juxt count first) |
86 | | - (partition-by identity s)))) |
87 | | -(add-pr4 '[s]) |
| 104 | + (:tiles/vert |
| 105 | + (partition-by identity |
| 106 | + numbers))))) |
| 107 | +(add-pr4 '[numbers]) |
88 | 108 | (add-pr4 'pronounce) |
89 | 109 | (add-pr4 '(defn :tiles/slot |
90 | 110 | :tiles/slot |
91 | 111 | :tiles/slot)) |
92 | 112 | (kind/hiccup (create-ws "pr4" "300px")) |
93 | 113 |
|
| 114 | +(md "The final result") |
94 | 115 | ((add-ws 'js/pr5) |
95 | | - '(defn pronounce [s] |
| 116 | + '(defn pronounce [numbers] |
96 | 117 | (:tiles/vert |
97 | 118 | (mapcat (juxt count first) |
98 | | - (partition-by identity |
99 | | - s))))) |
| 119 | + (:tiles/vert |
| 120 | + (partition-by identity |
| 121 | + numbers)))))) |
100 | 122 |
|
101 | 123 | (kind/hiccup (create-ws "pr5" "300px")) |
102 | 124 |
|
| 125 | + |
| 126 | +(md "## 4Clojure #85: Powerset") |
| 127 | + |
| 128 | +(md "Write a function which generates the power set of a given set. The power set of a set x is the set of all subsets of x, including the empty set and x itself.") |
| 129 | + |
| 130 | +(defn powerset |
| 131 | + [original-set] |
| 132 | + (reduce (fn [result e] |
| 133 | + (into result |
| 134 | + (map (fn [x] |
| 135 | + (conj x e)) |
| 136 | + result))) |
| 137 | + (hash-set #{ }) |
| 138 | + original-set)) |
| 139 | + |
| 140 | +(powerset (hash-set 1 2)) |
| 141 | + |
| 142 | +^:kindly/hide-code |
103 | 143 | (kind/scittle |
104 | | - '(defn add-blocks [ws code] |
105 | | - (js/addBlocks ws (str code)))) |
| 144 | + '(set! (.-add_ps js/window) |
| 145 | + (fn [code] |
| 146 | + (js/addBlocks js/ps code)))) |
106 | 147 |
|
107 | | -(kind/scittle '(add-blocks js/prend '(8 9 10))) |
| 148 | +^:kindly/hide-code |
| 149 | +(defn btn-hiccup [tupels] |
| 150 | + (into [:div] |
| 151 | + (map-indexed (fn [i cs] |
| 152 | + [:button |
| 153 | + {:onClick |
| 154 | + (reduce (fn [r c] |
| 155 | + (str r "add_ps('" c "');")) |
| 156 | + "" |
| 157 | + cs)} |
| 158 | + (str "Step number " (inc i))]) |
| 159 | + tupels))) |
| 160 | + |
| 161 | +(md "Again, complete a set of according puzzles step by step. This time, there is only one singe workspace. Start by clicking the first button.") |
108 | 162 |
|
109 | | -(kind/hiccup (create-ws "prend" "300px")) |
| 163 | +^:kindly/hide-code |
| 164 | +(kind/hiccup |
| 165 | + (btn-hiccup |
| 166 | + [['(:tiles/vert (map :tiles/slot :tiles/slot)) |
| 167 | + '(:tiles/vert (fn [x] :tiles/slot)) |
| 168 | + 'e |
| 169 | + 'x |
| 170 | + '(conj :tiles/slot :tiles/slot) |
| 171 | + 'result |
| 172 | + ] |
| 173 | + ['(:tiles/vert (fn [result e] :tiles/slot)) |
| 174 | + '(:tiles/vert (into :tiles/slot :tiles/slot)) |
| 175 | + 'result] |
| 176 | + ['(:tiles/vert (reduce :tiles/slot :tiles/slot :tiles/slot)) |
| 177 | + '(hash-set #{}) |
| 178 | + 'original-set]])) |
| 179 | + |
| 180 | + |
| 181 | +(kind/hiccup (create-ws "ps" "500px")) |
0 commit comments