Fork me on GitHub
#specter
<
2018-01-31
>
cperrone14:01:50

Hi Nathan and all, I posted the problem in the #beginner channel, but someone suggested that I ask here. (I actually played with specter a bit (love it) but this is well beyond my current abilities). At its simplest level, let’s say I have a ‘dumb’ container component, e.g.:

(defn my-component [content]
[:div content])
… and a (possibly arbitrarily nested) tree structure, e.g.
(def tree [["a" ["b" "c" "d"]] "e"])
What approach would you use to walk the tree, plug-in the component and its content to return something like this?
(def result [:div
             [:div "a"
              [:div "b"]
              [:div "c"]
              [:div "d"]]
             [:div "e"]])
(My naive implementation has the component recursively calling itself on the children, which I really don’t want.) Thanks so much in advance!

nathanmarz14:01:58

@cperrone what exactly are the rules you want to follow for the transformation?

nathanmarz14:01:39

looks like you wrap leaf values in :div, except for the case of "a"

nathanmarz14:01:45

which wraps it and the children

nathanmarz14:01:51

but "b" doesn't follow that same rule

cperrone14:01:30

Let me think. In this case, a contains b c d (which are siblings), e is at the same level of a

cperrone14:01:11

So the rule (I think) is the same. my-component should be able to “render” its content recursively. An input map with nested :children vector may work as well, if it makes it easier. The crux of the problem is that the container wraps arbitrary content (which may include child containers).

nathanmarz15:01:35

@cperrone this is pretty close to what you want:

(defn my-component [node]
  `[:div ~(:val node) ~@(:children node)])

(def tree [{:val "a" :children [{:val "b"} {:val "c"} {:val "d"}]} {:val "e"}])

(def NODES (recursive-path [] p (continue-then-stay :children ALL p)))

(transform [ALL NODES] my-component tree)

cperrone15:01:00

oh man, thank you so much. My kid just arrived, so I need to take a break, but I’ll definitely play with it later on.

nathanmarz15:01:38

the key is to have a consistent and regular structure to your data

cperrone15:01:12

yes, indeed. I love how declarative specter is by the way. It’s easy to see what’s going on. Ok, gotta go, but thank you again!