Fork me on GitHub
#beginners
<
2022-03-16
>
Daniel Shriki15:03:59

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

mruzekw15:03:13

clj꞉user꞉>
(keyword "hello-world")
:hello-world

lispyclouds15:03:34

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

👍 2
💯 2
delaguardo15:03:43

clojure.edn/read

mruzekw15:03:02

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

respatialized15:03:58

FYI: read-string shouldn't be used with untrusted input sources: https://clojuredocs.org/clojure.core/read-string 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.

1
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.io.PushbackReader (java.lang.String and java.io.PushbackReader are in module java.base of loader 'bootstrap')

lispyclouds16:03:02

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

lispyclouds16:03:36

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

lispyclouds16:03:37

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

lispyclouds16:03:10

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! https://cemerick.com/blog/2011/07/05/flowchart-for-choosing-the-right-clojure-type-definition-form.html

👍 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.

Nundrum18:03:43

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

1
noisesmith18:03:25

flatland/useful has good macros https://github.com/clj-commons/useful - I guess it moved to commons and isn't flatland any more

noisesmith18:03:12

my favorite macro:

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

noisesmith18:03:05

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!

clj.max20:03:23

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.

clj.max20:03:16

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

v3ga18:03:02

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. https://gist.github.com/v3gal0g/0f902bb97882b8453637fa78d7d536d0

ghadi18:03:43

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

ghadi18:03:11

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

v3ga21:03:31

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

ghadi22:03:39

Sorry that isn't the stack trace

ghadi22:03:12

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

ghadi22:03:59

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

v3ga22:03:52

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.

ghadi22:03:31

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

👍 1
v3ga22:03:29

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.

v3ga23:03:10

You don't have to spend much time but even if just in the deps.edn if you see anything horribly wrong? https://github.com/v3gal0g/chrysalis-xd

ghadi23:03:35

How do you start your server?

v3ga00:03:44

in nexus.clj (start-server)

v3ga06:03:17

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.

hiredman21:03:51

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

hiredman21:03:11

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

hiredman21:03:38

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!

hiredman21:03:17

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

hiredman21:03:03

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

hiredman21:03:21

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

v3ga21:03:31

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

zach22:03:08

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!

hiredman23:03:44

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

👀 1
littleli12:03:42

This is beautiful.

Daniel Craig15:04:19

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

littleli17:04:04

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

Daniel Craig17:04:17

No I am not 🙂

littleli17:04:59

Do you receive question like mine often?

Daniel Craig18:04:58

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

zach23:03:08

there it is! hahaha, thank you!