This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-04-22
Channels
- # announcements (2)
- # architecture (33)
- # babashka (4)
- # beginners (445)
- # bristol-clojurians (10)
- # calva (23)
- # cider (43)
- # clj-kondo (36)
- # cljs-dev (13)
- # cljsrn (20)
- # clojure (136)
- # clojure-argentina (8)
- # clojure-dev (1)
- # clojure-europe (18)
- # clojure-germany (1)
- # clojure-italy (5)
- # clojure-nl (45)
- # clojure-spec (66)
- # clojure-uk (29)
- # clojurescript (69)
- # conjure (157)
- # cursive (2)
- # datomic (216)
- # emacs (10)
- # exercism (4)
- # figwheel-main (8)
- # fulcro (30)
- # graphql (21)
- # hoplon (5)
- # kaocha (7)
- # leiningen (3)
- # luminus (4)
- # music (1)
- # off-topic (24)
- # pathom (10)
- # re-frame (19)
- # reagent (11)
- # reitit (16)
- # remote-jobs (1)
- # ring-swagger (5)
- # rum (7)
- # shadow-cljs (125)
- # spacemacs (8)
- # sql (9)
- # tools-deps (12)
Hi, i just came across this on clojure docs and was wondering why; > ;; it is tempting to try an index on a list > (get '(a b c) 1) > ;;=> nil > > ;; but you should use nth > (nth '(a b c) 1) > ;;=> b Is there some performance implication regarding nth on a list? I'm a bit confused why get is off limits here. Thanks,
i guess i'm trying to decide if i should use nth and potentially incur a performance penalty, or coerce into a vector and then have get close at hand.
this is in the context of a list comprehension that i can fit into memory without too much issue
yeah, i guess thats the right question, i'm walking the entire sequence so i think i should shove it into a vector ?
i was getting at the opposite side of that. creating a vector walks the entire sequence. if you just need to find the nth thing once, just linear scan and don't pay the penalty for the datastructure creation on top of the O(n) behavior
if you need random access often, use a datastructure that provides the API and performance that you need
is finger-tree (https://github.com/clojure/data.finger-tree) . really that slow ?
You can run your own benchmarks to verify, but I have a recollection of doing or seeing some benchmark results that showed its constant factors were quite a bit slower for the operations that is has in common with vectors. vectors cannot efficiency insert new elements at the front, whereas finger trees can, so depending upon how often you want to do that, finger trees could be faster overall.
i have used it once for deque. since it has different purpose, comparing it's performance with normal vector is not fair. in my case, it was 2-3 times slower than Java's ArrayDeque. https://github.com/namenu/advent-of-code/blob/c87a9228a5b5bb3e3f70ba65c2a75a12d7fb43e3/src/aoc/year2018/day09.clj#L64
I'm trying to containerize my dev environment, which involves connecting to a database at a separate host. Connection is refused because apparently JDBC is trying to connect to localhost, even though the correct DATABASE_URL
env var is detected. Here is me debugging from inside the container:
www-data@d850df1729bc:/app$ clojure -A:dev -m
Starting nREPL server at localhost:7000
Connecting to database at URL: jdbc:
log4j:WARN No appenders could be found for logger (com.zaxxer.hikari.HikariConfig).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See for more info.
Apr 22, 2020 4:59:04 AM org.postgresql.core.v3.ConnectionFactoryImpl log
WARNING: ConnectException occurred while connecting to localhost:5432
.ConnectException: Connection refused (Connection refused)
...stack trace...
Here is the fn responsible for connecting:
(defn connect! []
(if-let [database-url (System/getenv "DATABASE_URL")]
(do
(println "Connecting to database at URL:" database-url)
(conman/connect! {:jdbc-url database-url}))
(throw
(ex-info "Database exception!"
{:causes #{:no-database-url}})))
Any ideas for what to poke at?However Docker Compose does it. I've tested it by running psql --host=dbhost --username=postgres rtc
inside the container and that works, so I don't think dbhost is the issue?
ok, I installed dnsutils inside the container, and both of these work:
psql --hostname=dbhost --username=postgres rtc
psql --hostname=$(dig +short dbhost) --username=postgres rtc
You need to look at conman, and then at the connection pool library conman actually uses
Conman pass the uri you give to https://github.com/pupeno/to-jdbc-uri and my guess is that is what is mangling things
also to-jdbc-uri
appears to be doing what's expected...
user=> (j/to-jdbc-uri (System/getenv "DATABASE_URL"))
"jdbc:"
Aha! This is coming from the migrations part of the code...later on in my db
ns. The conman/connect!
call is actually working!
So migratus is defaulting to localhost, which works outside my container but not in, which makes sense. So I just need to dig in to the right :db
params to pass to it. Thanks for the help!
is there any tool to find all unused vars (mostly functions) in a big clojure project?
by the same author, there is this: https://github.com/borkdude/carve
carve looks like what I'm looking for. Thanks
carve is rad
i want to get rid of unused legacy stuff
Hey guys, is there any way to spread a vector/list into the argument arguments of function call without using apply?
(If you're inside a syntax-quoted form, that is, which it looks like you are from the other comment)
I think I'm doing something wrong, (i'm a clojure beginner). I'll read a bit more and try to figure it out, thanks for the help
Can you not use the routes function directly? Apply should work then. https://github.com/weavejester/compojure/wiki/Routes-In-Detail
There is a third party macro that I want to spread into, since its a macro I cannot use apply. Specifically, in the compojure library, there is a defroutes
macro that I would like to call, with a list of pre-compiled routes from various modules. Instead of manually entering each and every module into the call, I'd like to do something like (apply defroutes app-routes list-of-all-my-routes)
(defn test-chan []
(let [c (a/chan 10)]
(a/onto-chan c [1 2])
(a/close! c)
(println (a/<!! c)) ; prints nil
(println (a/<!! c)) ; prints nil
(println (a/<!! c)))) ; prints nil
If I omit this sexp (a/close! c),
then everything is fine. wh closing on a channel twice will fail the takes ?
takes a val from port. Will return nil if closed. Will block
if nothing is available.
from documentation for <!!
"Puts the contents of coll into the supplied channel.
Returns a channel which will close after the items are copied."
does using go blocks causing much latency? I previously heard on this channel someone saying go block under jvm (clojure) is quite tricky regarding the performance.
by itself, adding a go block will slow down your code (if that's the only change). If you are doing something parallel in order to have better throughput then need to do complex coordinataion, a go block can facilitate that speedup if that makes sense
async is a debt / detriment you take on (in terms of both perf and debugability) that should be balanced by some other greater benefit which happens to need async
Hey! Do you have any general suggestions how to transform XML (possibly parsed via data.xml) efficiently (both in terms of performance and code readability and manageability) into nested map, extracting specific keys and values from tags and content at specific levels?
I thought about Specter, but I'm not sure if it's actually possible to collect stuff from multiple places into map with it.
I worked with XMPP in Clojure and ended up using transducers inspired by this blog post: https://juxt.pro/blog/posts/xpath-in-transducers.html
In general I need to build map from various nested properties of this XML. I've got multiple "objects" in this XML which I need to extract.
Probably a transducer to import the XML with (data.xml) and then you can easily create/extract nested maps with (get-in m [kp1 kp2 kp3])
This might be helpful for you to look at the nested values/maps https://github.com/dmillett/clash#data-shape
So it's something like: first go to tag x > tag y > tag x of id y and then take every children of tag z and build map out of it's tag a and b and c.
(for [maybe-x (tree-seq map? :content xml) :when (= (:tag maybe-x) :x) maybe-y (:content maybe-x) :when (= (:tag maybe-y) :y) :when (= (:id (:attr maybe-y)) (:id (:attr maybe-x)))] whatever)
or something
you can even do recursive queries with it by just wrapping it in a recursive function
For server-side access to Firebase, should I just use the java admin library or is there a preferred pre-existing wrapper, or should I use something like taika, a wrapper around the REST API?
Oh, sorry, you said Firebase 🙂 I've never really understood how *base and *store are related
Hey, does Clojure have an official/unofficial motto or philosophy? For example: Java: "Write once, run anywhere" Erlang: "Write once, run forever" Haskell: "Avoid success at all costs"
I think this was discussed some time in the past month or three. I recall there being many proposed answers, but none "official". Lots of proposed unofficial mottos, of course.
Oh no, I can't search back that far
there's a thread on this at https://ask.clojure.org/index.php/8347/is-there-a-slogan-for-clojure
Thanks Alex!
I've created a macro:
(defmacro defelem [name element]
`(ws/defcard ~name
(ct.react/react-card
(r/as-element
~element
)
)
)
)
But when I run it in the repl, I get the error TypeError: Cannot set property 'defelem' of undefined. Why is this and what's the fix?Macros have to be executed in Clojure, even for ClojureScript, since they rely on the Clojure compiler for expansion and then the resulting code can be compiled by ClojureScript. I think.
@pshar10 Depending on which ClojureScript REPL you're using, asking in #clojurescript #lumo #planck #devcards ... may yield better answers about how to deal with macros in those environments.
Say I have a list '(1 3 4 5), and I want to append to it some number n only if it already doesn't exist in it. How to do that in clojure elegantly?
(let [coll '(1 3 4 5)]
(if ((set coll) 4)
coll
(conj coll 4)))
Btw use vectors instead of lists.
Better version:
(let [coll '(1 3 4 5)]
(if (some #{4} coll)
coll
(conj coll 4)))
@nikolavojicic why vectors and not lists?
Vector data structure is more performant. List is a linked list, its purpose in Clojure is code writing mostly...
Yes, for adding elements at the beginning.
For search by index and for adding to the end, vector is faster.
Also, there are vector specific functions such as subvec.
for iterating all the way through then my guess is a vector because of arrays getting pulled into cache lines, but that's the kind of thing that needs measuring
Use vector always. Btw don't confuse lazy seq and list!
@nikolavojicic your example doesn't return what I want though
Fixed it, sorry, should have run it :)
And of course, if what you want is a collection of things with no duplicates, and order does not matter, then a set is the most appropriate 🙂
Added better version that uses some
.
@U0CMVHBL2 how to create an empty set? I'm using #{} but doing a conj on it is returning a vector
user=> (conj #{} 5)
#{5}
You do not get that result?
And if order of insertion matters, add all the elements and use distinct
at the end.
@U0CMVHBL2 Yes this works, but I'm using re-frame and initializing a db value to #{}
I do not now that db's limitations that it may have on the types of values it allows you to store.
I have some code that gives a reflection warning when run on OpenJDK 8 but does not give a reflection warning on OpenJDK 11 or OpenJDK 14. I found that surprising so I'd be interested to hear suggestions/explanations of what might have changed between JDK versions for that to happen. It's with this image metadata extractor library com.drewnoakes/metadata-extractor {:mvn/version "2.13.0"}
(I haven't checked whether it's a multi-release JAR which I suppose could account for it -- actually being different JVM bytecode on 8 vs 11/14)
(nope, that's not it -- no MR stuff in that lib)