Skip to content

Commit 7a43720

Browse files
committed
superset
1 parent 32011dd commit 7a43720

1 file changed

Lines changed: 90 additions & 18 deletions

File tree

src/games/twotiles_4clojure.clj

Lines changed: 90 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,24 @@
2828
[:script "Blockly.defineBlocksWithJsonArray(twotiles.blocks);"]])
2929

3030
^:kindly/hide-code
31-
(defn create-ws [name height]
31+
(defn ws-hiccup [name height]
3232
[:div
3333
[:div {:id name, :style {:height height}}]
3434
[:script (str "var " name " = Blockly.inject('" name "',"
3535
"{'sounds': false, 'scrollbars':false, 'trashcan':false})")]])
3636

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)." )
3843

39-
(defn pronounce [s]
44+
(defn pronounce
45+
[numbers]
4046
(mapcat (juxt count first)
41-
(partition-by identity s)))
47+
(partition-by identity
48+
numbers)))
4249

4350
(doall [(pronounce [1])
4451
(pronounce [1 1])
@@ -53,57 +60,122 @@
5360
ws
5461
(str code))))))
5562

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+
5665
^:kindly/hide-code
5766
(def add-pr1 (add-ws 'js/pr1))
58-
(add-pr1 's)
67+
(add-pr1 'numbers)
5968
(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)))
6272
(kind/hiccup (create-ws "pr1" "300px"))
6373

74+
75+
(md "Step two")
6476
^:kindly/hide-code
6577
(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)))
6781
(add-pr2 'first)
6882
(add-pr2 'count)
6983
(add-pr2 '(juxt :tiles/slot :tiles/slot))
7084
(kind/hiccup (create-ws "pr2" "300px"))
7185

86+
(md "Step three")
7287
^:kindly/hide-code
7388
(def add-pr3 (add-ws 'js/pr3))
74-
(add-pr3 '(partition-by identity s))
89+
(add-pr3 '(:tiles/vert
90+
(partition-by identity numbers)))
7591
(add-pr3 '(juxt count first))
7692
(add-pr3 '(:tiles/vert
7793
(mapcat :tiles/slot
7894
:tiles/slot)))
7995
(kind/hiccup (create-ws "pr3" "300px"))
8096

97+
98+
(md "Step four")
8199
^:kindly/hide-code
82100
(def add-pr4 (add-ws 'js/pr4))
83101
(add-pr4
84102
'(:tiles/vert
85103
(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])
88108
(add-pr4 'pronounce)
89109
(add-pr4 '(defn :tiles/slot
90110
:tiles/slot
91111
:tiles/slot))
92112
(kind/hiccup (create-ws "pr4" "300px"))
93113

114+
(md "The final result")
94115
((add-ws 'js/pr5)
95-
'(defn pronounce [s]
116+
'(defn pronounce [numbers]
96117
(:tiles/vert
97118
(mapcat (juxt count first)
98-
(partition-by identity
99-
s)))))
119+
(:tiles/vert
120+
(partition-by identity
121+
numbers))))))
100122

101123
(kind/hiccup (create-ws "pr5" "300px"))
102124

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
103143
(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))))
106147

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.")
108162

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

Comments
 (0)