This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # announcements (2)
- # beginners (448)
- # calva (10)
- # cider (27)
- # clojure (121)
- # clojure-argentina (2)
- # clojure-brasil (6)
- # clojure-chicago (1)
- # clojure-colombia (9)
- # clojure-ecuador (1)
- # clojure-europe (1)
- # clojure-italy (14)
- # clojure-mexico (1)
- # clojure-nl (19)
- # clojure-spec (11)
- # clojure-uk (160)
- # clojurescript (25)
- # core-async (7)
- # cursive (8)
- # data-science (1)
- # datascript (1)
- # datomic (4)
- # devops (2)
- # graalvm (3)
- # hoplon (1)
- # immutant (1)
- # jackdaw (32)
- # jobs (2)
- # joker (2)
- # keechma (9)
- # leiningen (26)
- # luminus (13)
- # off-topic (2)
- # overtone (2)
- # quil (5)
- # reagent (38)
- # reitit (13)
- # shadow-cljs (16)
- # spacemacs (2)
- # tools-deps (4)
- # xtdb (12)
- Can I do that without protocols? (without performance hint) - Can I do it faster on JVM?
(defprotocol IFib (fib-nth [this ^Long n])) (deftype Fib [^BigInteger prev ^BigInteger current ^BigInteger tmp ^Long idx] IFib (fib-nth [this n] (set! (.-idx this) n) (while (pos? idx) (set! (.-idx this) (dec idx)) (set! (.-tmp this) (.add prev current)) (set! (.-prev this) current) (set! (.-current this) tmp)) current)) (fib-nth (new Fib BigInteger/ZERO BigInteger/ONE BigInteger/ZERO (long 0)) (long 7))
Oh god. Sorry
It's simpler, easyier and ~20% faster.
(defn fib-nth-v2 [n] (loop [^BigInteger prev BigInteger/ZERO ^BigInteger current BigInteger/ONE ^Long idx n] (if (pos? idx) (recur current (.add prev current) (dec idx)) current)))
it means "this is a boxed long object" which if you are type hinting is never what you want
the above just reads like someone who doesn't know what they are doing trying to blindly optimize the code
I should say, "someone who doesn't know clojure and doesn't have experience with functional programming"
If mutable state is what you're looking for, I'd suggest you check out
If you haven't heard of it, it's a part of what's called the Clojure STM and is the 'Clojure' way of doing anything involving mutation in a very controlled manner
Here's the Brave Clojure chapter on it, https://www.braveclojure.com/zombie-metaphysics/
However, if you're mutating things in Clojure, most of the time there's a simpler way to do it
fib is basically the worst way to do that, because fib is not a function that requires mutable state and is an almost universal example for teaching how to write a pure function
playing with mutable state and fib is like "how can I write fib in a way that is harder to understand, and will perform worse?"
(defn fib ^long [^long n] (loop [prev 0N current 1N idx n] (if (pos? idx) (recur current (+ prev current) (dec idx)) current)))
is easier to understand, and, well, I haven't bench marked, and the jvm can really chew through code, but I would be surprised if it isn't as fast or faster then that nonsense
^ It is faster than that "nonsense" 565.453129 ns vs the nonsense being 973.900396 ns.
hey everyone! I'm trying to make a new data reader here, and my
data_readers.cljc contain following:
The problem is, I mark something in completely unrelated namespace and I get this error:
(ns kasta.i18n) (defn t [arg1] arg1)
I wonder what I could be doing wrong? I moved out i18n ns to a separate library since I thought that could be a problem, but no luck here. Any pointers?
Syntax error reading source at (mk/fe/common/header.cljc:42:29). Attempting to call unbound fn: #'kasta.i18n/t
kasta.i18n/t anywhere? If so, is it possible that something tries to use it before it's actually defined?
If you don't use
declare then I have no idea, sorry. Maybe you could create a minimal working example and create a gist or something out of it that you could share?
I guess I'll try to do that myself and work up to the situation it's not working anymore... It's pretty puzzling 😞
does anyone know of a way to extract all the list of requires given a ns (without parsing ideally?)
ns-map would help but it doesn't atm sadly
I could parse it with https://github.com/clojure/tools.analyzer maybe but the output doesn't seem so user friendly
I ended up with some regexps for now, it's probably in this case, but would be nice to know if there a better way
clojure.tools.namespace.file/read-file-ns-decl gives me the ns declaration at least
I'm doing some weird stuff with it here https://github.com/Olical/conjure/blob/ea4b3090536669fac1f3bd58c2753af634373576/src/conjure/munge/main.clj Reading in all decls, getting their requires then sorting them topologically to get an order to load the files in. Might come in handy!
I havent tested it but https://clojuredocs.org/clojure.java.shell/sh should work, im sure there is a better way though
I found when using
"python" "xx.py", the full path of
python must be provided, otherwise any third-party imported lib in
xx.py couldn't be found.
i had trouble with stuff like this, it might because the path is getting mucked with due to this https://clojurians.slack.com/archives/C03S1KBA2/p1560950692262600 or because it doesnt use the same path as your typical shell
:env can be wonky, if you override it you might need to get the current env and add to it instead of just setting it
and how can I wait for some asynchronous response from the outside program if it costs much time?
my purpose is to call tensorflow-based python program and get the response in my clojure process, maybe libpython-clj is not enough, a http server might be ok
I’d like to deploy a library to clojars but only the files under the src folder. How can I do that?
@viebel by default only src and resources are included I think (if you're using Lein). You can rename resources dir to dev-resources and update your project.clj to add that directory to the classpath in dev profile. Is that what you're after ?
hey all, is there a way to get a Figwheel-style repl for clojure? One with auto-complete, colors, etc? The
lein one is a little lacking
@nbtheduke I think you want
rebel-readline ? Try https://github.com/bhauman/rebel-readline#leiningen
Hi, how to parallelize I/O intensive operations (remote calls) while controlling the amount of parallelism? (other than core.async). Shouldn't https://clojure.github.io/clojure/branch-master/clojure.core-api.html#clojure.core.reducers/fold be able to do just that?
(r/fold N-in-parallel r/cat r/append! (r/map remote-call (vec my-data))) ?
This is what I tried
and expected it to print "parallelism" > 1 (ie around 100/10) but it does not. Why?
(let [maxp (atom 0) cnt (atom 0)] (add-watch cnt :x (fn [_ _ _ current] (swap! maxp max current))) (->> (vec (range 100)) (r/map #(do (swap! cnt inc) (try (Thread/sleep 10) % (finally (swap! cnt dec))))) (r/fold r/cat r/append!)) (str "Done, max parallelism: " @maxp))
As far as I can see from your code, you have not specified n, so it defaults to 512, and since your vector is 100 long, it’s all done in one partition
OMG how could have I forgotten to put N in? Thanks!
n=1 I get max parall. = 4.
With nr items 1000, op duration 100ms and partition size 1 I still only get parallelism 4, expected much higher.
that’s dependent on chunk size. Have a look at http://clojuredocs.org/clojure.core/chunk#example-5c9cebc3e4b0ca44402ef6ec
Thanks, @U054W022G! So there is no way to tell clojure to "do N operations in ||" other than using core.async or manual partitioning and using future on each partition, perhaps with a custom pool with a particular number threads?
it’s not perfect approach (all blocking threads waiting on IO) but it depends on what you’re doing
I would suggest creating your own executor limited to exactly the number of threads you want to limit the parallelism to, and then schedule all your operations on that executor
executors are simple and easy to use and do exactly what you want. the big downside is by default they can act as infinite queues, which, as long as you have back pressure elsewhere shouldn't be an issue
@U054W022G my understanding is that if you are bottlenecked by IO, limiting thread usage to core count isn't actually useful
People around here put me on to Claypoole.
upmap turned out to be very useful for my particular case.
Same - we use claypoole in a lot of places and after tuning pool sizes it's been working like a charm for us
Though I don't really need many threads if I can get the remote calls to release their threads while they wait for a response...
threads are relatively cheap - eg. badly planned async / polling IO can be more costly via added code complexity compared to the overhead of heap usage for more idling threads
it seems like I often see people coming from languages / vms where threads are expensive or hard to use applying criteria that don't neccessarily apply for clojure on the jvm
if you create a set of namespaces that form a cycle, the compiler will error and show you precisely which namespaces were in the cycle
ideally one starts a project with a "big picture" understanding of how namespaces should relate to each other, but of course with a bigger team that gets lax
We have stuff partially loaded on first refresh, but the compiler isn't complaining. I think it might be related to Clara rules, which does some magic.
Currently: Kill repl -> boot system -> no cycles -> system broken -> refresh -> system works.
a small wrapper around clojure.tools.namespace.repl/refresh that prevents losing the reference to the running system
refresh is pretty good at detecting cycles and complaining about it as well, as long as you have correct dependency information in your ns forms
so repl/refresh isn't getting correct dependency information when it parses the ns forms
further, I will guess that you are importing a defrecord class instead of requiring its namespace and using the factory function
Our GraphQL endpoint returns zero entries for a kind of object that's created by a Clara rule with a null left-hand-side (meaning it should be created at compile time).
deftype / defrecord / defprotocol are not idempotent - calling twice creates two distinct classes with the same name and package
not actually - I misspoke. That's the "refresh" wrapper, but we are using the "restart" wrapper in this case, which does not keep the running system.
are you requiring the namespace that defines your defrecords every place you construct them?
I do not know if it will find anything regarding ns forms or dependencies that you are not already seeing with the suggestion above, but if your project is Clojure/JVM you can try running Eastwood on it: https://github.com/jonase/eastwood
One thing it does that a lot of other tools do not is find mismatches between namespace names and the file name they are defined within, which causes trouble for some tools.
@hiredman hmmm, this could be it. Clara does some magic here, too... and I think I remember something that could have broken this.
my guess is somehow in the normal order of loading your project, you get a defrecord defined twice, and a clara rule is creating an instance check will the first class type, and then the record is re-def'ed, and at runtime you get instances of a different class with the same name
and refreshing is causing everything to get reloaded without the double def'ing which makes it work
when analyzing clojure code, should this be supported?
so, multi-arity defmulti/defmethod, in this syntax?
(defmulti xyz (fn ([x y] x) ([x y z] x))) (defmethod xyz "z" ([x y] "z") ([x y z] "z"))
A bit of REPL testing with Clojure 1.10 seems to indicate the implementation is happy with that. Whether it should be supported is above my pay grade 🙂
defmethod takes a “fn-tail” iirc - basically everything in an fn form except literally the fn symbol (including name iirc)
yep, including name, which is massively helpful for making readable stack traces for multimethods
I know a few people have created libraries/tools for formatting/streamlining//etc. Java stack traces. I also know that sometimes these tools remove stuff that makes it more difficult to debug things in some situations, so much so that Stuart Sierra created a Clojure lib that actively tries to disable hooking/patching of such things: https://github.com/stuartsierra/stacktrace.raw
Has anyone written up documentation or articles on reading and interpreting Java stack traces, the raw ones, targeted at Clojure developers. I say targeted at Clojure developers, because there are definitely some Clojure-specific tips that would be helpful to know there, including the kind of thing bronsa just mentioned above.