This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-11-29
Channels
- # adventofcode (1)
- # announcements (2)
- # beginners (163)
- # biff (3)
- # calva (19)
- # cider (56)
- # cljs-dev (5)
- # clojure (43)
- # clojure-belgium (2)
- # clojure-europe (47)
- # clojure-norway (32)
- # clojure-uk (2)
- # clojurescript (24)
- # datomic (5)
- # events (1)
- # fulcro (2)
- # hoplon (11)
- # hyperfiddle (12)
- # jobs (1)
- # lsp (15)
- # malli (7)
- # music (1)
- # polylith (2)
- # re-frame (7)
- # reagent (7)
- # shadow-cljs (25)
- # specter (9)
- # squint (16)
- # xtdb (5)
another question, how would you write this:
(take the key of map represented as vector into the value as the key :name
. it's a nested structure)
{:id 0
:children [:child-1 {:id 1}
:child-2 {:id 2
:children [:child-3 {:id 3}]}]}
to:
{:id 0
:children [:child-1 {:id 1 :name :child-1}
:child-2 {:id 2 :name :child-2
:children [:child-3 {:id 3 :name :child-3}]}]}
a way to collect the name to apply to the node: [(view #(apply hash-map %)) ALL (collect-one FIRST) LAST]
a way to navigate to every node:
(recursive-path [] p
(if-path #(and (map? %) (contains? % :children))
(stay-then-continue :children (view #(apply hash-map %)) MAP-VALS p) STAY))
combining the two:
(def nodes
(recursive-path [] p
(if-path #(and (map? %) (contains? % :children))
(stay-then-continue :children (view #(apply hash-map %)) ALL DISPENSE (collect-one FIRST) LAST p)
STAY)))
(transform nodes (fn ([v] v) ([c v] (assoc v :name c)))
{:id 0
:children [:child-1 {:id 1}
:child-2 {:id 2
:children [:child-3 {:id 3}]}]})
Yesterday I tried it without specter, and I'm not sure what I like better:
(defn attach-names [app]
(if (:children app)
(update app :children #(->> %
(partition 2)
(mapv (fn [[k v]] [k (assoc (attach-names v) :name k)]))
flatten
vec))
app))
I just couldn't wrap my head around how to do it with specter, and the way you wrote it doesn't seem to me straightforward as just "normal" clj code