This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-05-10
Channels
- # aws (39)
- # babashka (4)
- # beginners (5)
- # biff (25)
- # cider (14)
- # clj-on-windows (40)
- # clojure-europe (36)
- # clojure-gamedev (1)
- # clojure-losangeles (4)
- # clojure-norway (51)
- # clojure-spec (5)
- # clojure-uk (2)
- # clojurescript (2)
- # clr (176)
- # data-science (10)
- # datalevin (17)
- # datomic (7)
- # deps-new (4)
- # docs (3)
- # emacs (12)
- # figwheel (3)
- # figwheel-main (5)
- # hyperfiddle (20)
- # instaparse (3)
- # introduce-yourself (8)
- # lsp (66)
- # malli (43)
- # off-topic (4)
- # rdf (11)
- # reagent (5)
- # releases (2)
- # sci (11)
- # shadow-cljs (24)
- # slack-help (2)
- # specter (7)
- # tools-deps (3)
- # xtdb (48)
it's been hard for me to come up with problems that i want to solve with specter, but i have one now. i have parts of my data-structure where i have meta data on a list/vector, i want to merge/transform each item in the list with that meta data. i typically do this sort of thing with walks and core.match, but i would like to see how this looks like in specter.
;; input
{
:example-meta
^{:custom/template "example/file/path.file"}
[
{:summary "all fields filled in"}
{:summary "missing single-file uploads"}
]
}
;; output =>
{
:example-meta
[
{:summary "all fields filled in"
:custom/template "example/file/path.file"}
{:summary "missing single-file uploads"}
:custom/template "example/file/path.file"
]
}
so, i think i know how to do this with a nested transformation, but i'm wondering if this can be done with a single transformation. can a single transformation detect the key i care about in meta, then operate on the structure the meta-data is attached to?(transform [MAP-VALS
(view (juxt (comp #(select-keys % [:custom/template]) meta) identity))
(pred first)]
(fn [[meta resources]]
(->> resources
(transform ALL (partial merge-with str meta)))
))
i came up with this, i tried experimenting with a lot of specter functions, including multi-path
i found view
to be very useful, and multi-path
does almost what i want (which is something like juxt
). i expect there is a much better way to do this. (comp select-keys...
seems like it's redundant with other specter functions(merge-with str ...
is intentional, meta data in this case would be assumed to be a prefix
(transform [:example-meta
(collect-one META #(select-keys % [:custom/template]))
ALL]
(fn [meta resources] (merge-with str meta resources))
data)