This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-03-18
Channels
- # announcements (31)
- # asami (11)
- # aws (33)
- # babashka (30)
- # beginners (69)
- # calva (1)
- # chlorine-clover (10)
- # cider (3)
- # clj-kondo (24)
- # cljdoc (19)
- # cljs-dev (1)
- # cljsrn (2)
- # clojure (104)
- # clojure-australia (4)
- # clojure-dev (29)
- # clojure-europe (202)
- # clojure-germany (36)
- # clojure-nl (6)
- # clojure-poland (3)
- # clojure-serbia (6)
- # clojure-spec (18)
- # clojure-uk (32)
- # clojurescript (36)
- # conjure (1)
- # core-async (6)
- # datomic (15)
- # duct (1)
- # emacs (11)
- # fulcro (24)
- # graalvm (12)
- # jobs (3)
- # juxt (1)
- # kaocha (2)
- # keechma (4)
- # lsp (1)
- # malli (102)
- # meander (17)
- # off-topic (16)
- # pathom (8)
- # re-frame (12)
- # remote-jobs (7)
- # rewrite-clj (72)
- # shadow-cljs (27)
- # sql (26)
- # tools-deps (8)
- # vim (3)
- # xtdb (28)
- # yada (5)
Is there a native way to avoid including a key in a map when the value would match nil ?
Not exactly sure what you mean. Here’s how you can make sure that a value is non-nil.
(m/match {:a 2}
{:a (m/some ?a)}
?a)
;; => 2
(m/match {:b nil}
{:a (m/some ?a)
:b nil}
?a)
;; => non exhaustive pattern match
Did you want to do that or did you want to check for a key that is explicitly nil?
If so, I think the best I can think of is this. Maybe there is a better way?
(m/match {:b nil}
(m/and
(m/pred #(contains? % :b))
{})
:yep)
(If neither of these are right feel free to give some examples)
Thanks for the answer (m/match{:a nil :b 2} {:a ?a :b ?b} {"a" ?a "b" ?b}) ;; => {"a" nil, "b" 2}
I found this answer in the chat log: (m/search {:foo/a "..." :foo/b "..." :foo/optional nil } {:foo/a (m/pred string? ?value)} [:a ?value] {:foo/b (m/pred string? ?value)} [:b ?value] {:foo/optional (m/pred string? ?value)} [:optional ?value])
If so you can use
(m/rewrite {,,,}
(m/submap-of !key (m/pred string? !value))
(m/map-of !key !value))
For my use case the killer feature is realy to keep the shape of the data because I need to rename the keys . What I am after is a way to not include the key value in the output if the value is nil.
(m/match
{:a nil
:b 2}
{:a ?a
:b ?b}
{"a" ?a
"b" ?b})
=> {"b" 2}
(fn f [m]
(m/find m
{(m/keyword ?name) (m/some ?x) & ?rest}
(merge {?name ?x} (f ?rest))
{_ _ & ?rest}
(f ?rest)
_
{}))
Using rewrite
:
(m/rewrite m
{(m/keyword ?name) (m/some ?x) & (m/cata ?rest)}
{?name ?x & ?rest}
{_ _ & (m/cata ?rest)}
?rest
_ {})
I will just say you can also just do a post process step. (On my phone) Something like
(into {} (filter second) my-data-after-meanderized))