This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-04-23
Channels
- # announcements (3)
- # babashka (68)
- # beginners (297)
- # calva (13)
- # cider (4)
- # clj-kondo (8)
- # cljs-dev (10)
- # cljsrn (26)
- # clojure (100)
- # clojure-europe (4)
- # clojure-germany (1)
- # clojure-italy (9)
- # clojure-nl (5)
- # clojure-spec (9)
- # clojure-uk (41)
- # clojurescript (69)
- # conjure (70)
- # cursive (44)
- # data-science (20)
- # datascript (2)
- # datomic (55)
- # emacs (1)
- # exercism (3)
- # graalvm (2)
- # kaocha (11)
- # leiningen (6)
- # meander (9)
- # mental-health (1)
- # off-topic (73)
- # pathom (6)
- # pedestal (1)
- # re-frame (3)
- # reagent (52)
- # reitit (8)
- # rum (39)
- # shadow-cljs (152)
- # spacemacs (10)
- # tools-deps (28)
- # xtdb (5)
How do I do this where :Price
is an optional key. I have certain JSON files where say :Price
or perhaps :Name
inside of :Price
is missing. I want to still match on the file and offer a default value
(defn data-transform
[data]
(m/search data
{:Data {:Tickets (m/scan {:Seat ?seat
:Eventname ?event-name
:Price {:Name ?price-name
:Amount ?price-amount}})}}
{:seat ?seat
:ticket-category (or ?price-name nil)
:price (or ?price-amount nil)}))
Do you actually need search semantics for some bigger part of this. Or are you okay with it being a match?
Actually I can make it work with search.
(m/search data
{:Data {:Tickets (m/scan {:Seat ?seat
:Eventname ?event-name
:Price (m/or
{:Name ?price-name
:Amount ?price-amount}
(m/let [?price-name "default-name"
?price-amount "default-price"]
nil))})}}
{:seat ?seat
:ticket-category ?price-name
:price ?price-amount})
Iām having some internet troubles atm so have to reply on phone. What would you do if :Price itself is missing?
The answer @U5K8NTHEZ gave accounts for that :Price
, (m/or {,,,} (m/let [,,,]))
I actually think the solution you gave me will be sufficient. But I am curious as to how one would handle missing data like I described
Yeah sorry I missed all the other ones. I will try to write that up as well.
I should explain the steps on how I got here, but I already wasted too much time playing with this š
(m/defsyntax with-defaults [bindings body]
(m/rewrite bindings
[!binding (m/and !lvar1 !lvar2) !default ...]
('m/with [!binding ('m/or ('m/some !lvar1)
('m/let [!lvar2 !default] nil))
...]
~body)
))
(m/search data
(with-defaults [%name ?name "name"
%amount ?amount 0]
{:Data {:Tickets (m/scan {:Seat ?seat
:Eventname ?event-name
:Price (m/or
{:Name %name
:Amount %amount}
(m/and nil
%name
%amount))})}})
{:seat ?seat
:ticket-category ?name
:price ?amount})
Doing it all inline was getting too verbose. So I made my own little defsyntax thing.