clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
msolli 2021-05-21T06:39:54.133900Z

clj-http uses the Apache HttpComponent client, which can log all outgoing calls using log4j2. See https://github.com/dakrone/clj-http#logging. Using Logback, to enable logging of outgoing requests add this to logback.xml: <logger name="org.apache.http.wire" level="debug"/>

msolli 2021-05-21T06:42:08.134200Z

You can even change it runtime:

(.. (org.slf4j.LoggerFactory/getLogger "org.apache.http.wire")
    (setLevel (ch.qos.logback.classic.Level/DEBUG)))

simongray 2021-05-21T11:34:34.137Z

If I have both a my-ns.clj and a my-ns.cljc is it possible to refer the functions from the cljc namespace or am I forced to give it some other name?

simongray 2021-05-21T11:35:40.137700Z

The reason being that most of the code is CLJ code, so I would like to avoid indenting my entire CLJC file by having it in its own CLJ file.

simongray 2021-05-21T11:35:59.137900Z

With only the shared code in the CLJC file.

borkdude 2021-05-21T11:38:02.139600Z

if you are using it from clojure, there is no way to get to the .cljc file as .clj is prioritized

simongray 2021-05-21T11:38:11.139900Z

ok, sucks

2021-05-21T11:38:24.140200Z

you have to give it another name. clojure is looking for foo.clj or foo.cljc when you require namespace declared in foo if you have both - I think only first will be loaded, but this is not stated anywhere in docs

simongray 2021-05-21T11:39:00.140900Z

ok - thanks for answering. I’ll move stuff around and try to come up with some new namespace name… sigh

borkdude 2021-05-21T11:39:44.141300Z

you can rename the other one to _common.cljc and then load that from your clojure file

simongray 2021-05-21T11:41:11.142200Z

I guess, but I just wanted the experience of CLJC. I guess I can just make it CLJC and live with crappy indentation everywhere.

markus 2021-05-21T12:18:45.145900Z

Hello! Has anyone here packaged a Clojure project with both jlink and jpackage to produce a slimmed down deliverable for end users? I have an existing Java project that uses modules and is packaged with jpackage and I would like to add Clojure to it. But all information I can find online are basically leiningen plugins that run jlink on Clojure only projects. Not sure how to translate that to my situation.

2021-05-21T13:35:54.148100Z

does jlink do tree shaking (automatic removal of unused classes) ? because that tends to not play nicely with clojure

2021-05-21T13:37:37.149700Z

TBH if you already know how to use jlink / jpackage you might just want to use those tools directly and define clojure.main (or if you absolutely need to, your own gen-class / aot ns) as the entry point. clojure is a java library and you can use java tooling.

2021-05-21T13:38:32.150300Z

"clojure only" just means "clojure is my only java lib outside the jre itself"

alexmiller 2021-05-21T13:42:55.151100Z

afaik you can use jlink and jpackage with Clojure - just AOT, then you've got bytecode, do whatever you're supposed to with jlink and jpackage

borkdude 2021-05-21T16:22:02.156800Z

TIL:

(ns repro
  (:import
   (java.time format.DateTimeFormatter LocalDateTime)))
Does anyone actually else use import like this?

borkdude 2021-05-21T16:22:11.157Z

(it was a bug filed against clj-kondo)

2021-05-21T16:24:38.157600Z

You mean using ( instead of [?

2021-05-21T16:25:23.158200Z

then I’m using import like that )

borkdude 2021-05-21T16:25:35.158700Z

No, importing DateTimeFormatter like this

alexmiller 2021-05-21T16:25:36.158800Z

I assume he means the nesting, not that

☝️ 1
alexmiller 2021-05-21T16:26:46.160200Z

I don't see any support in the docstring that that is something you can do

borkdude 2021-05-21T16:27:27.160800Z

oh, that would be good news, because I find it pretty confusing :)

borkdude 2021-05-21T16:27:31.161Z

the (clojure.)spec does tolerate it

alexmiller 2021-05-21T16:28:05.161600Z

I think this is about semantics, not sure the spec could cover this

borkdude 2021-05-21T16:28:40.162Z

I think the docstring suggests it isn't supported: package-symbol class-name-symbols*

borkdude 2021-05-21T16:28:43.162200Z

so I could just close it like that

alexmiller 2021-05-21T16:29:14.162600Z

I think the fact that it does something useful is more likely by accident than intent

borkdude 2021-05-21T16:31:27.163Z

closed it: https://github.com/clj-kondo/clj-kondo/issues/1283

seancorfield 2021-05-21T16:47:29.165300Z

We were using that structure for imports in a couple of places in our (older) code at work but I think clj-kondo flagged the imported classes as unknown and when I looked at the :import I was like “WAT?!” for a few seconds, surprised that it was actually legal in Clojure! I thought it was a horrible way to structure an :import so I rewrote it to the “expected” structure — which satisfied clj-kondo (and my OCD).

👍 3
2021-05-21T17:08:09.167400Z

I'm the one who raised that ticket on kondo, I've seen this structure in a few places, but I don't remember where I saw it first. I know it's been incredibly convenient for wrapper libraries, like the previously shown java.time, or in my experience the org.lwjgl namespaces.

2021-05-21T17:10:01.168200Z

As for the square brackets vs parens, I always use parens for imports because the first element of the sequence is in a "priviliged" position, in that it is a package, and the other elements are classes.

1
seancorfield 2021-05-21T17:22:11.169600Z

https://stuartsierra.com/2016/clojure-how-to-ns.html recommends [..`]` for :require and (..`)` for :import and I prefer that style but I know there are plenty of folks who prefer [..`]` for both.

borkdude 2021-05-21T17:27:56.170500Z

@suskeyhose Nesting is supported for namespaces (but this style is also discouraged by how to ns, but clj-kondo does recognize the style)

looprecur 2021-05-21T20:39:48.172700Z

Hi All, I am using the JDBC library for Clojure with Postgres. I have some SQL that aggregates an array as a column value. On return I get the following error: java.lang.Exception: Not supported: class org.postgresql.jdbc.PgArray . What is a way around this?

ghadi 2021-05-21T20:47:54.172900Z

pretty sure rich disagrees with that, uses [] for both

seancorfield 2021-05-21T20:51:10.174100Z

@looprecur Which JDBC library are you using? The next.jdbc docs have a section on Working with Arrays https://cljdoc.org/d/com.github.seancorfield/next.jdbc/1.2.659/doc/getting-started/tips-tricks#working-with-arrays

seancorfield 2021-05-21T20:51:56.174400Z

Yeah, Rich is definitely on record as favoring [] for both. As is @alexmiller as I recall.

seancorfield 2021-05-21T20:52:58.175100Z

Feel free to follow-up in #sql where other PostgreSQL users may be able to help (it’s a lot lower traffic than this channel).

looprecur 2021-05-21T20:57:42.176400Z

@seancorfield I'm using the older clojure.java.jdbc 0.7.12. I'll see if I can move to next.jdbc. Very grateful for the help.

seancorfield 2021-05-21T21:03:32.177100Z

c.j.j has a “similar” extension mechanism so you could probably “translate” the next.jdbc approach to your own code but the protocol names are different and it’s much less well documented.

alexmiller 2021-05-21T21:07:47.177600Z

yep

seancorfield 2021-05-21T21:15:42.177800Z

Maybe I’ll get a T shirt made up that says (:import (i.am.with Stuart)) :rolling_on_the_floor_laughing:

2
looprecur 2021-05-21T21:18:06.178Z

Found this code snippet on SO that does the trick: https://stackoverflow.com/a/25786990

1
restenb 2021-05-21T21:59:38.180600Z

so I need to strip troublesome characters out of filenames and replace them with -. troublesome in this case forward/backward slashes, whitespace, underscore, punctuation etc.

restenb 2021-05-21T22:00:03.181100Z

trying to create one single match for clojure.string/replaceand this works; (clojure.string/replace "troublesome\\file .name" #"\s|_|\\|/" "-") -> "troublesome-file-.name"

restenb 2021-05-21T22:00:53.181900Z

however I can't figure out how to get rid of punctuation marks following the same pattern, adding |.to the matcher just replaces everything suddenly

restenb 2021-05-21T22:04:04.182600Z

NEVERMIND, it works using a character class matcher: #"\s|\t|_|\\|/|[.]"

Michael Gardner 2021-05-21T22:17:54.182700Z

#"[\\/.|\s]" is equivalent to that

Michael Gardner 2021-05-21T22:19:01.182900Z

but personally I'd take a whitelist approach like #[^a-zA-Z0-9_]