This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-02-09
Channels
- # announcements (3)
- # babashka (17)
- # beginners (75)
- # calva (43)
- # cider (1)
- # clj-kondo (54)
- # cljdoc (8)
- # cljs-dev (70)
- # clojure (83)
- # clojure-europe (27)
- # clojure-nl (4)
- # clojure-norway (4)
- # clojure-spec (8)
- # clojure-uk (13)
- # clojured (2)
- # clojurescript (47)
- # community-development (4)
- # conjure (2)
- # cursive (3)
- # datomic (5)
- # emacs (5)
- # events (3)
- # fulcro (13)
- # graalvm-mobile (19)
- # helix (2)
- # introduce-yourself (1)
- # jobs (2)
- # jobs-discuss (28)
- # kaocha (9)
- # livestream (11)
- # malli (15)
- # meander (24)
- # nextjournal (8)
- # off-topic (26)
- # pathom (1)
- # pedestal (8)
- # polylith (2)
- # portal (31)
- # re-frame (4)
- # reagent (10)
- # reitit (8)
- # remote-jobs (3)
- # sci (1)
- # shadow-cljs (66)
- # spacemacs (20)
- # testing (6)
- # vim (15)
- # xtdb (7)
I'm rewriting html I parsed with crouton
and trying to collect text from within nested tags, but I miss something with the flattening part. What's missing in this pattern?
{:tag (m/or :code :span :p :div :em :a)
:content (m/some [!x ...])}
[(m/cata !x) ...]
I started writing code like https://clojurians.slack.com/archives/C03S1KBA2/p1644357312001629 instead of cata. It lets me debug it more easily.
Sorry, I’m not sure from your answer.
If the value of :content
isn’t a vector then [!x …]
won’t match.
(m/rewrite '(1 2 3)
(m/seqable !x ...) [!x ...])
;; [1 2 3]
The value of :content
is a vector of strings or maps which will contain more :content
I recommend adding the key to cata
as the first argument, which will tell us what should happen
Not sure I follow. specifically, I'm parsing some html table:
(m/rewrite (second tables)
{:tag :table
:content [(m/cata !m) ...]}
[!m ...]
{:tag :tbody :content nil} {}
{:tag :thead
:content
[{:content
[{:content [?parameter]}
{:content [?description]}]}]}
{:parameter ?parameter
:description ?description}
{:tag :tbody
:content [!tr ...]}
[(m/cata !tr) ...]
{:tag :tr
:content
[{:tag :td :content [?key ?desc]}
{:tag :td :content [(m/cata !doc) ...]}]}
{:field (m/cata ?key)
:type (m/cata ?desc)
:doc [!doc ...]}
{:tag (m/or :a :code :span :p :div :em :a :ul :li :i)
:content (m/some [(m/cata !xs) ...])} [!xs ...]
{:tag (m/or :a :code :span :p :div :em :a :ul :li :i)} nil
{:tag :br} "\n"
?x ?x)
{:tag (m/or :code :span :p :div :em :a)
:content (m/some [(m/cata [:flatten !x]) ...])}
[:flatten [!xs ...]]
[!xs ...]
[:flatten ?x]
?x
this way, every time you use cata in any place, it will either unpack the vector or return the argument unchanged
Even a tiny example like
{:tag :p
:content
[{:tag :p
:content
[{:tag :p
:content ["Hello"]}
{:tag :p
:content ["world"]}]}
{:tag :p
:content
[{:tag :p
:content ["Yes"
{:tag :p
:content ["No"]}]}]}]}
(m/rewrite data
{:tag :table
:content [& [(m/cata !m) ...]]}
[!m ...]
{:tag :tbody :content nil} {}
{:tag :thead
:content
[{:content
[{:content [?parameter]}
{:content [?description]}]}]}
{:parameter ?parameter
:description ?description}
{:tag :tbody
:content [!tr ...]}
[& [(m/cata !tr) ...]]
{:tag :tr
:content
[{:tag :td :content [?key ?desc]}
{:tag :td :content [& [(m/cata !doc) ...]]}]}
{:field (m/cata ?key)
:type (m/cata ?desc)
:doc [!doc ...]}
{:tag (m/or :a :code :span :p :div :em :a :ul :li :i)
:content (m/some [(m/cata !xs) ...])} (m/cata [!xs ...])
{:tag (m/or :a :code :span :p :div :em :a :ul :li :i)} nil
{:tag :br} "\n"
(m/with [%a (m/some !xs)
%b [(m/or %b %a) ...]
%c (m/or %b %a)]
%c)
[!xs ...]
?x ?x)
;; => ["Hello" "world" "Yes" "No"]