This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-03-16
Channels
- # aleph (1)
- # announcements (16)
- # babashka (36)
- # beginners (62)
- # calva (15)
- # cider (21)
- # cljsrn (5)
- # clojure (84)
- # clojure-dev (3)
- # clojure-europe (22)
- # clojure-italy (2)
- # clojure-nl (2)
- # clojure-uk (3)
- # clojurescript (36)
- # core-async (2)
- # cursive (4)
- # datomic (8)
- # emacs (14)
- # events (1)
- # fulcro (4)
- # hyperfiddle (6)
- # introduce-yourself (3)
- # jobs (1)
- # leiningen (4)
- # lsp (100)
- # nrepl (3)
- # off-topic (36)
- # pathom (17)
- # podcasts (1)
- # polylith (4)
- # portal (14)
- # react (1)
- # reagent (3)
- # reitit (8)
- # releases (3)
- # remote-jobs (1)
- # reveal (7)
- # shadow-cljs (19)
- # sql (16)
- # web-security (3)
a method to turn “:some-key” into :some-key?
clojure.edn/read
@U7ERLH6JX awesome, tnx!
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.
(read “:some-key”) throws though…
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')
you should use (clojure.edn/read ...)
same error
ah yes, clojure.edn/read-string
yea, that function works perfectly 🙂
no need for the namespace prefix, it’s loaded automatically
tnx a lot!
as a general practice, dont rely on namespaces other than clojure.core
being loaded though. its mostly loaded by something else requiring it
also when you open fresh repl?
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.
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
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 https://github.com/clj-commons/useful - 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)))
#'user/locals
(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>
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.
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. https://gist.github.com/v3gal0g/0f902bb97882b8453637fa78d7d536d0
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
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
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? https://github.com/v3gal0g/chrysalis-xd
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?
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.
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
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
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!(map + [1 5] [3 2] [7 0])
works too@USDPTD3FY My apologies, but are you the famous James Bond actor? 🙂
No I am not 🙂
Sometimes haha. Most often when a new Bond film is in theaters