This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-10-14
Channels
- # beginners (4)
- # boot (2)
- # cider (17)
- # clara (8)
- # cljs-dev (5)
- # clojure (16)
- # clojure-italy (14)
- # clojure-nl (1)
- # clojure-norway (1)
- # clojure-russia (1)
- # clojure-uk (4)
- # clojurescript (8)
- # data-science (19)
- # datomic (4)
- # ethereum (1)
- # events (1)
- # fulcro (15)
- # graphql (4)
- # hoplon (10)
- # jobs (1)
- # mount (1)
- # off-topic (15)
- # parinfer (4)
- # perun (1)
- # portkey (6)
- # re-frame (15)
- # reagent (10)
- # schema (1)
- # shadow-cljs (80)
- # specter (4)
- # test-check (35)
Hi!
Is it possible to collect
a value of every "branch" separately instead of all of them?
I'm trying to get all possible get-in
path vectors for a given nested map (preferably excluding :root
):
(magic {:root {:a {:b 1 :c 2} :d {:e 1 :f 2}}})
=> [[:a :b] [:a :c] [:d :e] [:d :f]]`
The best I could manage so far was this:
(select [MAP-VALS (collect MAP-KEYS) MAP-VALS MAP-KEYS]
{:root {:a {:b 1 :c 2} :d {:e 1 :f 2}}})
=> [[[:a :d] :b] [[:a :d] :c] [[:a :d] :e] [[:a :d] :f]]
...which is almost there, but collect
grabs all the branches' values at every step.
I could massage that second structure into what I need, but I suspect there is a nicer way of approaching this 🙂@aisamu it's like this:
(def data {:root {:a {:b 1 :c 2} :d {:e 1 :f 2}}})
(select [:root ALL (collect-one FIRST) LAST MAP-KEYS] data)
if your data is recursive, this example is instructive for how to collect all the paths (which can be arbitrary/varying lengths): https://github.com/nathanmarz/specter/wiki/Using-Specter-Recursively#find-the-index-route-of-a-value-within-a-data-structure