This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-03-09
Channels
- # admin-announcements (3)
- # beginners (60)
- # boot (95)
- # braid-chat (2)
- # cider (12)
- # clara (1)
- # cljsrn (30)
- # clojars (2)
- # clojure (58)
- # clojure-art (1)
- # clojure-austin (3)
- # clojure-madison (11)
- # clojure-miami (6)
- # clojure-norway (3)
- # clojure-poland (13)
- # clojure-russia (67)
- # clojurescript (108)
- # core-matrix (39)
- # cursive (15)
- # data-science (6)
- # datomic (3)
- # hoplon (7)
- # jobs (9)
- # jobs-discuss (40)
- # ldnclj (5)
- # off-topic (6)
- # om (91)
- # proton (9)
- # re-frame (29)
- # reagent (7)
- # spacemacs (3)
- # testing (9)
- # yada (1)
i’ve pulled meta tags out of a web page with enlive, and i want to combine them into maps
@swizzard: so what form is the the data you have, and what form do you want it to be in?
the input is a seq of maps that look like this: {:attrs {:content "17" :itemprop "numTracks"} :content nil :tag :meta}
so i want to turn a seq like ({:attrs {:content "17" :itemprop "numTracks"} :content nil :tag :meta} {:attrs {:content “foo" :itemprop "name"} :content nil :tag :meta}…)
into a seq like ({“numTracks” “17”, “name” “foo”,…}…)
so you want to extract the :itemprop
and :content
keys from :attrs
from each element and then accumulate them into a map. is that right?
({:attrs {:content "17" :itemprop "numTracks"} :content nil :tag :meta}
{:attrs {:content "2009" :itemprop "copyrightYear"} :content nil :tag :meta}
{:attrs {:content "10 Cc" :itemprop "byArtist"} :content nil :tag :meta}
{:attrs {:content "Rock" :itemprop "genre"} :content nil :tag :meta}
{:attrs {:content ""
:itemprop "url"}
:content nil
:tag :meta}
{:attrs {:itemprop "name"}
:content ("Live in Concert: Clever Clogs")
:tag :span}
{:attrs {:content "14" :itemprop "numTracks"} :content nil :tag :meta}
{:attrs {:content "1995" :itemprop "copyrightYear"} :content nil :tag :meta}
{:attrs {:content "10 Cc" :itemprop "byArtist"} :content nil :tag :meta}
{:attrs {:content "Rock" :itemprop "genre"} :content nil :tag :meta}
{:attrs {:content ""
:itemprop "url"}
:content nil
:tag :meta}
{:attrs {:content "Avex" :itemprop "publisher"} :content nil :tag :meta}
{:attrs {:itemprop "name"} :content ("Mirror Mirror") :tag :span}
{:attrs {:content "8" :itemprop "numTracks"} :content nil :tag :meta}
{:attrs {:content "1975" :itemprop "copyrightYear"} :content nil :tag :meta}
{:attrs {:content "10 Cc" :itemprop "byArtist"} :content nil :tag :meta}
{:attrs {:content "Rock" :itemprop "genre"} :content nil :tag :meta}
{:attrs {:content ""
:itemprop "url"}
:content nil
:tag :meta}
{:attrs {:itemprop "name"} :content ("Original Soundtrack") :tag :span}
{:attrs {:content "10" :itemprop "numTracks"} :content nil :tag :meta}
{:attrs {:content "0" :itemprop "copyrightYear"} :content nil :tag :meta}
{:attrs {:content "10 Cc" :itemprop "byArtist"} :content nil :tag :meta}
{:attrs {:content "Rock" :itemprop "genre"} :content nil :tag :meta}
{:attrs {:content ""
:itemprop "url"}
:content nil
:tag :meta}
{:attrs {:itemprop "name"} :content ("...meanwhile") :tag :span})
Are you getting 3 seqs by splitting at the span tags?
ahhh I see
i think i could like write a transducer that wraps completed maps in reduced
but that’s scary
You could use something like split-with
to grab all tags until you get to a particular one. If you did that iteratively, it would have the same effect as your partition
call.
here's what I'm thinking (pseudocode): (reduce (fn [[cur-album & rest :as albums] tag] (if (already-has-tag cur-album tag) (conj albums (new-album tag)) (conj rest (merge cur-album tag)))) [] albums)
that should probably be a list, not a vector as the initial value
no problem
the way I wrote it, you'd need an initial entry
@swizzard: I think it would make sense to map your tags into a more amenable format before combining them into albums. It's a lot easier to work with '({:num-tracks 17} {:copyright-year 2009} ...)
than the format you get from enlive.
(fn [[cur-album & r :as albums] {{:keys [:itemprop :content]} :attrs cntnt :content}]…
I think the reason I shy away from that is it feels like making one piece of code do two things.
I think it's fine.
I am a serial over-refactorer. 😛
honestly i’ve already got the map-validation function & don’t want to write another little helper function
that sounds very fair
The latter emits a string that can be parsed by the clojure reader
@chadhs: i don't know if this will actually help, but it's analogous to the difference between str
and repr
in python
the idea is that you could e.g. copy the result of (pr-str my-thing)
into the repl or some source code and get back my-thing