specter

Latest version: 1.1.3
2018-02-18T22:47:01.000108Z

Hi @nathanmarz and all. I'm stuck on a (probably) simple one. I have this xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<book>
<frontmatter/>
<chapter name="Introduction">
    <para>Here is the intro</para>
    <para>Another paragraph</para>
</chapter>
<chapter name="Conclusion">
    <para>All done now</para>
</chapter>
</book>

2018-02-18T22:47:29.000055Z

the Challenge is to get the string content from chapter elements with attr name = "Introduction" ; using thread last

(time (->> [doc]
           (mapcat :content)
           (filter #(= :chapter (:tag %)))
           (filter #(= "Introduction" (get-in % [:attrs :name])))
           (mapcat :content)
           (filter #(= :para (:tag %)))
           (mapcat :content)
           (filter string?)))
; transducers and compose
(time (eduction
        (comp
          (mapcat :content)
          (filter (comp (partial = :chapter) :tag))
          (filter (comp (partial = "Introduction") :attrs :name))
          (mapcat :content)
          (filter (comp (partial = :para) :tag))
          (mapcat :content)
          (filter string?))
        [doc]))
; specter: this isn't correct yet...
(select [:content ALL
         (filterer :tag #(= % :chapter))
         (filterer :attrs :name #(= % "Introduction"))
         :content ALL
         (filterer :tag #(= % :para))
         :content ALL
         string?] doc)

nathanmarz 2018-02-18T23:03:59.000067Z

@poppetew I think you're looking for selected? rather than filterer?

nathanmarz 2018-02-18T23:04:03.000064Z

(select [:content
         ALL
         (selected? :tag (pred= :chapter))
         (selected? :attrs :name (pred= "Introduction"))
         :content
         ALL
         (selected? :tag (pred= :para))
         :content
         ALL
         string?]
  doc)

1
nathanmarz 2018-02-18T23:04:28.000098Z

pred= is also slightly nicer than doing #(= % ...)