Fork me on GitHub
#beginners
<
2020-05-09
>
Spaceman07:05:37

Is there a way to automatically format my clojure code in emacs? Like something that includes a prettifier that squashes all the trailing parenthesis to a single line automatically?

Vincent Cantin09:05:20

I have a question about the conventions in Clojure .. predicates’s names are often ended with a ? . Should we also use a ? for hash-map keys (keywords) referring to boolean values? e.g. :valid? instead of :valid

Michaël Salihi12:05:19

Good question whose answer interests me too. I think so, from what we can observe on some libraries like ring-jetty-adapter for example: https://github.com/ring-clojure/ring/blob/master/ring-jetty-adapter/src/ring/adapter/jetty.clj#L133-L140

krzyz01:05:16

I think it's less important with map keys. The reason it is a convention for procedure calls is that it informs you that you are returning a boolean, but with a map key, you will see a default value of true/false right next to it. Don't let that stop you from doing it if that's how you like it though. It definitely doesn't hurt. 🙂

👍 4
dev.4openID10:05:17

@pshar10, if in emacs/spacemacs just highlight code and then press tab - it formats; if in vsc then press Ctl-shift-P and select beautify (if not found you need to add the add-on)

Spaceman11:05:15

in spacemacs that just indents it. I mean making: ((( code... ) ) ) to (((code ... )))

AC18:05:33

a common trick is to use smartparens (or paredit, etc) to clean up as you go. for example, hitting ")" to close the sexp causes the parentheses to gather on the same line.

Spaceman20:05:26

I do have paredit mode on on spacemacs but that doesn’t automatically gather them?

AC21:05:57

I may be mistaken about paredit -- I thought it behaved the same but it's been a while since I switched to smartparens. (the example above is smartparens with sp-autoskip-closing-pair set to 'always)

dev.4openID10:05:00

(def d1 [{:key "document_env", :value "teste"} {:key "document_env2", :value "teste2"}])

dev.4openID10:05:51

@tvalerio @andy.fingerhut an alternative is (flatten (map vals d1)) results in ("document_env" "teste" "document_env2" "teste2"); it is equivalent, no?

andy.fingerhut12:05:53

That is a list of 4 strings. The questioner was asking how to get a result that was a map with 2 key/value pairs, which is a different result.

Spaceman11:05:19

I want to ask an open question about macros to seasoned clojurians. What do you use macros for in your projects? Can you give an example where you used a macro and were like, "holy shit, this makes life so much better." Would you please share it?

tristefigure13:05:49

Hi. I'm not a beginner but I'd like to know what's the recommended way to go with ClojureScript these days. Figwheel ? Shadow-cljs ?

tristefigure13:05:29

I don't practice ClojureScript often enough to know which one I should pick.

simongray13:05:35

Shadow-cljs is quite easy to get started with, but if you start a new CLJS project using a leiningen template, then figwheel is usually set up for you too. One advantage of shadow-cljs is that it's more of an all-in-one tool, so if you're only doing basic CLJS then you only need this.

alpox16:05:54

Hi all! Just a short question: Is there any way to register spec definitions (`s/def` ) with dynamic keywords?

alpox16:05:28

I saw it will be possible in spec 2 with s/register , but I wonder if there is a solution to this in spec 1

alpox16:05:56

I found a solution through spec-tools.impl/register-spec!

include21:05:11

Hi. 🖐️ anyone knows if this website is maintained/updated https://www.clojure-toolbox.com/ ?

phronmophobic21:05:51

it’s definitely being updated (I had a library added not too long ago). afaik, it’s being updated via pull requests that anyone can submit, but I’m not sure it’s curated in the sense that I don’t think libraries get removed or are actively searched for by the maintainer.

phronmophobic21:05:12

it’s possible that it’s being actively maintained, but I’m not sure.

phronmophobic21:05:18

it’s worth noting that it’s common for clojure libraries to go years without changes and still work well in the present. it’s one of the benefits of clojure that clojure code bitrots much more slowly than other ecosystems

include21:05:56

I agree about your last point but for newbies like me we get a little bit confused :) in fact I was searching for ‘etoin’ alternatives but didn’t found any place to search.

include21:05:26

as a newbie I spend lots of time “”Yak shaving”” libs

phronmophobic21:05:37

i’ve never thought about how other people evaluate libraries

phronmophobic21:05:53

i’ve also never heard of etoin

include21:05:54

I don’t have yet the proper/deep knowledge to eval if I am using the correct lib for some task :( sometimes it’s frustrating

phronmophobic21:05:16

I don’t feel like I have issues evaluating libraries, but it’s also possible that I’ve only gotten marginally better and have just gotten used to it

include21:05:37

Like libs for ArangoDB. All look pretty outdated regarding their features and API versioning

include21:05:02

Yes maybe you rock in clojure :)

phronmophobic21:05:29

I also have a background with java and javascript, so it’s nice to be able to add java and javascript libraries to the list that I might be comfortable with, but I could see how that might be annoying for developers who are looking at clj and cljs as a way to avoid learning the dumb quirks and trivialities of js and java

phronmophobic21:05:55

i’m usually pretty wary of libraries that try to do too much on top of wrapping java/javascript libraries

phronmophobic21:05:56

for something like arangodb, I would probably prefer using the java library directly unless there’s a clj wrapper library that looks like a really good fit

include21:05:24

So in that case do you prefer to just go with the java lib version and use interop?

include21:05:05

I was writing my question before reading your reply :) nice nice I kind like your reply

include21:05:48

I will follow your tip! :)

phronmophobic21:05:04

an example of a library that adds a lot of value compared to using java library is carmine (a redis client lib). https://github.com/ptaoussanis/carmine

include21:05:40

Let me check

phronmophobic21:05:14

since the redis protocol has a https://github.com/antirez/redis-doc/blob/master/commands.json. you don’t really have to worry about it not supporting some feature

include21:05:34

cool distributed lock also 🙂

include21:05:24

I see. you can just work with that data structure

Scott Starkey22:05:03

OK, I’m learning about the threading macros, and I’m wanting to thread a state through the following transformations:

(my-function state 0)
(my-function state 0)
(my-function state 0)
(my-function state 0)
(my-function state 1)
(my-function state 1)
(my-function state 1)
(my-function state 1)
… and then get the state out the other side. Can someone please help me with the idiomatic way of doing this?

seancorfield22:05:57

@scotto Your function returns the new state each time?

seancorfield22:05:28

If so, (-> state (my-function 0) (my-function 0) ... (my-function 1) (my-function 1))

Scott Starkey22:05:17

That’s what I thought, but it failed for some reason in practice. Lemme try it again.

seancorfield22:05:17

If your calls really are to the same function every time, just with different arguments after the state, I'd probably use reduce instead.

seancorfield22:05:00

(reduce (fn [state data] (my-function state data)) state collection-of-data-values)

👍 4
jsn23:05:32

Why (fn [state data] (my-function state data) , why not just pass my-function directly to reduce ?

seancorfield23:05:58

@UTQEPUEH4 In this case, yes, that works. I was trying to show the shape of the (my-function state 0) style calls (and also in the back of my mind thinking of a general case where my-function actually took more than two arguments and you might need (apply my-function state data) with collection-of-data-values being collection-of-collection-of-argument-values 🙂 ).