This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-03-24
Channels
- # announcements (13)
- # asami (9)
- # aws (3)
- # babashka (13)
- # babashka-sci-dev (7)
- # beginners (32)
- # calva (59)
- # cider (9)
- # clj-kondo (5)
- # cljsrn (14)
- # clojure (98)
- # clojure-brasil (5)
- # clojure-dev (1)
- # clojure-europe (30)
- # clojure-france (12)
- # clojure-nl (1)
- # clojure-norway (7)
- # clojure-uk (7)
- # clojurescript (20)
- # conjure (2)
- # datahike (7)
- # datalog (38)
- # datomic (29)
- # events (1)
- # fulcro (72)
- # graalvm (1)
- # gratitude (3)
- # helix (7)
- # honeysql (3)
- # improve-getting-started (2)
- # introduce-yourself (1)
- # leiningen (13)
- # lsp (17)
- # malli (11)
- # meander (8)
- # nextjournal (3)
- # off-topic (5)
- # pathom (2)
- # portal (43)
- # rdf (2)
- # re-frame (8)
- # reagent (7)
- # reitit (1)
- # shadow-cljs (27)
- # spacemacs (31)
- # sql (2)
- # tools-deps (3)
- # vim (1)
- # xtdb (12)
(m/rewrite {:items [{:a 10 :b 20 :c 55}
{:a 30 :b 40 :c 45}]}
{:items [{:a !alpha :b !beta} ...]}
{:items [{:alpha !alpha :b !beta} ...]})
@jimmy thank you!
As part of a larger transform, I’m trying to handle parameters that follow this pattern, i.e., they can either express simple operations to apply to a set, or they can express the desired value of the set:
;; owners current state: [3 4 5]
{:owners {:add [1 2] :remove [3 4]}} ;; => {:owner_ids [1 2 5]}
{:owners {:reset [1 2]}} ;; => {:owner_ids [1 2]}
Right now I’m doing the actual application of the add/remove operations in Clojure code in the RHS of a match
:
(m/match
{:ctx {:db db}
:attributes {:id 123
:input {:owners {:add [1 2] :remove [3 4]}}}}
{:ctx {:db ?db :as ?ctx}
:attributes {:id ?id
:input {:owners ?owner-ops}}}
{:ctx ?ctx
:attributes {:id ?id
:input {:owner_ids (apply-ops
(get-current-owners ?db ?id)
?owner-ops)}}})
Is there a more idiomatic way to do this that expresses more of the transformation in Meander?In terms of idioms, I would say just try to balance what Meander and "vanilla" Clojure are good at. For example, it is totally possible to apply the owner operations you have here using m/rewrite
but, apart from being a good exercise, it may confuse people on your team, etc.
I have this rewrite where I’m using m/app
to slightly post-process the output. In the case of !description
I would like to keep the original value as well as the post-processed one, but mentioning it twice in the output means that 2 descriptions are consumed per :invoice/item
. Is there a way to overcome this?
(defn xero-invoice->datom [invoice & {:keys [service/provider]}]
(m/rewrite
(assoc invoice ::provider provider)
{::provider ?provider
:Invoices
[{:LineItems [{:UnitAmount !unit-amount
:LineAmount !line-amount
:Description !description
:Quantity !quantity}
...]
:CurrencyCode ?currency
:InvoiceNumber ?invoice-id
:DateString ?issue-date
:Total ?amount
:DueDateString ?due-date
:InvoiceID ?xero-id
:TotalTax ?vat
:Contact {:Name ?xero-contact-name}}]}
{:service/provider ?provider
:service/consumer (m/app names->consumers ?xero-contact-name)
:time/issue-date (m/app instant/read-instant-timestamp ?issue-date)
:time/due-date (m/app instant/read-instant-timestamp ?due-date)
:payment/amount ?amount
:payment/currency (m/app currency ?currency)
:payment/vat ?vat
:xero/id ?xero-id
:invoice/id ?invoice-id
:invoice/items [{:person/consultant (m/app desc->consultant !description)
:xero/description !description
:service/quantity !quantity
:service/unit :hours
:payment/amount !line-amount}
...]}))