This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-05-30
Channels
- # announcements (3)
- # aws (5)
- # beginners (71)
- # boot (7)
- # calva (74)
- # cider (6)
- # clj-kondo (2)
- # cljs-dev (5)
- # clojars (6)
- # clojure (84)
- # clojure-dev (7)
- # clojure-europe (1)
- # clojure-italy (23)
- # clojure-nl (43)
- # clojure-sanfrancisco (1)
- # clojure-spec (4)
- # clojure-uk (173)
- # clojurebridge (1)
- # clojurescript (14)
- # cursive (44)
- # datomic (9)
- # duct (2)
- # emacs (2)
- # fulcro (4)
- # graalvm (4)
- # graphql (27)
- # hoplon (6)
- # keechma (50)
- # off-topic (3)
- # other-languages (8)
- # pathom (2)
- # pedestal (14)
- # planck (5)
- # re-frame (3)
- # reitit (6)
- # ring (2)
- # robots (2)
- # spacemacs (9)
- # tools-deps (15)
- # vim (44)
Does anyone know how to set a custom entity resolver with clojure.xml/parse
? I need to reroute online DTDs to local ones. Currently I'm rewriting the XML itself with regexes, which seems a bit non-idiomatic.
regarding performance:
user=> (defn contains [t] (contains? #{:foo :bar} t))
#'user/contains
user=> (defn contains2 [t] (or (= :foo t) (= :bar t)))
#'user/contains2
it seems that contains2 is ~twice as fast, although the idiom in contains is very common.it is clear, in first case it have to create additional set structure and search in it. But it is more readable and noone thinks about 2 times fast on nanoseconds
maybe Clojure optimizes the first case by creating the set at compile time? but maybe not
Not sure about sets (they can hold values unknown on compile stage), but I guess that case t
will most fastest way for this code
or something like this:
(defmacro one-of [x elements]
`(or ~@(for [e elements]
`(= ~e ~x))))
(let [x 10] (one-of x [1 2 3]))
But why you try to catch nanoseconds but not clear readable code with some?
or using set
?
case appears to be much faster:
user=> (time (dotimes [_ 10000000] (one-of :bar [:foo :bar])))
"Elapsed time: 221.057525 msecs"
nil
user=> (time (dotimes [_ 10000000] (case :foo (:foo :bar) true false)))
"Elapsed time: 61.048416 msecs"
For my project, some modules are loaded from ~/.m2
, and some from /var/maven
. How can I control the exact location (e.g. force all to load from ~/.m2
)?
/var/maven is a very non-standard thing, so either it is some setting in some tool you are using, or some non-standard packaging of some tool you are usig
your maven config should be controlling where it looks for local archives
Is the clojure syntax indescernable from edn, or is edn specifically used in the context of serialization?
edn is a string format
you can essentially change a clj file to edn and it's still valid?
or does edn have some declaration standards?
Hello all, how can I translate this to clojure?
table.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (event.isPrimaryButtonDown() && event.getClickCount() == 2) {
System.out.println(table.getSelectionModel().getSelectedItem());
}
}
});
I suspect the reason you’re not seeing a lot of answers to this question is that it is a non-specific question about some code with no context or explanation. If you could be more clear about what specific parts you are having trouble with, it would be easier to help. After a bit of research, it looks like you are using JavaFX, which I never have, and I don’t know if anyone else on this channel has either. Because of that, I am not going to be able to put together a test environment to really try out my answer, which means it will likely contain mistakes. But with that caveat, and assuming that you are asking how to express that code using Clojure Java interop syntax, it would be something like this…
First, I am assuming you have your table object in a Clojure local table
.
(.setOnMousePressed table
(proxy [javax.event.EventHandler] []
(handle [event]
(when (and (.isPrimaryButtonDown event)
(= (.getClickCount event) 2))
(println (.. table getSelectionModel getSelectedItem))))))
That’s not the indentation/line breaks I would really use but I adjusted it to be more readable in the narrow thread window.
To more closely parallel your Java code (and avoid some reflection), you could type hint the event
parameter, which would look like (handle [^javafx.scene.input.MouseEvent event]
…)
And you could shorten this code by importing the EventHandler
and MouseEvent
classes in your Clojure namespace so you would not have to fully-qualify them in your code.
@theeternalpulse I'm pretty sure there's things in clojure that the edn standard doesn't' accept
haha, just as I was about to paste this
user=> (clojure.edn/read-string "#()")
RuntimeException No dispatch macro for: ( clojure.lang.Util.runtimeException (Util.java:221)
user=> (clojure.edn/read-string "`(foo ~bar)")
RuntimeException Invalid leading character: ` clojure.lang.Util.runtimeException (Util.java:221)
interop seems fine
user=> (clojure.edn/read-string "(java.util.Date.)")
(java.util.Date.)
user=> (clojure.edn/read-string "Math/PI")
Math/PI
user=> (clojure.edn/read-string "(. Math PI)")
(. Math PI)
another big one:
user=> (clojure.edn/read-string "::foo")
RuntimeException Invalid token: ::foo clojure.lang.Util.runtimeException (Util.java:221)
edn can't have a current namespace, so auto-namespaced symbols don't make sense
it does, just not a "current namespace"
user=> (clojure.edn/read-string ":foo/bar")
:foo/bar
neat info, I wasn't sure, I was watching an older rich video and he ploped some clojure code and showed it as edn, so I wasn't sure if there was some connection outside of serialization to the language
it's intentional that everything that is valid edn (or nearly so) is valid in clojure source (as syntax or literal)
but it doesn't extend to everything in the other direction
on the other hand, metadata (and the ^
metadata shorthand) does work on edn, and transfers to the read value
user=> (clojure.edn/read-string "^:foo []")
[]
user=> (meta *1)
{:foo true}
Clojure reader accepts more syntax than EDN but almost all of that is just syntax sugar and after read becomes data that can be put in EDN (excluding #=()
). Rich Hickey mentioned this in one of his talks AFAIR, emphasising the fact that eval
deals in data structures and not text, and the LISPs give users the ability to extend the language by participating in this eval
right after reading.
Clojure for Java Programmers part 1 if memory serves me right.
I ran into a weird issue just now where I was wrapping with-redefs
in a fn
. When I tried to execute the function, the with-redefs
would fail for a function that I was rebinding but succeed for an atom that I was rebinding. After relocating with-redefs
so that it wrapped the fn
instead, everything works.
I'm actually struggling to put this into a toy example, for whatever reason.
Clearly there is something going on I don't understand. Just wondering if there is any obvious "gotcha" that I ran into.
was the fn called by something lazy that might escape the context of the with-redefs form?
i don't think it would be evaluated lazily. here's what was calling fn
:
(defn with-props
"Temporarily rebinds props and calls `f`"
[props-map f]
(let [old-props (atom {})]
(doseq [[k v] props-map]
(swap! old-props assoc k (.getProperty props/properties k))
(.setProperty props/properties k v))
(f)
(doseq [[k v] @old-props]
(.setProperty props/properties k v))))
and where is the with-redef? - oh it's inside f
yeah, my question was if any of (do-stuff) might be lazy, or otherwise execute after with-redefs exits
(eg. if it uses other threads)
was writing to a channel, and the consumer of the channel had the var i was trying to redef
also - with-redefs is a really blunt tool - it's redefining those vars in all your other threads!
if two threads both use with-redefs, you have a race condition that could lose the original pre-redef values
a (grabs bindings) a (stashes bindings) a (sets bindings) b (grabs bindings) a (resets bindings) b (stashes bindings) b (sets bindings) b ("resets" bindings)
that last step actually sets the bindings from a, not the original bindings
I wouldn't use with-redefs outside tests, and even then I'd be sceptical
(that is, if your tests use threads at all it's still likely to cause problems)
where can I find in which places in defn
or fn
metadata can be expected? on the fn name, on arguments… where else?
at the end of the function body for multi-arity
user=> (defn foo ([]) {:extra true})
#'user/foo
user=> (-> #'foo meta :extra)
true
since there's no single argument list to put the meta on
err.. but a hash-map before the function bodies also works
user=> (defn bar {:also :OK} ([]) {:extra true})
#'user/bar
user=> (-> #'bar meta :also)
:OK
user=> (-> #'bar meta :extra)
true
so those are the normal hashmaps as metadata, but in which positions can people put metadata? (defn ^String bar ...)
, (defn bar ^String ([] ...)
? I’d like to have a comprehensive overview
Like, does it make sense https://github.com/clojure/clojure/blob/e72ffdcd039189e3fe230d160e093ae6ba8f24cf/src/clj/clojure/core.clj#L2680 ?
@lucelios "is every member of an empty set a vector?" - this is considered true in classical logic