This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-11-21
Channels
- # beginners (5)
- # boot (15)
- # capetown (1)
- # chestnut (2)
- # cljs-dev (9)
- # cljsjs (3)
- # cljsrn (1)
- # clojure (190)
- # clojure-brasil (2)
- # clojure-greece (14)
- # clojure-italy (3)
- # clojure-poland (8)
- # clojure-romania (1)
- # clojure-russia (2)
- # clojure-serbia (3)
- # clojure-spec (38)
- # clojure-uk (98)
- # clojure-ukraine (2)
- # clojurescript (65)
- # clojurex (1)
- # core-async (16)
- # cursive (16)
- # datomic (3)
- # defnpodcast (7)
- # emacs (11)
- # funcool (2)
- # hoplon (16)
- # jobs (1)
- # leiningen (4)
- # lumo (9)
- # off-topic (2)
- # om (1)
- # other-languages (1)
- # protorepl (1)
- # re-frame (50)
- # reagent (16)
- # reitit (32)
- # remote-jobs (1)
- # rum (1)
- # shadow-cljs (73)
- # spacemacs (36)
- # specter (21)
- # sql (6)
- # unrepl (107)
- # untangled (4)
hi, i have a normalized map say: version -> entity_1 -> entity_2
and I want to transform it into: entity_1 -> version -> entity_2
. what is the best way to do this using specter?
for example: input:
{1 {"e1" {"p1" nil "p2" 9} "e2" {"p3" nil "p4" 6}} 2 {"e1" {"p6" nil "p7" nil}}}
output:
{"e1" {1 {"p1" nil "p2" 9} 2 {"p6" nil "p7" nil}} "e2" {1 {"p3" nil, "p4" 6}}}
@johanatan specter won't help that much with that since you're basically making a whole new data structure
specter is for when you're changing part of a data structure, or changing many pieces the same way within the overall structure
but i fail to see how in principle the "whole" isn't just an all-encompassing "part"
i.e., with any transformation-based language (say e.g. XSLT) changing the whole is just changing like changing a part where the part == the whole
in other words, it probably is possible with specter (and I would wager that the ultimate solution to it ends up being rather elegant once discovered) but the solution, like many (most?) in specter, is elusive and mind-bending to try to arrive at
I don't think there's an elegant specter solution to this
this is a total restructuring
that it's still a map -> map -> map afterwards is deceiving
it is an interesting use case
this works:
(apply deep-merge (for [[version events] in] (specter/transform [specter/MAP-VALS] #(do {version %}) events)))
where deep-merge
is defined as:
(defn deep-merge
[& vals]
(if (every? map? vals)
(apply merge-with deep-merge vals)
(last vals)))
Where specter isn't that well suited for this kind of transformation, there's this cool lib I remember seeing that should be perfect. https://github.com/disalvjn/faconne