Fork me on GitHub
#beginners
<
2023-05-23
>
Tommi Martin06:05:16

Hi, Quick question as I build my skills using clojure i'd like to keep a close eye to potential clojure style guides, i've browsed two found from slack so far but neither of them seem to support automated linting that complains to me whenever I write the code in vscode for example. Are there such linters for commonly used/approved styleguides in clojure?

Colin (fosskers)08:05:02

Do you use clojure-lsp?

Colin (fosskers)08:05:16

It has clj-kondo built-in, which is the main linter.

Tommi Martin12:05:39

Currently i'm not using any but it seems that clj-kondo is available for vs-code. Thank you very much for pointing me in the right direction

skylize13:05:26

For VS Code, you will want to install Calva. It comes with clojure-lsp and clj-kondo.

Tommi Martin13:05:07

Hmm I was just looking at that, Calva is installed yet I see precious few warnings in my code. I don't think i'm that good as a beginner, so might need to play around to see if it's working right

skylize13:05:19

clj-kondo has a lot of optional linters. Borkdude likes making new linters, but is pretty frugal about pushing them on people by default. If you see some warnings, but want to see more, Checkout the docs to add more linters to your setup. https://github.com/clj-kondo/clj-kondo/blob/master/doc/config.md#options https://github.com/clj-kondo/clj-kondo/blob/master/doc/linters.md

Tommi Martin13:05:56

Thank you kindly I'll be sure to take a look

seancorfield14:05:21

In terms of style guides, this one is the most widely followed: https://guide.clojure.style/ -- I'm not sure how much of it clj-kondo can enforce but Calva + LSP + Kondo is a great combination with VS Code!

👀 2
mister_m17:05:19

I am getting weirdly stumped by this: how do I turn a list of characters into a string? I've so far managed to just map with str over the list - how can I join them?

dpsutton17:05:08

(apply str [\a \b \c])

☝️ 2
dpsutton17:05:57

The fact that they are characters is irrelevant. It’s just dealing with the collection

mister_m17:05:52

Right - I just wasn't thinking of apply being the idiom for putting it together

mister_m17:05:38

is there an arity limit on apply str? would a phenomenally long list of characters via apply break a call to str ?

dpsutton17:05:34

You tell me :). Try it out in your repl

🧠 2
mister_m17:05:56

:saluting_face:

jpmonettas17:05:15

@U01188CHUFL when you give many arguments to str, it is just a loop over the sequence adding one by one to a StringBuilder, so should be ok https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L556-L561

jpmonettas17:05:40

clojure.string/join does the same, so they should be equivalent on the perf side

Bruno Porto18:05:01

I did not validate it. But I would give reduce a try.

Bruno Porto18:05:35

(reduce #(str %1 %2) "" ["o" "l" "a"])

dpsutton18:05:37

That is the worst way to go about this

dpsutton18:05:13

Strings are horrible accumulators for more strings. Look at the source of str to see how it accumulates parts of the final string in its reduce

jpmonettas18:05:15

it is going to constantly be creating StringBuilders and discarding them after adding one more String

jpmonettas18:05:02

also looks uglier than (apply str [...]) imo

jpmonettas18:05:54

the only thing faster is if your original list of chars is a char[], then you can just build the string with the char[] constructor, like (String. (char-array [\a \b \c]))

dpsutton18:05:51

regardless of aesthetics, it can be catastrophic on large collections

(let [coll (repeat 1000000 "a")]
  (time (count (apply str coll)))
  (time (count (reduce str "" coll))))
"Elapsed time: 54.076833 msecs"
"Elapsed time: 49594.28475 msecs"
1000000
.054 seconds vs 49 seconds

😮 2
Bruno Porto18:05:09

Nice! Thanks for the insight! Another doubt i was thinking of now: from a bigO notation perspective it would run the same amount, correct?

jpmonettas18:05:59

yes, they are both k*O(n) with very different ks

2
👍 2
dpsutton18:05:27

I’m not sure that’s true due to the constructor of the string builder

jpmonettas18:05:29

yeah, I guess it depends if the constructor is O(1)

jpmonettas18:05:41

looking at the source, it isn't, it copies the CharSequence, so I was wrong, it isn't O(n)

💡 2