This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-02-17
Channels
- # announcements (12)
- # babashka (27)
- # beginners (65)
- # biff (8)
- # calva (22)
- # clj-kondo (1)
- # clj-otel (5)
- # clojure (65)
- # clojure-europe (127)
- # clojure-nl (1)
- # clojure-norway (11)
- # clojure-portugal (2)
- # clojure-uk (2)
- # clojurescript (18)
- # cursive (5)
- # data-science (3)
- # datahike (14)
- # datascript (3)
- # datomic (7)
- # deps-new (11)
- # emacs (31)
- # exercism (1)
- # fulcro (1)
- # honeysql (3)
- # hyperfiddle (38)
- # introduce-yourself (4)
- # leiningen (2)
- # malli (20)
- # meander (2)
- # missionary (3)
- # off-topic (4)
- # pathom (3)
- # practicalli (2)
- # reagent (5)
- # releases (1)
- # sci (1)
- # shadow-cljs (9)
- # xtdb (8)
(def data
{:editions [{:book {:id 0
:title "title1"
:authors ["author1" "author2"]
:pages 123
:isbn "2353-2343"
:publication-year 1995
:first-edition true}}
{:book {:id 1
:title "title2"
:authors ["author3"]
:pages 123
:isbn "6745-4623"
:publication-year 2001}}
{:book {:id 2
:title "title1"
:authors ["author3"]
:pages 123
:isbn "6745-4623"
:publication-year 2001}}
{:book {:id 3
:title "title2"
:authors ["author4"]
:pages 123
:isbn "6745-4623"
:publication-year 2001}}
{:book {:id 4
:title "title1"
:authors ["author3"]
:pages 123
:isbn "6745-4623"
:publication-year 2001}}]})
(defn join-books [editions]
(m/find editions
(m/with [%book {:book {:title ?title
:authors [!authors ...]
& !book-data}}]
[%book & [(m/or %book !not-book) ...]])
(cons {:title ?title
:authors !authors
:book-data !book-data}
(join-books !not-book))))
(join-books (:editions data))
;; =>
({:title "title1",
:authors ["author1" "author2" "author3" "author3"],
:book-data
[{:id 0,
:pages 123,
:isbn "2353-2343",
:publication-year 1995,
:first-edition true}
{:id 2, :pages 123, :isbn "6745-4623", :publication-year 2001}
{:id 4, :pages 123, :isbn "6745-4623", :publication-year 2001}]}
{:title "title2",
:authors ["author3" "author4"],
:book-data
[{:id 1, :pages 123, :isbn "6745-4623", :publication-year 2001}
{:id 3, :pages 123, :isbn "6745-4623", :publication-year 2001}]})
join-books
can also be written with m/rewrite
but m/find
is best starting out.
(defn join-books [editions]
(m/rewrite editions
(m/with [%book {:book {:title ?title
:authors [!authors ...]
& !book-data}}]
[%book & [(m/or %book !not-book) ...]])
[{:title ?title
:authors [!authors ...]
:book-data [!book-data ...]}
& (m/cata [!not-book ...])]
_else []))