Fork me on GitHub
#clojure
<
2020-09-17
>
ryan echternacht01:09:34

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

seancorfield01:09:38

Try (seq [1 2 3]) and (not-empty [1 2 3])

seancorfield01:09:52

The former will give you a sequence, the latter will give you a vector.

ryan echternacht01:09:36

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

seancorfield01:09:11

not-empty is useful for when you either want nil or the original data structure.

ryan echternacht01:09:41

usually, i'm just using it as a test, so I don't really care. but I see your point

seancorfield01:09:47

For example (let [foo (not-empty "Hello")] foo) gives you a string, but (let [foo (seq "Hello")] foo) gives you a sequence of characters

ryan echternacht01:09:12

(cond-> ... (not-empty x) (do-with-x))

seancorfield01:09:44

I would use seq there -- you're only testing x

ryan echternacht01:09:40

idiomatic? I feel like not-empty reads more naturally for me

seancorfield01:09:54

Definitely not idiomatic.

seancorfield01:09:22

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 ...))

simongray08:09:06

How can I get the value of a private static field in a Java class?

simongray08:09:44

Googling only returns solutions for accessing static fields or private instance fields, but not private static fields on classes.

seancorfield08:09:11

@simongray You can use reflection and change the access to public, I think.

seancorfield08:09:26

But I guess I would ask: why are you trying to do that?

simongray08:09:28

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.

simongray08:09:33

Rather than duplicate the initialisation code in Clojure to make another singleton, I thought it would be easier to access the value directly.

seancorfield08:09:15

Reflection is your friend then, I suspect...

simongray08:09:31

Yeah, not having much luck with clojure.reflect/reflect. It seems to not work for classes.

seancorfield08:09:03

Sorry, no, I meant raw Java reflection.

dharrigan09:09:38

This may help

simongray09:09:50

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.

borkdude10:09:08

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."}))

borkdude12:09:43

I guess that was already mentioned by lilactown

MatthewLisp14:09:47

Hello people! Is there a more elegant way of doing this:

(if true
  :something
  (eval (do (require '[some.ns.mock :refer [mock]])
            '(mock))))

dpsutton14:09:17

(requiring-resolve 'clojure.test/testing) i think would fit the bill

👍 3
MatthewLisp14:09:29

how i eval the function then?

dpsutton14:09:47

check the docstring. that returns the function

MatthewLisp14:09:18

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

dpsutton14:09:56

((requiring-resolve 'clojure.core/+)) returns 0 as it is calling clojure.core/+

borkdude14:09:11

if you know the namespace containing that var has already been loaded, then a simple resolve also works

MatthewLisp14:09:08

thanks you both 😄

MatthewLisp14:09:14

(if true
  :something
  ((requiring-resolve 'some.ns.mock/mock)))
eval is gone 😄

MatthewLisp14:09:32

*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

MatthewLisp14:09:07

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

ghadi15:09:37

the require happens at runtime, but you're using (mock) at compile time

Joe Lane15:09:39

Sounds like the mock should be injected.

borkdude15:09:00

The question has been answered in a thread

💯 6
fadrian23:09:24

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?

phronmophobic23:09:21

what is (resources-path) ? the exception indicates that it's returning nil

fadrian23:09:09

(resources-path) is not run, as far as I know. This is a compiler error, not a runtime error.

hiredman23:09:18

This indicates an incorrect understanding of how clojure compiles and runs code

hiredman23:09:08

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

hiredman23:09:54

So anything like (def x (f)) will execute f while being compiled

hiredman23:09:43

(assuming you are referring to aot compilation as compiling, which is not great short hand, because clojure is always compiled)

phronmophobic23:09:26

(let [path (resources-path)]
   (prn "resource path: " path)
   (def teams-json (json/read-str (slurp path) :key-fn keyword)))

dpsutton23:09:53

Sure it is. That def must be established when loading the namespace

phronmophobic23:09:06

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

fadrian23:09:16

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.