This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-10-29
Channels
- # aws (2)
- # bangalore-clj (2)
- # beginners (36)
- # boot (10)
- # cider (9)
- # cljs-dev (19)
- # clojure (47)
- # clojure-russia (4)
- # clojure-spec (18)
- # clojure-uk (4)
- # clojurescript (71)
- # core-async (20)
- # core-logic (2)
- # css (3)
- # cursive (5)
- # data-science (15)
- # datomic (7)
- # emacs (13)
- # figwheel (4)
- # klipse (1)
- # luminus (5)
- # lumo (1)
- # off-topic (33)
- # re-frame (17)
- # shadow-cljs (1)
- # spacemacs (5)
- # specter (21)
- # unrepl (1)
- # vim (7)
Hi i just discovered specter, it’s awesome. I have a quick question. I’m trying to add a map to a list of maps., but setval treats it as a collection and adding each k-v pair. Like so:
testmap
=> {:a "a", :b [{:id 1, :str "1"} {:id 2, :str "2"} {:id 3, :str "2"}]}
(s/setval [:b s/END] {:id 5
:a 6} testmap)
=> {:a "a", :b [{:id 1, :str "1"} {:id 2, :str "2"} {:id 3, :str "2"} [:id 5] [:a 6]]}
How do I get around this?Not sure if this is the idiomatic way but does this do what you're looking for?
(def test-map {:a "a", :b [{:id 1, :str "1"} {:id 2, :str "2"} {:id 3, :str "2"}]})
(transform [:b END] #(conj % {:id 5 :a 6}) test-map)
That returns
{:a "a", :b [{:id 1, :str "1"} {:id 2, :str "2"} {:id 3, :str "2"} {:id 5, :a 6}]}
for meI also don't think I needed end there
(transform [:b] #(conj % {:id 5 :a 6}) test-map)
that should do the same
since a vector gets added to the end anyway
hey i just tried this too
(s/setval [:b s/END] [{:id 5
:a 6}] testmap)
seems to workAh yeah! 🙂
Nice! 🙂
@eoliphant use AFTER-ELEM
to append a single element to a sequence
(setval [:b AFTER-ELEM] {:id 5 :a 6} testmap)
that's more efficient than using END
for this use case
this Specter snippet works, but I think it’s a bit of a code smell to have both cond-path
and a cond
in my transformation function. This being Specter, I suspect that there is a better way to do it 🙂
(def DATA-WALKER
(s/recursive-path [] p
(s/cond-path
map? (s/continue-then-stay s/MAP-VALS p)
seq? (s/continue-then-stay s/ALL p))))
(defn simplify [m]
(s/transform
DATA-WALKER
#(cond
(sequential? %) (vec %)
(map? %) (into {} %))
m))
Any suggestions?I’ve looked at multi-transform
, but I haven’t figured out how to use it with recursive paths
@schmee multi-transform
version would be (multi-transform [DATA-WALKER (if-path map? (terminal #(into {} %)) (terminal vec))] m)
you don't need the cond
since after DATA-WALKER
it's either map?
or seq?