This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-12-14
Channels
- # adventofcode (38)
- # announcements (42)
- # aws (3)
- # babashka (8)
- # beginners (165)
- # calva (36)
- # chlorine-clover (14)
- # cider (6)
- # clj-kondo (5)
- # cljsrn (33)
- # clojure (27)
- # clojure-australia (1)
- # clojure-czech (1)
- # clojure-doc (1)
- # clojure-europe (26)
- # clojure-nl (6)
- # clojure-spec (6)
- # clojure-uk (3)
- # clojurescript (10)
- # code-reviews (20)
- # conjure (1)
- # core-logic (5)
- # cursive (3)
- # data-science (5)
- # datomic (35)
- # emacs (1)
- # figwheel-main (3)
- # fulcro (10)
- # honeysql (1)
- # introduce-yourself (4)
- # jobs (3)
- # jobs-discuss (4)
- # minecraft (2)
- # missionary (28)
- # nextjournal (3)
- # off-topic (45)
- # pathom (7)
- # polylith (1)
- # portal (22)
- # practicalli (2)
- # re-frame (4)
- # reagent (19)
- # releases (3)
- # remote-jobs (3)
- # reveal (1)
- # rum (4)
- # shadow-cljs (37)
- # spacemacs (14)
- # sql (1)
- # tools-build (7)
- # tools-deps (16)
- # vim (13)
- # xtdb (15)
I can't figure out which of these is clearer
(defn play
[number boards]
(mapv (fn [board]
(mapv (fn [row]
(mapv (fn [{:keys [value] :as p}]
(if (= value number)
(assoc p :marked true)
p))
row))
board))
boards))
(defn play'
[number boards]
(letfn [(process-cell [{:keys [value] :as p}]
(if (= value number)
(assoc p :marked true)
p))
(process-row [row]
(mapv process-cell row))
(process-board [board]
(mapv process-row board))]
(mapv process-board
boards)))
I vote play'
, but play
isn't bad either. I do like using specter for this kind of stuff, but I know there's lots of people who prefer not to use specter for various reasons.
I could even use post-walk, but the data structure is so uniform that it seems silly
it seems like you're processing a 2d array. another option is to use a map of [x,y]
to cell
and then you can just use update-vals
yeah, maybe 2d isn't actually ideal here
A recursive structure might warrant a recursive function
(defn mapv-dim [dim f coll]
(if (= 1 dim)
(mapv f coll)
(mapv #(map-dim (dec dim) f %) coll)))
(defn play [number boards]
(mapv-dim 3
#(cond-> % (= number (:value %)) (assoc :marked true))
boards))
Now you just bump up the dimension when asked to check all boards on all tournaments on all planets in all solar systems
The map has a few advantages:
• it's trivial to filter for only occupied cells
• an empty board is just {}
• expanding, contracting the board isn't too bad
but it's not ideal for all uses cases. I'm not doing advent of calendar, so no idea if it makes sense there.
it's a set of bingo cards
anyway, I would have switched to hash-map indexed by [x,y] pairs if there was even one bug in my play function, but it was good as written the first time it got used lol
maybe I'll switch to a hash-map for part two
ehh, nope the only change is when to stop iterating, all other rules are unchanged (that is all marking / winning logic stays the same, only difference is we keep going until all boards win which is well outside this scope)
the kind of thing you can only get away with when coding for fun
(let [next-boards ((rand-nth [play play']) boards n)]
yet another alternative is to generate points [x y]
on the board and map over those instead
potetem has the right of it. The function is over the index, not the collection. Or something.
alternatively, flatten the board into a single collection and keep the dimensions as data.
which is better will have to do with if you ever care about "rows" or not as a single collection.