This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-07-28
Channels
- # announcements (12)
- # babashka (87)
- # beginners (84)
- # calva (22)
- # circleci (4)
- # clj-kondo (46)
- # cljdoc (6)
- # cljsrn (15)
- # clojure (87)
- # clojure-europe (18)
- # clojure-uk (7)
- # clojurescript (20)
- # community-development (3)
- # conjure (1)
- # cursive (13)
- # datomic (14)
- # events (7)
- # fulcro (27)
- # graphql (31)
- # helix (8)
- # jobs-discuss (1)
- # lsp (43)
- # malli (11)
- # meander (64)
- # off-topic (7)
- # pathom (26)
- # polylith (9)
- # practicalli (2)
- # re-frame (33)
- # reagent (2)
- # reitit (5)
- # releases (2)
- # rewrite-clj (2)
- # shadow-cljs (69)
- # specter (5)
- # sql (1)
- # tools-deps (85)
- # tree-sitter (1)
- # vim (3)
Hi there, say I have maps that can look like this
{:foos [{:xx 1} {:yy 2] :bar "BAR"}
{:foos [#uuid "xx" #uuid "yy"] :bar "BAR"}
{:foos [{} {}] :bar "BAR"}
How can I trim the :foos
if "empty" in specter?
tried
[(sp/must :foos) (sp/pred #(every? empty? %)) (sp/terminal sp/NONE)]
but it throws in case of #uuid
@richiardiandrea you should just check for map?
first in that pred. Example:
(let [xs [{:foos [{:xx 1} {:yy 2}] :bar "BAR"}
{:foos [#uuid "123e4567-e89b-12d3-a456-426652340000" #uuid "123e4567-e89b-12d3-a456-426652340010"] :bar "BAR"}
{:foos [{} {}] :bar "BAR"}]]
(sp/setval
[sp/ALL (sp/must :foos) (sp/pred #(every? (fn [x] (and (map? x) (empty? x))) %))]
sp/NONE
xs))
=> [{:foos [{:xx 1} {:yy 2}], :bar "BAR"}
{:foos [#uuid"123e4567-e89b-12d3-a456-426652340000" #uuid"123e4567-e89b-12d3-a456-426652340010"], :bar "BAR"}
{:bar "BAR"}]
oh ok that makes sense - I was thinking there would be more specter magic I was missing instead
This may be better, but depends what you are trying to do (adapted from a similar thing I needed):
(def rec-node-path
(sp/recursive-path
[] p
(sp/cond-path
map? (sp/continue-then-stay sp/MAP-VALS p)
vector? (sp/continue-then-stay sp/ALL p))))
(let [xs [{:foos [{:xx 1} {:yy 2}] :bar "BAR"}
{:foos [#uuid "123e4567-e89b-12d3-a456-426652340000" #uuid "123e4567-e89b-12d3-a456-426652340010"] :bar "BAR"}
{:foos [{} {}] :bar "BAR"}]]
(sp/setval [sp/ALL :foos rec-node-path empty?] sp/NONE xs))