This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-01-27
Channels
- # announcements (1)
- # aws (10)
- # babashka (53)
- # calva (133)
- # clj-kondo (46)
- # cljdoc (6)
- # cljs-dev (33)
- # clojure (105)
- # clojure-boston (1)
- # clojure-europe (11)
- # clojure-nl (4)
- # clojure-poland (1)
- # clojure-switzerland (6)
- # clojure-uk (13)
- # clojurescript (106)
- # cursive (1)
- # datascript (2)
- # emacs (13)
- # events (1)
- # figwheel-main (4)
- # fulcro (17)
- # graphql (8)
- # heroku (2)
- # honeysql (8)
- # lsp (76)
- # luminus (30)
- # malli (12)
- # meander (23)
- # music (1)
- # nextjournal (83)
- # off-topic (6)
- # pathom (3)
- # polylith (19)
- # re-frame (8)
- # reagent (2)
- # reveal (3)
- # shadow-cljs (54)
- # sql (9)
- # testing (11)
- # tools-deps (15)
- # xtdb (14)
I have some nested data structure looking approximately like:
[{:x [{:y [{:z [{:w [!k ...]} ...]} ...]} ...]} ...]
Where !k
is a string
I have another which is a map of {(keyword !k) {:data ?data}}
Where I want to produce
[{:x [{:y [{:z [{:w [!k ...] :data ?data} ...]} ...]} ...]} ...]
@ben.sless I am weak in understanding
in words:
sequence of maps, in key x there is a sequence of maps ,,, down to key w
In w there is a sequence of strings
I want to take that sequence, use it to lookup in another map where each key maps to a sequence, and assoc this sequence in the same map as w
(m/rewrite [{:a [{:b [{:c ["d" "e" "f"]}]}]}
{:d 1 :e 1 :f 1}]
[{:a [{:b [{:c [(m/app keyword !ks) ...]}]}] :as ?m}
(m/map-of !ks !vs)]
{& [?m [!ks !vs] ...]})
@ben.sless something like that?
More like
(let [lookup {:a [1 2 3]
:b [4 5 6]
:c [7 8 9]
:H '[x y z]}]
(m/rewrite
{:x
[{:y
[{:z
[{:w ["a" "b"]}
{:w ["c" "d"]}]}
{:z
[{:w ["e" "f"]}
{:w ["g" "h"]}]}]}
{:y
[{:z
[{:w ["A" "B"]}
{:w ["C" "D"]}]}
{:z
[{:w ["E" "F"]}
{:w ["G" "H"]}]}]}]}
{:x [!x ...]}
{:x [(m/cata !x) ...]}
{:y [!x ...]}
{:y [(m/cata !x) ...]}
{:z [!x ...]}
{:z [(m/cata !x) ...]}
{:w [(m/and !x (m/app keyword !k)) ...]}
{:w [!x ...] :k [!k ...] :lookup ~lookup}))
You can use [!x ..!n]
to capture the number of times something repeats. Helps keep those nested repeats in place.
I'll try again and try to see where I bumped against that specific wall, because I started with it
(m/rewrite
{:in [{:x
[{:y
[{:z
[{:w ["a" "b"]}
{:w ["c" "d"]}]}
{:z
[{:w ["e" "f"]}
{:w ["g" "h"]}]}]}
{:y
[{:z
[{:w ["A" "B"]}
{:w ["C" "D"]}]}
{:z
[{:w ["E" "F"]}
{:w ["G" "H"]}]}]}]}]
:look
{:a [1 2 3]
:b [4 5 6]
:c [7 8 9]
:H '[x y z]}}
{:in [{:x [{:y [{:z [{:w [(m/and !s (m/app keyword !k))
..!nk]}
..!nw]}
..!nz]}
..!ny]}
..!nx]
:look {!k [!d ...]}}
[{:x [{:y [{:z [{:w [!s ..!nk]
:d [!d ...]}
..!nw]}
..!nz]}
..!ny]}
..!nx]
)
accumulating the looked-up values doesn't work right, I'm probably missing something
(on my phone). Memory variables don't join like that. You'd want to assign :look to something and do something like (m/app get lookup)
Can't think of the right way right now. But will try to play with it later if I have time at the computer and no one else already solved it.
Very close now
(let [lookup {:a [1 2 3]
:b [4 5 6]
:c [7 8 9]
:H '[x y z]}
f (fn [x] (get lookup (keyword x)))]
(m/rewrite
{:x
[{:y
[{:z
[{:w ["a" "b"]}
{:w ["c" "d"]}]}
{:z
[{:w ["e" "f"]}
{:w ["g" "h"]}]}]}
{:y
[{:z
[{:w ["A" "B"]}
{:w ["C" "D"]}]}
{:z
[{:w ["E" "F"]}
{:w ["G" "H"]}]}]}]}
{:x [!x ...]}
{:x [(m/cata !x) ...]}
{:y [!x ...]}
{:y [(m/cata !x) ...]}
{:z [!x ...]}
{:z [(m/cata !x) ...]}
{:w [(m/and !x (m/app f !d)) ...]}
{:w [!x ...] :d [!d ...]}))
Aha!
(let [lookup {:a [1 2 3]
:b [4 5 6]
:c [7 8 9]
:H '[x y z]}
f (fn [x] (get lookup (keyword x) []))]
(m/rewrite
{:x
[{:y
[{:z
[{:w ["a" "b"]}
{:w ["c" "d"]}]}
{:z
[{:w ["e" "f"]}
{:w ["g" "h"]}]}]}
{:y
[{:z
[{:w ["A" "B"]}
{:w ["C" "D"]}]}
{:z
[{:w ["E" "F"]}
{:w ["G" "H"]}]}]}]}
{:x [!x ...]}
{:x [(m/cata !x) ...]}
{:y [!x ...]}
{:y [(m/cata !x) ...]}
{:z [!x ...]}
{:z [(m/cata !x) ...]}
{:w [(m/and !x (m/app f [!d ...])) ...]}
{:w [!x ...] :d [!d ...]}))
not a fan of this solution though, so if you come up with something better, please share