Fork me on GitHub
#beginners
<
2022-09-19
>
zeddan08:09:02

Using Calva and VSCode, is it possible to require things in the REPL? I’ve tried (:require my-projet.utils :as utils) but I get ; Syntax error (ClassNotFoundException)

Bart Kleijngeld08:09:57

Try: (require '[my-project.utils :as utils])

zeddan08:09:19

Ah that’s great, thanks

👍 1
Bart Kleijngeld08:09:57

The :require keyword is part of the ns declaration, that's what probably got you confused. I recommend reading up a little about namespaces and the REPL basics 🙂

pez09:09:02

FYI, there is nothing special about Calva's REPL. I know it can seem like that and you don't know how all these things compose. But regard Calva's REPL as if it is a regular Clojure REPL. That mental model will take you far. And when you are ready to challenge that model, then, well, then you will be ready. 😃

👍 1
zeddan09:09:17

Check 🙂 Won’t include that info in the future then

pez09:09:25

Oh, it is often worth including. There are some side effects to how the REPL is wired that can be involved in a particular problem. But it is also good to know that the regular Clojure reality applies. Calva connects to an nREPL server running in the same process as your application and the Clojure REPL. The nREPL server takes requests from Calva and uses the Clojure REPL to evaluate, then packages the results in a response and gives it to Calva. Meaning that the forms you evaluate using Calva will be evaluated by the Clojure REPL. Meaning that a syntax error like the one in the thread start, comes directly from the horse's mouth.

zeddan10:09:48

I see, thanks for the info 🙂

pez10:09:36

You are welcome 😃 And welcome to #calva !

sheluchin10:09:29

I should avoid putting anything in deps.edn's :extra-paths that isn't meant to be evaluated?

pavlosmelissinos10:09:04

Can you give an example?

sheluchin10:09:11

Say I have some clojure files which I don't want to run, but just want to count the number of lines in them. Is it safe/make any sense to put those in :extra-paths?

Alex Miller (Clojure team)12:09:11

You don't need to put them in :extra-paths to read them

Alex Miller (Clojure team)12:09:36

But doing so will not automatically cause them to be evaluated

Alex Miller (Clojure team)12:09:38

There are some tools (like tools.namespace) that may load all of the namespaces on the classpath, but whether that affects you depends on tooling

sheluchin12:09:26

I found that if I include a particular Clojure repo in :extra-paths, unit tests which throw an exception stop working in my particular tooling setup :thinking_face:

Alex Miller (Clojure team)12:09:16

Hard to explain without more info. I have seen someone here that ran into a similar issue due to use of tools.namespace but don't remember the details

1
pavlosmelissinos12:09:03

Yes, it would be easier to help you if we had a reproducible case.

sheluchin12:09:10

I can't reproduce in the REPL so I'm guessing it's got something to do with how my editor runs the tests. Thanks for your input @U064X3EF3 and @UEQPKG7HQ.

👍 1
sheluchin13:09:00

I created an issue about it in the cider-nrepl repo and apparently in this case it does matter that there is "correct code" on the classpath. Just wanted to leave the link/details here in case anyone stumbles across it in the future. https://github.com/clojure-emacs/cider-nrepl/issues/760

🙏 1
Joseph Graham11:09:19

Hi all. Trying to optimise this with threading but I can't because a string is not a function: (Integer/parseInt ((request :query-params) "id")) Tried optimizing it to this: (-> request :query-params "id" Integer/parseInt)

Bart Kleijngeld11:09:43

Before jumping in how to go about it, I would ask myself, critically, if a threading version would really be more readable. I doubt that personally. Can you explain what you dislike about your initial expression, and why you like to use threading here?

Mario Trost11:09:52

(macroexpand '(-> request
    :query-params
    "id"
    Integer/parseInt))

(. Integer parseInt ("id" (:query-params request)))

Joseph Graham11:09:24

Thanks Mario that shows why it doesn't work

simongray11:09:10

Use parentheses. The trick with leaving them out only works for a few built-in Clojure data literals since they implement IFn, i.e. not strings and Java methods.

👍 1
Mario Trost11:09:24

What @U03BYUDJLJF said 👆 If it is just for an exercise, I like macroexpand to show me what the threading macro is doing.

👍 1
Joseph Graham11:09:02

Alright thanks all.

pavlosmelissinos12:09:33

As of clojure 1.11 you can use parse-long instead of Integer/parseInt, which does behave better in a threading macro.

(some-> request
        :query-params
        (get "id")
        parse-long)
This ☝️ looks perfectly ok to me and I like it better than (parse-long ((:query-params request) "id")) It tells you how to get the parameter "id" by listing the steps you have to do, in order, and doesn't throw an exception if query-params doesn't contain the key "id".

😃 1
🙏 1
👍 1
Joseph Graham13:09:23

wow, thanks. I like it!

zeddan13:09:46

I want to replicate this behavior (ruby) @data.xpath("//E1EDP01[not(ABGRU) or ABGRU = '99'][not(PSTYV = 'TATX')]") in clojure. So far I have (xml-> xml :IDOC :E1EDP01) but I’m unsure how to do the not statements. Does anyone know?

zeddan14:09:21

It’s taken from [clojure.data.zip.xml :refer [attr text xml-> xml1->]]

pavlosmelissinos14:09:16

Oh, ok, I'm not familiar with the library but maybe you can use (complement text=)

zeddan14:09:45

Ok so this returns rows that has the ABGRU tag (xml-> xml :IDOC :E1EDP01 [(tag= :ABGRU)])

zeddan14:09:55

Now I just need to negate that

zeddan14:09:07

I tried using complement, but I can’t get it to work

pavlosmelissinos14:09:19

(tag= :ABGRU) returns a function

pavlosmelissinos14:09:47

are you sure (complement (tag= :ABGRU)) doesn't work?

pavlosmelissinos14:09:22

this: (xml-> xml :IDOC :E1EDP01 [(complement (tag= :ABGRU))])

zeddan14:09:44

I have two E1EDP01 blocks, one that includes the ABGRU tag and one that doesn’t. Sorry to say but the last piece doesn’t seem to yield the block without the tag, it returns an empty array

pavlosmelissinos14:09:02

I don't know what's going on and this problem seems specific to that library, so maybe check out #xml .

zeddan14:09:41

I’ll try there, thanks anyway for your time 🙂

pavlosmelissinos17:09:57

Did you find a solution after all, @U033Q6RBXJ6?

zeddan07:09:12

Yes:

(xml-> xml :IDOC :E1EDP01
                    (fn [loc]
                      (and (or
                            (some #(and (= :ABGRU (:tag (zip/node %)))
                                        (= "99" (text %)))
                                  (clojure.data.zip/children loc))
                            (not-any? #(= :ABGRU (:tag (zip/node %)))
                                      (clojure.data.zip/children loc)))
                           (not-any? #(and (= :PSTYV (:tag (zip/node %)))
                                           (= "TATX" (text %)))
                                     (clojure.data.zip/children loc)))))

👍 1
pavlosmelissinos07:09:02

that's good to hear 🙂

Jim Strieter22:09:03

When using tools.cli, how do you tell Clojure that a parameter is supposed to be a string? My cli-options vector has this:

(def cli-options

seancorfield22:09:14

You need "--id-str ID" -- you have to provide a named placeholder for value-based options. See the example in the Quick Start https://github.com/clojure/tools.cli#quick-start where -p/--port is a value option and -v/-h are flag options.