This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-09-24
Channels
- # announcements (2)
- # beginners (131)
- # calva (4)
- # cider (29)
- # cljs-dev (18)
- # cljsrn (8)
- # clojure (61)
- # clojure-czech (1)
- # clojure-europe (5)
- # clojure-italy (14)
- # clojure-nl (6)
- # clojure-switzerland (2)
- # clojure-uk (125)
- # clojuredesign-podcast (10)
- # clojurescript (25)
- # clojutre (15)
- # clr (4)
- # code-reviews (4)
- # data-science (1)
- # emacs (1)
- # events (2)
- # fulcro (12)
- # graalvm (4)
- # jobs (2)
- # keechma (1)
- # off-topic (1)
- # pathom (18)
- # re-frame (3)
- # reagent (7)
- # shadow-cljs (106)
- # spacemacs (33)
- # sql (12)
- # xtdb (5)
Yeah, got that from: https://clojure.org/guides/learn/functions#_anonymous_function_syntax
… is place holder for the rest of the values, it’s not valid syntax
%&
is valid inside #(...)
but doesn't work with macros
it gathers up all args that aren't referenced by number, as a list
i meant the ellipsis 😓
there was no elipsis when I looked
hopefully someone chimes in with a better solution
We'll see. are
is actually from a forked version of clojure.test, but getting approvals on a PR with changes to that will be difficult.
oh you’re writing tests?
you might want to stick to is
then iterate over your collection
The tests are in clojure using webdriver to do the test and then a mix of webdriver and ssh to validate.
Is this good code? https://github.com/viebel/Learning/blob/master/clojure/functionize.clj
so looking at your earlier code
(= (attr driver :name (name x) :value) y)
this is the template for your assertion
you can iterate through a collection with doseq
then for each pair of values x y
you’re concerned with, you can run that assertion
(doseq [[x y] [[0 0] [1 1] [2 2]] (is (= x y)))
small example… anyway that’s how i’d approach the problem and not do any of that functionize stuff
Hmm, if I changed the vector to a map (since they are :key "value" anyway) that would work, yes?
user=> (doseq [[x y] [[:a "a"] [:b "b"]]] (println x y))
:a a
:b b
I think you’re trying to get something like this(doseq [[x y] [[:a "a"] [:b "b"] [:c "c"]]]
(is (= (attr driver :name (name x) :value) y))
You can turn your vector into a map like you suggested and i think that’ll work(doseq [[x y] {:a "a" :b "b" :c "c"}]
(is (= (attr driver :name (name x) :value) y))
anyway, if you find yourself having to mess with macro expansion there’s a good chance there’s a better solution for what you’re trying to do
if you look at the docs for are this is what it’s doing
Example: (are [x y] (= x y)
2 (+ 1 1)
4 (* 2 2))
Expands to:
(do (is (= 2 (+ 1 1)))
(is (= 4 (* 2 2))))
Well, it through an exception, but I'm on the right track. Thank you so much for your time @christian.gonzalez! I need to get dinner and head home.
np good luck
I want to use Clojure to write some web scraping scripts. I'm preferring ClojureScript for the faster startup times and as prep-work to learn how the frontend side of things works. I chose planck
for no particular reason.
This is where I'm stuck.
I fire up Emacs with https://github.com/clojure-emacs/inf-clojure installed. I set inf-clojure-generic-cmd
to planck -d
.
(require '[planck.http :refer [get]])
(... (:body (get "")))
Now how do I get elements from that string? Is there a library similar to Python's BeatifulSoup? Searching DDG for "clojurescript html parsing" doesn't return anything that stands out as useful and I feel like that might be a silly thing to search for. Maybe the reason there's no library is because this entire ecosystem is built on JavaScript and there's some obvious internal I'm missing? Regardless of how it's really done, I can't even figure out how to get access to something like jQuery which is another question I have. I see the wrapper jayq
, but executing (require '[jayq.core :refer [$]])
with planck -D jayq:2.5.5
just results in Execution error (ReferenceError) at (<cljs repl>:1). Can't find variable: jQuery
@ericihli in order to use jayq you need too make sure the jquery lib is available, you might have better luck using google's more modern closure library (goog.*) instead https://developers.google.com/closure/library/docs/gettingstarted
closure comes with the java cljs compiler, I don't recall if it comes with planck though
perhaps goog.dom would be of use https://google.github.io/closure-library/api/goog.dom.html
eg. call goog.dom.createDom on your string to get a data structure
then use the other goog.dom functions on that structure
iirc there might be a cljs version of the enlive scraping / templating lib as well
Ah hah. That might be what I was looking for. I went to their api docs and searched for document
when I realized that's what all their functions worked on and the only thing I saw was xml.createDocument
but that gave me the same error that it couldn't find a document
to work on. I didn't find dom.createDom
. Let me try that.
oh wait - I think I had a visual off-by-one error and constHtmlToNode
might be the right thing
that's the one that takes html string(s)
I want to keep a ranking of users... what sort of datastructure would be good? a map indexed by rank?
user-rank { 1 {:username "hax0r"} 2 {:username "pax0r"}}
for examplewould that be good? Is there better?
orrrr, can I leave it like a vector of maps
and then keep a key in there :rank
and then do sort-by and reduce-kv or something each time?
(maybe too silly and computationally intense for no good reason)
lay it on me, what cha think
There is a library called data.priority-map that, like a normal map you insert key/value pairs into it, but unlike a normal map when you call seq
on it, it is guaranteed to return the key/value pairs in order by the value, which is the "priority" that you pick.
Not sure if that would be useful in your case: https://github.com/clojure/data.priority-map
that's actually perfect sounding
let me see. thanks a lot andy
Well, my use-case might be simpler.
Select number of users get initial invite codes that work. when a friend submits for their own invite code they have to use a working one as a referral. friend submits a referral and gets their own code. originating user gets a rise in rank.
my english at midnight is something to suffer thru, i apologize. beta invites. get your own invite, share it, compete with other users to rise up in ranks so you get one of the beta tester slots.
I think using a map that simply has the integers as the base-level keys is good for this, but I'm not sure how to update the order smoothly without messing everything up and klobbering stuff, so maybe priority map be the way.
can i force a key to be equal to a number of elements? o.O
Hmm. How about just reducing a sorted set into a rank for the item i'm interested in?
I can do this, (->> coll :a-key)
to return back the value of that key, why does, then (def xf (comp :a-key))
(into [] xf coll)
fail (and throw a null pointer)?
(comp :a-key)
is the same as :a-key
, so your form could be written (into [] :a-key coll)
How to get the position of an element in a sorted set?
map-indexed to the rescue!
(println (map-indexed (fn [idx p]
{:name (:firstname p)
:rank (inc idx)})
(sort-by :num-verified > @user-atom)))
I don't know if a sorted set supports index-of like behavior, but you could do it like this:
(reduce (fn [idx e] (if (= e :dude) (reduced idx) (inc idx))) -1 (sorted-set :foo :a :bar :dude :baz))
2
or with map-indexed indeedI was trying to keep it sorted initially, keep the keys as integers of the rank
but then i have to use a human brain to move things around the set...
if you want to lookup something by something, it's often handy to keep it in a map from something to something
now i've opted to keep a number of votes in each map and then invoke a sort-by when need be, but i was hoping to avoid all this sorting computation itme.
since users might want to know their rank multiple times even in a minute...
Not ready yet but once I’ve got the new release out I imagine you could rustle something up with https://github.com/riverford/compound/tree/compound2 - with a primary map index and a secondary sorted set
looks really neat , i might not have the number of brain cells required to make sense of it
I’ll try and remember to let you know when its out and knock up an example
even if you use something else, it would be a fun thing to put in the doco
to be clear you’ve got a list of things being voted on and want to be able to access them perhaps by id but also to keep a sorted version around ?
actually a little different
hacker news mention was a red herring
i'm just keeping a list of users ranked by "number verified invites" to give out beta access to the first 50 people
I want to know how Hacker News keeps a ranked list of Everything
powered by Arc (=lispy)
@sova maybe (def s (sorted-set-by (fn [x y] (< (:score x) (:score y))) {:score 101} {:score 99}))
can also help?
I'm seeing that this can lead to elements falling out: https://clojuredocs.org/clojure.core/sorted-set-by#example-542692d5c026201cdc327096
You need a total sort
Andy’s comparator guide covers this https://clojure.org/guides/comparators
I have a vector of maps, I want to update one keyed value in one particular map...
I feel like I can use update-in but i'm not sure
Thanks for mentioning Total Sort
ah assoc-in could work, i'm just setting it to true...
Thanks ^-^
oh no the null pointer exception found out where i live! :x
This function returns a null pointer and i don't know why.
(:rank (first (filter #(= (:code %) code) (map-indexed (fn [idx p] {:code (:code p) :rank (inc idx)}) (sort-by :num-verified > @user-rank)))))
stylistic choice: use threading or transducers here, which bracket is which is hard to read here
Threading: https://clojure.org/guides/threading_macros / Transducers: https://clojure.org/reference/transducers
do you mean it throws an npe?
basically just a filter, i am trying to insulate around nils but when it finds a matichng entry the GET says nullpointer exception
yes when accessed via browser ...
i dunno what's wrong with it because it works in repl as expected
OK, get never throws, but inc will throw if idx is nil
oh! thanks
so will > if :num-verified is nil
what do i do?
either use (fnil inc 0)
instead of inc
, or do some sort of validation / cleanup of the data
(ins)user=> (inc nil)
Execution error (NullPointerException) at user/eval1 (REPL:1).
null
(ins)user=> ((fnil inc 0) nil)
1
thanks so much
i was really pulling out beard over it
umm... actually map-indexed insures idx is never nil
so it has to be the >
in sort-by
What are the things that allow REPL-driven development to work as fine as it does in Clojure? I suposse immutability is one of the most important ones.