This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-06-02
Channels
- # aleph (5)
- # beginners (112)
- # boot (137)
- # cider (10)
- # cljs-dev (36)
- # cljsrn (2)
- # clojure (118)
- # clojure-argentina (1)
- # clojure-berlin (1)
- # clojure-brasil (3)
- # clojure-dev (4)
- # clojure-italy (2)
- # clojure-nl (13)
- # clojure-russia (23)
- # clojure-spec (5)
- # clojure-uk (53)
- # clojurescript (344)
- # clojutre (1)
- # core-async (65)
- # cursive (9)
- # datascript (7)
- # datomic (28)
- # devops (1)
- # emacs (16)
- # events (1)
- # jobs (5)
- # keechma (18)
- # lumo (56)
- # off-topic (7)
- # om (3)
- # onyx (14)
- # protorepl (21)
- # re-frame (3)
- # reagent (20)
- # ring (12)
- # ring-swagger (9)
- # specter (17)
- # unrepl (14)
- # vim (14)
- # yada (22)
hello, I would like to use specter to apply a function to all map keys (recursively) on a map, can someone please give me an example on doing that?
@wilkerlucio can you show a specific example of a transformation?
that should be pretty easy
@nathanmarz sure, something like this:
{:person/name "Bla"
:person/child {:child/something "other"
:child/bla {:subchild/entry "blabla"}}}
to this:
{:name "Bla"
:child {:something "other"
:bla {:entry "blabla"}}}
considering it can have N nesting fields
@wilkerlucio looks like this:
(def data
{:person/name "Bla"
:person/child {:child/something "other"
:child/bla {:subchild/entry "blabla"}}})
(def MapWalker
(recursive-path [] p
(if-path map?
(continue-then-stay MAP-VALS p))
))
(setval [MapWalker MAP-KEYS NAMESPACE] nil data)
@nathanmarz thanks 🙂
(def ALL-MAPS
"All maps of an entity"
(recursive-path [] p
[(walker map?) (stay-then-continue MAP-VALS p)]))
@wilkerlucio those dont work with {:a [{:b :c}]}
...it's generally better not to use walker
but rather make a path that precisely encodes the structure of the data you're working with
walker is brute force which makes it less performant and often causes surprising bugs (e.g. by descending into things you didn't want, like records)
@souenzzo https://github.com/nathanmarz/specter/blob/master/src/clj/com/rpl/specter.cljc#L1284
that's the new definition of walker
I recently committed
can define your ALL-MAPS
similarly by handling the different data structure cases in a cond-path
(def ALL-MAPS-2
(recursive-path
[] p
(cond-path map? (continue-then-stay MAP-VALS p)
coll? [ALL p])))
I will try to swap in my code.@souenzzo nice catch