This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-11-03
Channels
- # beginners (167)
- # boot (22)
- # chestnut (3)
- # cider (9)
- # clojure (107)
- # clojure-berlin (1)
- # clojure-greece (3)
- # clojure-italy (6)
- # clojure-losangeles (6)
- # clojure-russia (8)
- # clojure-spec (71)
- # clojure-uk (42)
- # clojurescript (186)
- # community-development (1)
- # core-async (12)
- # core-typed (1)
- # css (15)
- # cursive (29)
- # data-science (11)
- # datomic (8)
- # defnpodcast (28)
- # duct (2)
- # fulcro (169)
- # graphql (6)
- # hoplon (3)
- # jobs-discuss (1)
- # kekkonen (5)
- # leiningen (11)
- # lumo (7)
- # off-topic (14)
- # om (1)
- # other-languages (14)
- # portkey (7)
- # re-frame (27)
- # reagent (14)
- # remote-jobs (1)
- # ring-swagger (5)
- # rum (15)
- # shadow-cljs (52)
- # spacemacs (59)
- # specter (78)
- # test-check (3)
- # vim (9)
- # yada (23)
so, let’s say I have used reify on an interface and it closes over on of my Clojure functions. At the REPL, I want to be able to redefine my fn and have the reified object call it. I assume a layer of indirection will do the trick, but what is the best way to achieve this? Use resolve first to get the fn and then invoke it?
@cpmcdaniel call the functions like this (#'foo x y)
instead of (foo x y)
Although vars work that way already
how do I express the following in clojure: 1. I have a job queue with 1000 items 2. I have 12 worker threads 3. whenever a worker thread finishes a job, it pops off the next job from the queue, and executes it
Java has an ExecutorService that is a great match for this but you would use interop for it
Maybe this? https://github.com/TheClimateCorporation/claypoole/blob/master/README.md
Or use that ^^
Which is a Clojurey wrapper for it
I'm using the ExecutorService. How do I call 'sxubmit' on a clojure function? The problem is that it appears to match both Collable and Runnable ... (fn [] ...)
Add a type hint to disambiguate
^Runnable
(def #^Runnable r (proxy [Runnable] [] (run [] (rand)))) <-- does your solution avoid the 'proxy' ?
You don’t need a proxy
All Clojure functions are Runnable
in The Joy of Clojure there's a passage stating: > Clojure functions are highly amenable to interoperability. Their underlying classes implement a number of useful interfaces does it means that the Clojure compiler wraps Clojure functions into Java classes that under the hood implement some interfaces or something like that?
@zignd Yes, each function is compiled to a class with a number of methods.
@zignd Here's the ancestors of a function type:
boot.user=> (defn foo [x] (inc x))
#'boot.user/foo
boot.user=> (class foo)
boot.user$foo
boot.user=> (ancestors *1)
#{java.util.Comparator clojure.lang.AFunction clojure.lang.IMeta java.io.Serializable clojure.lang.Fn java.lang.Runnable java.util.concurrent.Callable clojure.lang.IFn java.lang.Object clojure.lang.AFn clojure.lang.IObj}
The function compiles to a class called, in this case, boot.user$foo
(an inner class foo
in the boot.user
class which the namespace is compiled to), and it extends/implements all of those listed classes.@seancorfield Interesting, so the namespace is also compiled into a class, at least that's what it seems from your example. Does it mean that if I was to create a namespace with gen-class
its methods would be defined in the boot.user
class for example and not in an inner class?
The methods would be defined in the class of the namespace yes (and gen-class
lets you specify the class name if you want a different one).
It's why the default application you often see has a (gen-class)
in whatever/core.clj
and a -main
function -- and that's compiled to a whatever.core
class with a main
method (the -
is the default convention for gen-class
).
Thanks for your explanations @seancorfield!
Also note that anonymous functions get compiled to "unique" class names
boot.user=> (defn quux [n] (fn [m] (* m n)))
#'boot.user/quux
boot.user=> (def times2 (quux 2))
#'boot.user/times2
boot.user=> (type times2)
boot.user$quux$fn__1879
In this case quux
is compiled to an inner class inside boot.user
, and the anonymous function is compiled to an inner class called fn__1879
inside the quux
inner class.
Functions, all the way down 🙂
I’m trying to solve an issue we have testing a largish project with lein test
; occasionally the test run halts with
`Tests failed.
Error encountered performing task ‘test’ with profile(s): ‘base,system,user,provided,dev,humane-errors’
Tests failed.`
I just cannot get it to be more verbose than that. We’re running this on CircleCI in a docker container and the last test namespace to run seems to be one that contains some clojure.spec generative tests
other than that no clue. Does anyone have any idea on how I can figure out which test in the test namespace was the last one run or any idea on what the problem might be?
@joost-diepenmaat you can log test names by redefining report :begin-test-var
method: https://gist.github.com/metametadata/c40a5f099814e591cd627b874f9bb595#file-reporter-clj-L14
oh nice thanks!
Is there any idiomatic way of getting a map from a sequence of keys (with a value associated with each key based on the key)?
(into {}
(map (fn [a]
[a (derived-value a)])
[:alpha :bravo :charlie :delta :echo]))
I suppose you could replace that fn
with (juxt identity derived-value)
but that’s not necessarily any better
Huh, I hadn't thought about that. Thanks
@hiredman Nice, that is pretty concise.
(into {} (map (juxt identity (partial str :test))) [:a :b :c])
@hiredman Nice, that is pretty concise.
(into {} (map (juxt identity (partial str :test))) [:a :b :c])
(zipmap a (map derived-value a))
@alexmiller That certainly works, though I was looking for something that doesn't repeat a or require binding it.
(apply zipmap ((juxt identity #(map derived-value %)) a))
@alexmiller That is quite nice.
that’s awful :)
I’d use zipmap :)
Which is awful? 😛
the apply / juxt version
the simple zipmap one is far and away the clearest of these imo
Yeah, I'd agree.
@alexmiller Good call; zipmap feels cleaner in my actual use case as well.
(Compared to into/map/juxt)
unfortunately, the implementation of zipmap is not as fast as it could be atm, although in the majority of cases you won’t have enough data for it to make any difference
Yeah, there's only 7 keywords.
Good to know though.
I prefer the zipmap, though I want to give lip-service to medley.core
which has map-vals
to get rid of the the (into {} (map...
nastiness, such as
(map-vals derived-value (zipmap a a))
It's an extra step, but separating the steps of creating a self-keyed map might be a helpful pattern elsewhereoh sure, I agree that’s the intent
I wish we had map-keys / map-vals in core
Out of curiosity, why don't we?
b/c we consider new things for core somewhat rarely and I haven’t gathered the evidence to make a compelling case yet :)
Fair enough. Ah, thanks for the link, that was my next question.
vote away :)
Done!
I wrote map-keys into tools.deps.alpha this week :)
just seeing this, but my 2c — I happened to write map-keys, map-vals, and, just a couple days ago, map-kv in a utils library @alexmiller
oh sure, there are lots of them out there :)
they vary widely in quality and generality though which is why it’s not as obvious as you may think on what to add
(ImageIO/write (BufferedImage. 10 10 BufferedImage/TYPE_USHORT_GRAY)
"jpg"
( "test.jpg"))
^-- this returns false
.
How do I debug this ?I'm attempting to call some Clojure code from a Java project. It seems that when I attempt to do an "import clojure.java.api.Clojure;", it succeeds when I'm pulling org.clojure/clojure 1.7.0 (via Maven), but fails when I pull org.clojure/clojure 1.8.0 since the Clojure class apparently no longer exists. Has this Clojure class been deprecated/moved? (or is there a better way to call Clojure code from Java?)
I suppose I should clarify that my goal is to call code from a library written in Clojure -- so I may be barking up the wrong tree with my approach.
@aletts see https://clojure.org/reference/java_interop Calling Clojure From Java
the docs say you should use clojure.java.api.Clojure - I don’t see why it wouldn’t find that with 1.8 so I suspect something else is wrong
and I can verify I’ve used clojure 1.8 via clojure.java.api.Clojure in java code
Those are the docs I worked with initially (and then searched around to find some people with the import statements as well)
Hmm. Knowing that it's working for you with 1.8 should be helpful -- maybe I'll just have to wipe my .m2 repo
hey all, can I ask a very n00b clojure question?
@aletts this repo works https://github.com/noisesmith/clj-jsvc-adapter/blob/master/src/java/org/noisesmith/Cljsvc.java
@derpocious you don’t have to ask to ask, and there’s a #beginners channel you can use at your discretion
coo. thanks
@noisesmith Hmm. Apparently, Eclipse simply tells me clojure.java.api.Clojure doesn't exist when I'm pulling 1.8.0 (and it results in spurious error indicators everywhere), but it actually works fine. I don't have that problem when I pull 1.7.0.
But if I ask in the #beginner room then won't it just be all beginner there and no one to help me? 😛
@aletts well that’s weird
@derpocious Oh no, there’s lots of folks that go there looking for people to help
@noisesmith Actually - I stand corrected. The error indicators disappeared - seems I rebuilt/ran too fast. Cleaning my .m2 org/clojure stuff may have been the solution. Thanks.
:thumbsup:
Does anyone know if there is a website / post that outlines some of the main Clojure libraries (e.g. Ring, Compojure, Yada, http-kit, Aleph, etc), what each one does and how they similar / differ to each other, which are currently preferred, etc.?
Agreed
I know JUXT have the Clojure Radar
I tried to write something similar except it took into account the participation in the community of the library author. I got stuck on the calculating library author scores
Actually, that looks like a good overview
not sure how up to date it is
Thanks Adrian 👍 That's a pretty large list of libraries
I thought of clojure-toolbox too, but it only categorizes, it doesn’t really compare them