If I am trying to an array into the index.cljs.hl page how do I go about using the array in Clojurescript. I found that I can use:
(loop-tpl :bindings [single-data rpc/test-vector]
(h2 single-data))
In the hLisp part but if I want to use the array above where the html tag is I seem to run into problems. The array in the rpc.cljs page is as follows
(defc= test-vector ["Good" "Man" "Shoe"])
And I have tried using map without the data and just an array and it works:
(defn build-list
[]
(map #(h2 %) ["one" "two" "three"]))
But then if I try and use the array it no longer works and I have no clue why.
(defn build-list
[]
(map #(h2 %) rpc/test-vector))
Cheers
If you put '#rpc/test-vector' with the last example it works.
Cheers
Related
I'm new to both xpath and html so I'm probably missing something fundamental here. I have a html where I want to extract all the items displayed below. (I'm using scrapy to do my requests, I just need the proper xpath to get the data)
enter image description here
Here I just want to loop through all these items and from there and get some data from inside each item.
for item in response.xpath("//ul[#class='feedArticleList XSText']/li[#class='item']"):
yield {'name': item.xpath("//div[#class='intro lhNormal']").get()}
The problem is that this get only gives me the first item for all the loops. If I instead use .getall() I then get all the items for every loop (which in my view shouldn't work since I thought I only selected one item at the time in each iteration). Thanks in advance!
It seems you're missing a . in your XPath expression (to "indicate" you're working from a context node).
Replace :
yield {'name': item.xpath("//div[#class='intro lhNormal']").get()}
For :
yield {'name': item.xpath(".//div[#class='intro lhNormal']").get()}
You do miss smth. fundamental. Python by default does not have xpath() function.
You better use bs4 or lxml libraries.
See an example with lxml:
import lxml.html
import os
doc = lxml.html.parse('http://www.websters-online-dictionary.org')
if doc:
table = []
trs = doc.xpath("/html/body/div[1]/div[1]/table[1]/tbody/tr")
for tr in islice(trs, 3):
for td in tr.xpath('td'):
table += td.xpath("/b/text() | /text()")
buffer = ''
for i in range(len(table)):
buffer += table[i]
the full explanation is here.
I have gotten JSON info from an open API using
(def station-info (clj-http.client/get statinfo {:as :json}))
I have spit that information into a .clj file defined as si. It's content look like this:
{:stations [{:station_id "1755", :name "Aker Brygge", :address "Aker Brygge",
:lat 59.91118372188379, :lon 10.730034556850455, :capacity 33}
{:station_id "1101", :name "Stortingstunellen", :address "RÃ¥dhusgata 34",
:lat 59.91065301806209, :lon 10.737365277561025, :capacity 24}]}
When I call the function (map :station_id (:stations si)) it returns an empty list "()".
But if I define a function with the same info in the REPL and then use the same function, it works!
Very strange.
EDIT: Fixed it by turning the string from the file into a data structure:
(def si-data-structure (edn/read-string (slurp si)))
Your function is right, so your data must be wrong. In particular, you will surely find that (:stations si) is also empty. Look at the si variable and see if it really contains what you expect: is it a map? Is :stations one of its keys?
I Fixed it by turning the string from the file into a data structure:
(def si-data-structure (edn/read-string (slurp si)))
I am a newbie and making some exercises. How can I put a def with a list of sentences and a randomizer function inside a defn function? How does that work?
(def list["test1", "test2", "test3"]) - works fine
(rand-nth list) - works fine
How do I put it inside a function defn?
Thanks for help.
IIUC you just want to reimplement rand-nth, no?
(defn wrapped-rand-nth [a-list]
(rand-nth a-list))
If you want the list to be static (non-changing)
(defn randomize []
(rand-nth ["test1" "test2" "test3"]))
works, but it creates the vector upon each call, a better way is to
(let [the-list ["test1" "test2" "test3"]]
(defn randomize []
(rand-nth the-list)))
I have a JSON blob similar to the following:
[
{
"version": 1
},
{
"version": "3"
},
...
]
Note that some of the versions are numbers and some are strings.
I want to get a list of versions.
I can use the following lens combination to extract the numeric versions:
v1 :: [String]
v1 = obj ^.. AL.values . AL.key fieldName . AL._Number . to show
And the following to extract the strings
v2 :: [String]
v2 = obj ^.. AL.values . AL.key fieldName . AL._String . to T.unpack
But, how can I get a list of versions by a single pass over the list?
Is there any lens combinator that takes lenses AL._Number . to show and AL._String . to T.unpack and returns a combined getter so that if the first one failes, tries the second one? Something like msum for lenses?
There is in fact a combinator that tries an optic and goes to a backup if the first one fails. It's called failing.
Note that the condition on it should be satisfied by the case you describe. Even if it wasn't, the combinator would still function, it would just behave irregularly when refactoring. (Which is the main problem with using filtered as a Traversal.)
Before Carl's answer, which is what you should use, I was going to suggest outside as a way to perform case analysis with those prisms:
tryNumberThenString :: AL.AsPrimitive t => t -> [String]
tryNumberThenString =
outside AL._Number .~ (:[]) . show $
outside AL._String .~ (:[]) . T.unpack $
const []
v1 = obj ^.. AL.values . AL.key fieldName . folding tryNumberThenString
Note that, unless there is some other trick I am missing, this is not only more complicated than what Carl suggests but also less flexible -- I can only get a Fold from the plain function tryNumberThenString, while failing combines the prisms into a Traversal.
I have difficulty processing a list a Scala:
Currently I have a list of like this
(List(JString(2437), JString(2445), JString(2428), JString(321)), CompactBuffer((4,1)))
and I would like after processing, the result will look like below:
( (2437, CompactBuffer((4,1))), (2445, CompactBuffer((4,1))), (2428, CompactBuffer((4,1))), (321, CompactBuffer((4,1))) )
Can any body help me with this issue?
Thank you very much.
Try this:
val pair = (List(JString(2437), JString(2445), JString(2428), JString(321)),
CompactBuffer((4,1)))
val result = pair._1.map((_, pair._2))
First, pair._1 gets the list from the tuple. Then, map performs the function on each element of the list. The function (_, pair._2) puts the given element from the list in a new tuple together with the second part of the pair tuple.