Fork me on GitHub
#beginners
<
2021-05-07
>
seancorfield00:05:21

(if (seq cols) (apply map + cols) <whatever other value you want>)

seancorfield00:05:39

Or throw an exception perhaps if it “shouldn’t happen”.

zackteo00:05:18

Okay! Thank you! 😄

seancorfield00:05:54

If you want an exception and you don’t care about getting a lazy seq of sums, you could just (apply mapv + cols) — it will throw an arity error if cols is empty.

zackteo00:05:24

Also by any chance do you have example projects using clojure.tools.cli ? Am trying to use it but another reference might help

seancorfield00:05:16

I don’t know what will be more useful than the examples in the README but clj-new still uses it (even though the old -m command-line is no longer documented). See https://github.com/seancorfield/clj-new/blob/develop/src/clj_new/helpers.clj#L264 for the opts definitions and https://github.com/seancorfield/clj-new/blob/develop/src/clj_new/helpers.clj#L381 for the point of use.

seancorfield00:05:41

(although that uses :assoc-fn in places where I’d use :update-fn now)

seancorfield00:05:30

See this example from tools.cli’s README for what I mean about that:

["-f" "--file NAME" "File names to read"
    :multi true ; use :update-fn to combine multiple instance of -f/--file
    :default []
    ;; with :multi true, the :update-fn is passed both the existing parsed
    ;; value(s) and the new parsed value from each option
    :update-fn conj]

marciol00:05:36

Hi, I'm trying to apply the bean function over java.net.URL but I'm getting this error:

(bean (java.net.URL. ""))
Error printing return value (ConnectException) at java.net.AbstractPlainSocketImpl/doConnect (AbstractPlainSocketImpl.java:399).
Connection refused (Connection refused)
Anyone can say why this is happening?

dpsutton00:05:05

(bean (.URL. "")) shows it has :content on it. If you look at the error thrown by your function you'll see [java.net.URL getContent "URL.java" 1177] in it. And you presumably don't have a webserver running locally on port 80

marciol00:05:39

hmmmm, thank you @dpsutton

dpsutton00:05:56

(slurp (.getContent (.URL. ""))) is the contents of the resource

marciol00:05:35

Yes, the bean function walks over every getter and returns a map.

marciol00:05:00

This is why I'm getting this error. Thank you again @dpsutton

zackteo00:05:50

I think i'm generally just a bit confused about how to package my project into a binary (?) or rather I need to read up more cause I have been pretty much only been using lein run

zackteo00:05:24

@seancorfield I think me asking this would help. Is

my-program -vvvp8080 foo --help --invalid-opt
the equivalent of running the following in the repl ?
(-main "-vvvp8080 foo --help --invalid-opt")

zackteo00:05:46

This is from the quickstart example. I've been particularly confused about how to get that working

seancorfield00:05:41

They should be separate strings.

seancorfield00:05:10

(-main "-vvvp8080" "foo" "--help" "--invalid-opt")

seancorfield00:05:29

-main expects to be called with a sequence of zero or more strings

zackteo00:05:50

Right right :face_palm: i recall a similar confusion I had when working with clojure.java.shell/sh

zackteo00:05:12

Thank you!

Rob Haisfield01:05:42

Just describing my dog in Clojure. Not totally sure if I’m doing this right lol. One area is I want to also describe his reaction to the :triggers but not sure what to do there

zackteo01:05:52

@rob370 seems fine ? Are you perhaps thinking of the triggers being functions?

zackteo01:05:03

Is there a way to print out a table when you do lein run ? I know there is clojure.pprint/print-table but is there a direct that to cli output?

zackteo02:05:32

nvm I just realised it is because I was getting a clojure not found error from not requiring the namespace

zackteo03:05:18

How do I compile a leiningen project? Like I can do lein run to use my program. And I understand I can do lein uberjar but have a feeling that's not what I want exactly?

hiredman03:05:41

What do you want?

zackteo03:05:46

I think I want a simple way to allow someone to package my repo and run it as an executable. I think https://github.com/BrunoBonacci/lein-binplus comes close

zackteo03:05:10

Oh i think I can just include it in my project.clj hmm

hiredman03:05:22

It depends on your audience then, I usually take an uberjar to mean that, but if your audience is less technical then you will need something additional like that lein plugin

zackteo03:05:42

How do you run an uberjar actually?

solf03:05:33

java -jar uberjar.jar

hiredman03:05:03

That depends too

hiredman03:05:44

You can build jar files that launch like that

hiredman03:05:24

But java only knows how to run jvm bytecode, so you either have to hand it jvm bytecode (by aot compiling clojure) or bootstrap (run some bytecode that knows how to load clojure)

zackteo03:05:27

Hmmmm, will have to dig deeper into that but lein bin works well for now java -jar worked too for the standalone.jar that was produced

Sivaram Konanki10:05:04

what is the meaning of :* in Clojure?

delaguardo10:05:26

it is a normal keyword, nothing special about it

👍 1
practicalli_john07:05:02
replied to a thread:

There is great value in running tests via the REPL and externally via the command line (possibly by a watcher). I run selective tests (specific namespace, individual deftest) predominantly in the REPL during development and run all tests (including integration tests) via the command line before pushing commits (or if I get unexpected errors or longer running test suite) I run tests within the REPL using a connected editor. This avoids context switching to an external tool, although does require changes to be evaluated before they can be tested. You must manually ensure the REPL is in sync with the saved source code before committing changes. I use Kaocha test runner to run tests via the command line, which always tests code saved to disk, so uses a clearly defined state. This clearly defined state is especially valuable for integration tests. The CLI approach is also more robust for longer running tests than running within an editor. Running tests on the should be able to emulate the test step of a CI/CD service, providing a means to debug issues with such a service.