What's the main difference between (seq x)
and (not-empty x)
? They seem to be interchangeable in all of the places I use them (mainly for testing if a sequence is/isn't empty/nil
Try (seq [1 2 3])
and (not-empty [1 2 3])
The former will give you a sequence, the latter will give you a vector.
oh ok. So for my usual purpose of seeing if it is/isn't empty/nil, then they are basically the same. But I can imagine when this difference would matter
not-empty
is useful for when you either want nil
or the original data structure.
usually, i'm just using it as a test, so I don't really care. but I see your point
For example (let [foo (not-empty "Hello")] foo)
gives you a string, but (let [foo (seq "Hello")] foo)
gives you a sequence of characters
(cond-> ... (not-empty x) (do-with-x))
Thanks!
I would use seq
there -- you're only testing x
how come?
idiomatic? I feel like not-empty
reads more naturally for me
Definitely not idiomatic.
ok
thanks
I use it in situations like (let [field (some-> req :params :field str/trim not-empty)] (when field ... we know is it a non-empty string at this point ...))
How can I get the value of a private static field in a Java class?
Googling only returns solutions for accessing static fields or private instance fields, but not private static fields on classes.
@simongray You can use reflection and change the access to public, I think.
But I guess I would ask: why are you trying to do that?
There’s a bunch of Java initialisation code for a singleton instance that I need to reuse, but it’s hidden away behind a private static field.
Rather than duplicate the initialisation code in Clojure to make another singleton, I thought it would be easier to access the value directly.
Reflection is your friend then, I suspect...
Yeah, not having much luck with clojure.reflect/reflect. It seems to not work for classes.
Sorry, no, I meant raw Java reflection.
This may help
Thanks both you. I ended up just recreating the Java code myself using Clojure. It ended up being 7 lines of code + a few java class imports, so not too bad.
np 🙂
Regarding the namespaced / auto-resolved keyword reading discussion we had recently, here's another interesting .cljc case:
(ns repro
#?(:cljs
(:require
[clojure.string :as str])))
#?(:cljs
(defn foo []
{::str/test "This will break it."}))
I guess that was already mentioned by lilactown
Hello people! Is there a more elegant way of doing this:
(if true
:something
(eval (do (require '[some.ns.mock :refer [mock]])
'(mock))))
(requiring-resolve 'clojure.test/testing)
i think would fit the bill
*p.s: i'm avoiding doing this require on ns declaration at the beginning of the file because i only want to compile the mock ns class when i really need it
if i do
(if true
:something
(do (require '[some.ns.mock :refer [mock]])
(mock)))
it won't compile because it says that there's no reference to mock
how i eval the function then?
check the docstring. that returns the function
i mean, the result of that is a clojure.lang.Var
how to use that in order to execute the function that this var is pointing to
((requiring-resolve 'clojure.core/+))
returns 0 as it is calling clojure.core/+
oh, i see
if you know the namespace containing that var has already been loaded, then a simple resolve
also works
amazing
thanks you both 😄
(if true
:something
((requiring-resolve 'some.ns.mock/mock)))
eval is gone 😄the require
happens at runtime, but you're using (mock)
at compile time
Sounds like the mock should be injected.
The question has been answered in a thread
I'm using VSCode to edit an existing Clojure project, using the Calva plug-in. When I try to jack in, lein compiles the project, giving me the following error "java.lang.IllegalArgumentException: Cannot open <nil> as a Reader., compiling:(core.clj:84:32)" on this definition: (def teams-json (json/read-str (slurp (resources-path)) :key-fn keyword)) The problem seems to be in the slurp call. The function resource-path returns a string. When this function returns a static string, the compilation seems to succeed, but if there's anything else more complex (like using str to tack a directory name on the front of a resource filepath) the compiler throws this error. I checked online and there were a few references to issues with Clojure and/or Java and or lein being out of date, but I think that everything that's running is up-to-date. Does anyone have any ideas as to what's happening with the compilation?
what is (resources-path)
? the exception indicates that it's returning nil
(resources-path) is not run, as far as I know. This is a compiler error, not a runtime error.
(let [path (resources-path)]
(prn "resource path: " path)
(def teams-json (json/read-str (slurp path) :key-fn keyword)))
Sure it is. That def must be established when loading the namespace
i'm pretty sure resources-path
is being run
> (slurp nil)
Execution error (IllegalArgumentException) at components/eval28033 (form-init9329007192917549923.clj:572).
Cannot open <nil> as a Reader
Yes it is. I was blind after staring at this for the last two hours. I figured it out. It's being run at compilation time, and the context for (resources-path) isn't initialized yet. Thanks for your help.
This indicates an incorrect understanding of how clojure compiles and runs code
Compilation is the same as running, the compiler just saves the byte code it generates to disk instead of loading it into the jvm from memory
So anything like (def x (f))
will execute f while being compiled
(assuming you are referring to aot compilation as compiling, which is not great short hand, because clojure is always compiled)