Fork me on GitHub
#beginners
<
2022-08-05
>
quan xing11:08:02

m1 {:a 1 :c 2 :d 3} m2 {:a 2 :c 2 :d 3} I want to get the right of the different value map in two maps {:a2} Is there any embed function to find it in the Clojure?

Martin Půda12:08:24

(clojure.set/difference (set {:a 2 :c 2 :d 3})
                        (set {:a 1 :c 2 :d 3}))
                        
=> #{[:a 2]}

lsenjov12:08:08

I might break down the reasoning a little here. Maps are made of MapEntrys, which are key-val pairs. Using set or anything that coerces a map into a collection returns a sequence of key-val pairs. In clojure, equality on structures is automatically calculated (anonymous functions excepted). So clojure.set/difference finds any elements of m2 that don't appear in m1. The MapEntry [:a 2] doesn't appear in m1.

flowthing12:08:00

Alternatively, clojure.data/diff.

Ben Sless13:08:35

@U24QP2D4J in Clojure the maps are flattened and the entries are created only if you iterate on them as a sequence, on demand, to avoid indirection

👍 1
Joel16:08:12

Is there a way to tap> clojure tests output, to say make it go to portal for viewing diffs?

dpsutton16:08:01

Should be a way: • some https://clojure.github.io/clojure/clojure.test-api.html for clojure.test • the default version of https://github.com/clojure/clojure/blob/4b9eadccce2aaf97e64bcc8e35c05c529df8fdd2/src/clj/clojure/test.clj#L375 So I think you could do something like

(defmethod report :fail [m]
  (tap> {:expected (:expected m)
         :actual (:actual m)})
  (with-test-out
    (inc-report-counter :fail)
    (println "\nFAIL in" (testing-vars-str m))
    (when (seq *testing-contexts*) (println (testing-contexts-str)))
    (when-let [message (:message m)] (println message))
    (println "expected:" (pr-str (:expected m)))
    (println "  actual:" (pr-str (:actual m)))))

1
R.A. Porter16:08:35

As you've specifically mentioned Portal, you can follow this guide - https://github.com/djblue/portal/blob/master/doc/guides/test-runner.md

Joel16:08:23

i figured someone did this, but wasn't finding it. thanks.

ghadi16:08:11

@joel380 if you want a generic hook, look at the dynamic var clojure.test/report

ghadi16:08:21

and read the docstring of the clojure.test namespace

ghadi16:08:40

you can use that to intercept and tap>

Ben Lieberman20:08:56

I have some stubbed Java code that I pointed to in my project.clj by using the :java-source-paths key. I was able to successfully compile this code and it is now in the target folder as I expect. But it was not added to my classpath. I expected this to occur bc the docstring for lein javac says it is like using the javac -cp command. What did I do wrong? And is it enough to manually concatenate the directory location of the .class files onto my classpath, or is there a better way?

hiredman20:08:19

hard to say. what do you mean by "it was not added to my classpath"

Ben Lieberman20:08:40

Calling lein classpath shows that the dependencies from Maven were added. But not the Java code from :java-source-paths. I get a ClassNotFoundException when I try to import any of the code from the files

hiredman20:08:31

you don't need java source on the classpath

hiredman20:08:52

so there is no reason for :java-source-paths to be part of the classpath

hiredman20:08:47

a wild guess, what package is your java code in?

Ben Lieberman20:08:01

What about the resulting .class files though? I know very little about Java unfortunately but I was under the impression that in order for the class loader to find any compiled code it would have to be in the classpath

hiredman20:08:52

the files themselves don't go on the classpath, the files go in a directory tree that matches their package structure, and the root of that directory tree goes on the classpath

hiredman20:08:46

if the java code doesn't explicitly declare a package, it goes in the default package, which, uh, I don't entirely recall because it doesn't come up often, but it can cause issues. and if you are inexperienced with java you might leave out a package declaration, which might land you here, but that is me wildly jumping ahead of things

Ben Lieberman20:08:16

the package is com.ibm.mq.demo but I tried [:import com.ibm.mq.demo SessionBuilder] in my ns and that still threw a ClassNotFoundException

hiredman20:08:36

is the classnotfoundexception for SessionBuilder, or for some class SessionBuilder depends on?

Ben Lieberman20:08:46

it's for com.ibm.mq.demo

Ben Lieberman20:08:20

all the .class files are in my project root /target

hiredman20:08:28

oh, you are just typing [:import com.ibm.mq.demo SessionBuilder] into the repl?

hiredman20:08:39

yeah, that for sure doesn't work

Ben Lieberman20:08:49

no in my core.clj and then doing cider-eval-buffer

hiredman20:08:19

you need some more () or []

Ben Lieberman20:08:50

so it should be (ns blah [:import thing [SessionBuilder]])

hiredman20:08:54

that is being interpretered as a class with the full name com.ibm.mq.demo and a class with the full name SessionBuilder

hiredman20:08:23

(package.name class.name ...) where the ... is potentially more classes in the same package

Ben Lieberman20:08:25

got it working, thanks! it ended up being .demo.SessionBuilder

hiredman21:08:37

user=> (import 'java.util.concurrent.LinkedBlockingQueue)
java.util.concurrent.LinkedBlockingQueue
user=> LinkedBlockingQueue
java.util.concurrent.LinkedBlockingQueue
user=> (import '(java.util.concurrent LinkedBlockingQueue ArrayBlockingQueue))
java.util.concurrent.ArrayBlockingQueue
user=> LinkedBlockingQueue
java.util.concurrent.LinkedBlockingQueue
user=> ArrayBlockingQueue
java.util.concurrent.ArrayBlockingQueue
user=>

👌 1
sheluchin20:08:18

Is it accurate to describe all relationships between dependencies ->> dependants as a library -> client relationships, or is there some nuance to it? Are there some deps that aren't libraries? Are some users of dependencies not clients? Is there better jargon to use here?