This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-07-17
Channels
- # beginners (42)
- # cider (1)
- # cljs-dev (20)
- # clojure (73)
- # clojure-italy (8)
- # clojure-nl (53)
- # clojure-spec (11)
- # clojure-uk (88)
- # clojurescript (170)
- # clojutre (6)
- # core-async (26)
- # css (2)
- # cursive (13)
- # data-science (10)
- # datomic (15)
- # editors (3)
- # figwheel (28)
- # figwheel-main (67)
- # fulcro (57)
- # graphql (2)
- # immutant (2)
- # jobs (1)
- # jvm (4)
- # lein-figwheel (3)
- # leiningen (1)
- # off-topic (5)
- # pedestal (28)
- # re-frame (86)
- # reagent (18)
- # reitit (8)
- # ring (3)
- # ring-swagger (2)
- # shadow-cljs (78)
- # spacemacs (10)
- # specter (12)
- # tools-deps (32)
- # vim (3)
Hello. I need a hint for the following problem: When writing hiccup, I often have a vector containing mostly static content: [:ul [:li "A"] [:li "B"] [:li "C"]]
. But sometimes I have to include values from an other sequence into my list. Here I need to include elements from the vector (def subitems ["B.1" "B.2"])
into my hiccup. Is there an elegant way to place some code between [:li "B"]
and [:li "C"]
which inserts elements from subitems
wrapped in [:li ...]
s?
I am looking for something which would be used like that: [:ul [:li "A"] [:li "B"] (magic-for [subitem subitems] [:li subitem]) [:li "C"]]
.
(let [subitems ["B.1" "B.2"]]
`[:ul [:li "A"] [:li "B"] ~@(for [subitem subitems] [:li subitem]) [:li "C"]])
@witek Hiccup will automatically unwrap non-vector sequences, splicing them into the enclosing element
I have big, icky XML blobs to extract data from and structure into sensible EDN. Are zippers the way to go?
@U06B8J0AJ, is the goal of the output EDN to contain all of the XML content with a different shape? Or is the edn going to be more of a flat structure?
I see, you could give a go at [clojure.xml :refer [parse]]
to parse it, and then use xml-seq
to pick the bit you are interested in. A few examples here https://clojuredocs.org/clojure.core/xml-seq
The first question is: can you fit that DOM into RAM. I've seen 2 MB xmls expand to over 100 MB DOM trees after java's xml parser was done.
So far, I’ve parsed the XML into an intermediate format. For example;
<journal-meta>
<journal-id journal-id-type="publisher-id">IBM</journal-id>
<journal-id journal-id-type="hwp">spibm</journal-id>
<journal-title-group><journal-title>International Bulletin of Mission Research</journal-title></journal-title-group>
<issn pub-type="ppub">2396-9393</issn>
<issn pub-type="epub">2396-9407</issn>
<publisher>
<publisher-name>SAGE Publications</publisher-name>
<publisher-loc>Sage UK: London, England</publisher-loc>
</publisher>
</journal-meta>
Gets turned into
[[:journal-meta
[[:journal-id "IBM" [[:journal-id-type "publisher-id"]]]
[:journal-id "spibm" [[:journal-id-type "hwp"]]]
[:journal-title-group
[:journal-title "International Bulletin of Mission Research"]]
[:issn "2396-9393" [[:pub-type "ppub"]]]
[:issn "2396-9407" [[:pub-type "epub"]]]
[:publisher
[[:publisher-name "SAGE Publications"]
[:publisher-loc "Sage UK: London, England"]]]]]]
Given this, I wrote a function called cherry-pick
, which takes a set of keywords, traverses the entire structure, and returns any hits for those keywords.
For example, running (cherry-pick #{:issn})
Returns
[[:issn "2396-9393" [[:pub-type "ppub"]]]
[:issn "2396-9407" [[:pub-type "epub"]]]]
So the general strategy is, flatten the XML a bit, and have a way of tearing out pieces of interest from it. Given the cherry-picked items, do some more context sensitive conversions. But I’m not at all certain that this is a good way to go about it.
A comparison with xml-seq
(->> xml
.getBytes
io/input-stream
xml/parse
xml-seq
(filter (comp #(= :issn %) :tag)))
;;({:tag :issn, :attrs {:pub-type "ppub"}, :content ["2396-9393"]}
;; {:tag :issn, :attrs {:pub-type "epub"}, :content ["2396-9407"]})
And yes, you should pay attention at the size of the input XML. If you get OOMs you could go data.xml
lib (which is lazy)
Given that I have to search the entire structure one way or another, won’t the lazy version become reified anyway?
well if you pipe the xml through one side and get the processed version on the other, the full XML doesn't need to stay in memory in full
np, you might need to tweak the filter if you use data.xml
, but the process is more or less the same. It looks to me you can avoid the intermediate step.
Perhaps, but I still need a version of filter that can act on nested data structures, right?
@U050ECB92 What lib do you use for xpath?
basically some helpers to execute an XPath and transform the stuff back to maps and vectors. You might want to try clj-xpath, looks decent
@U06B8J0AJ How about XSLT ? http://cocoon.apache.org/2.2/1290_1_1.html
Hi folks, can any tolitius/mount
users help me out? I'm trying to dispatch a multimethod using (constantly name-of-defstate)
, where name-of-defstate
is just a keyword. Is this possible? I can't see any reason the dispatch function should be called before I do (mount/start)
, but it's not working.
Did you mean symbol instead of keyword? Also, if your dispatch is that, then it will always dispatch to the same value. constantly
returns a function that always returns its argument (it doesn't call it). You probably want something like (defmulti my-fn <name-of-defstate>)
.
Hi oscar, I meant name-of-defstate
returns a keyword from its :start
, e.g. :dev
to indicate dev mode globally in my application. I had hoped (defmulti my-fn (constantly mode-defstate))
would dispatch to the (defmethod my-fn :dev ...)
.
constantly
is called before (mount/start)
so it's getting passed the wrong value as its parameter.
How can I get ns
metadata?
that works, thanks
can someone check my sanity?
A.B.C(D)
in clojure interop is
(.. A B (C D))
or (.. A B C D)
?
I know this is probably a sensitive subject, but is there a way to colocate clojure tests with the files they test. I know the src/test separation is more than just superficial, but I've always found putting tests in a special "far away" directory to never as be as helpful as keeping them next to their test on files.
clojure.test doesn't make a distinction between test namespaces and code namespaces, you can put clojure.test tests anywhere
there actually is no canonical way to do that
functions are compiled into classes and there is a demunge function that can be used to turn that class name into a string
however, the function name => class name transformation is potentially lossy so in some cases you will not get the identical function name you started with
I think the function meta provide some info like the function name but I can't remember
you may have a :name
meta for the function var
(-> #'map meta :name str) ;; => "map"
has anyone used https://github.com/Yleisradio/new-reliquary ?
I'm struggling to get it to work. it seems like the new relic agent is not picking up my newrelic.yml file
new question: how do I load a local jar in my lein project so that it's available when I start a REPL?
add the repo that the jar is in to your maven repos, or worst case install it to your local maven cache with lein install
lein install is iffy because it causes unpredictable dep resolution