Fork me on GitHub
Daniel Shriki15:03:59

a method to turn “:some-key” into :some-key?


(keyword "hello-world")


(read-string ":some-key") maybe?

👍 2
💯 2



Ah, :hello-world not hello-world my bad


FYI: read-string shouldn't be used with untrusted input sources: If you need to keywordize user-input strings or other data coming from unknown sources, @U04V4KLKC’s suggestion of clojure.edn/read is the safer option.

Daniel Shriki16:03:31

(read “:some-key”) throws though…

Daniel Shriki16:03:23

Execution error (ClassCastException) at user/eval10570 (form-init6506450375318180810.clj:1). class java.lang.String cannot be cast to class (java.lang.String and are in module java.base of loader 'bootstrap')


you should use (clojure.edn/read ...)


ah yes, clojure.edn/read-string

Daniel Shriki16:03:11

yea, that function works perfectly 🙂

Daniel Shriki16:03:47

no need for the namespace prefix, it’s loaded automatically


as a general practice, dont rely on namespaces other than clojure.core being loaded though. its mostly loaded by something else requiring it

Daniel Shriki16:03:59

also when you open fresh repl?

Daniel Shriki16:03:02

or maybe repl specifically loads more namespaces into the user namespace


yeah running code in the repl vs outside could have varying requiring behaviour. hence the only guaranteed assumption is clojure.core is there. explicitly require any other namespaces.

👍 1
David Hale16:03:07

Found this flowchart and as someone new to the language it really helped clarify when I would use what type where. Wanted to leverage it to multiply other experiences but also lift it up to get it vetted by other more veteran Clojurists as wise counsel!

👍 1
Ian Oliveira17:03:57

Hey everybody, do you guys know if there is a place (like github repo) with "community macros" ? I've know some people that says that the First rule of macros: Don't write a macro but I would like to learn more about the subject and learn better by reading code.


The advice I've seen plenty of times is: look at the Clojure source itself. It's readable enough to be helpful.


flatland/useful has good macros - I guess it moved to commons and isn't flatland any more


my favorite macro:

(ins)user=> (defmacro locals [] (into {} (map (juxt keyword identity)) (keys &env)))
(cmd)user=> ((fn [x] (let [y (* x x)] (locals))) 42)
{:x 42, :y 1764}


using that to write production code is a bad idea, but it's extremely useful for debugging, especially when combined with tap>

Ian Oliveira20:03:12

thank you guys!


There's a Clojure Macro book out there. It's pretty short, but sweet. I really enjoyed it and it explains the different types of things people do with macros.

Ian Oliveira20:03:49

thank you so much @U5JPZFFR6 I think that this is exactly I need.


It's really not that difficult once you "get" it and that book definitely got me over that "one little trick" 😉


I asked in reitit but I figure I'll try here too… I'm having an issue figuring out i'm getting a java.classcastexception, ByteArrayInputStream error here? It occurs write at the format-response and format-negotiate middleware. I've been toying with it for two days and i'm lost.


@decim the best way to debug is to get a full stack trace


not a truncated one like in the gist. A full trace will exactly pinpoint where in the code it is failing


Ok i've updated the gist. with the full print out. It seems ok right until the "format-negotiate" middleware


Sorry that isn't the stack trace


The stack trace should have method names and line numbers and a lot of them


What's printed in the gist is some transformation, and without knowing about your tools it's hard to decipher


Yup sorry... funny I'm not finding the stacktrace in any of my emacs buffers. So what I gave there is the data that pops up in my rept. I'm using ring and reitit and it's showing each middleware as the request is manipulated. and it throws a HTTP 500 error code. So it's not like a full out error where it would kick me out to see the stacktrace. It's just sort of a vague "something is wrong in the encoding/decoding process" without telling me what field. I've been using UUIDs and i'm in the process of switching my SQL tables to use regular IDs. I believe the issue is there because though I can check for UUID I found the ID still is converted to a string and I manually have to convert the string back to UUID. I'm willing to bet my hardships have something to do with that.


The http 500 contains some digest of the exception, nearly useless In the repl or console there should be a full trace somewhere

👍 1

Hmm, let me make my repo public. I was going to say I felt even in my repl I'm not seeing all of the feedback that I normally would.


You don't have to spend much time but even if just in the deps.edn if you see anything horribly wrong?


How do you start your server?


in nexus.clj (start-server)


I was able to figure it out... I was trying to get the data from the request map wrong. Also, I think it's time to learn how to use try/catch so I can catch these sorts of things. THAT would have kicked something out to the stacktrace opposed to my ambiguous errors via repl I believe?

Vinicius Vieira Tozzi21:03:25

Hi everyone, I am still learning clojure, but I have a small question, I wanted to write something like (contains? [:a :b :c] :c) But as I read on clojuredocs this does not work, so I should some, like this: (some #{:b} [:a :b :c]) , but some returns me the object and I only care for true or false, so I wanted to use something like a non-nil? function. It happens to be that this functions is called some? , so in the end my code looks kinda like this: (some? (some #{:c} my-vector))) And this works exactly I want but it’s very weird to read, I think. Is there a more idiomatic to way to write this or that’s it. Maybe there is nothing wrong with it, it just reads a bit weird.


it depends what you are doing with it, but you likely don't need the outer some?


because clojure's if treats false and nil as false, and anything else as true


some? actually returns true for false, so I would tend towards (boolean (some ....)) if I really wanted to constrain the output to true or false

Vinicius Vieira Tozzi21:03:58

Ah that actually makes sense, so I can treat any value as true, instead of expecting exactly true or false from a function, then I can remove the some? and it works fine. Thank you!


just because nil and false being false and anything else being true is usually more what I want, so I reach for boolean out of habit


you can also use the .contains method from java collections, which clojure collections are also


user=> (.contains [:a :b :c] :b)


Ok i've updated the gist. with the full print out. It seems ok right until the "format-negotiate" middleware


Is there a quick/idiomatic way to map over a list of lists, applying a function to the equivalent item in each list? For example: I have a game between two players, made up of rounds, written as so [[1 5][3 2][7 0]] I would want to pass this sequence to a function which returns a pair: the sum for player1 and the sum for player2. Result of example should be: [11 7] How I am doing it now is:

(def game [[1 5][3 2][7 0]])
(reduce #(map + %1 %2) game) 
but it feeels like there’s already a function that does this, and i am curious if so!


user=> (apply map + [[1 5][3 2][7 0]])
(11 7)

👀 1

This is beautiful.

Daniel Craig15:04:19

(map + [1 5] [3 2] [7 0]) 
works too


@USDPTD3FY My apologies, but are you the famous James Bond actor? 🙂

Daniel Craig17:04:17

No I am not 🙂


Do you receive question like mine often?

Daniel Craig18:04:58

Sometimes haha. Most often when a new Bond film is in theaters


there it is! hahaha, thank you!