This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-06-30
Channels
- # announcements (40)
- # babashka (41)
- # beginners (32)
- # calva (15)
- # clara (8)
- # clj-kondo (14)
- # cljs-dev (30)
- # clojure (37)
- # clojure-dev (8)
- # clojure-europe (21)
- # clojure-norway (21)
- # clojure-uk (4)
- # clojured (3)
- # clojurescript (4)
- # community-development (10)
- # core-async (13)
- # cursive (23)
- # datomic (15)
- # emacs (9)
- # fulcro (3)
- # google-cloud (4)
- # graphql (24)
- # gratitude (2)
- # holy-lambda (4)
- # honeysql (5)
- # hyperfiddle (9)
- # keechma (1)
- # klipse (5)
- # lsp (23)
- # malli (4)
- # missionary (32)
- # pathom (28)
- # re-frame (2)
- # reagent (40)
- # reitit (17)
- # releases (2)
- # remote-jobs (1)
- # shadow-cljs (25)
- # specter (3)
- # vim (19)
- # xtdb (41)
hello, i have this function
defn f [a b & c] (println c)
is there a reason why (f 1 2)
prints nil
instead of an empty listAnd while there is such thing as an empty seq now in clojure, it wasn't always the case, there was only nil
And in many cases and places things still operate like that is the case, and it is mostly fine
@UK0810AQ2 it prints 2 nils, first nil being the c
and the second being the return value of println
@U03N4NQ0PCH aye, my bad, misread the question, sorry
also, i'm trying to replicate the +
function by defining anonymous functions
(def sum2 #(+ %&))
(sum2 1 2 3)
why does it throw this error?
Execution error (ClassCastException) at java.lang.Class/cast (Class.java:3889).
Cannot cast clojure.lang.ArraySeq to java.lang.Number
ohhh, ok thanks
So apply
is what you want.
(def sum2 #(apply + %&))
i see, thank you
Btw, I never noticed it before, but (apply + nil)
as well as (apply + '())
and (+)
return 0
, so nil
behaves like an empty sequence here (which would be consistent to the behaviour of the empty vararg in your previous question). But (+ nil)
returns nil
.
(apply + nil)
should be (apply + '(nil))
to be the equivalent to (+ nil)
. And its returns nil
.
Yeah it does make sense. I just find it a bit weird that nil
and '()
work the same way in args given that in Clojure they are not equal like in some other Lisps.
OK I see, interesting catch indeed.
I have a rest endpoint function like this:
(defn get-user-details
[{:keys [userid]}]
{:userid userid
:result {:details (j/execute-one! (make-db) (get-user-details-normal-exp userid))
:roles (j/execute (make-db) (get-user-roles-normal-exp userid))
:functions (j/execute (make-db) (get-user-functions-normal-exp userid))}})
The three keys inside :result
are populated using db select queries. How can I parallelize this?I think your question is a bit opaque. Clojure maps values are not realized until accessed. So your map, until you call something like
((get-in (get-user-details 123) [:result :details]))
will never have actually called the database.its at that point you would call the execute command. If you want to force the queries to run when you call the function, you can wrap your functions with
doall
and that will force them to execute.The parallel part however is not handled here. You could use pmap
to iterate over the functions you want to call then it would be paralleled. However, I think parallelizing query calls like this might be a bit of a code smell. At least to me, it seems like something would be off if doing this.
I may have been mistaken, one second while I look into this again
Right so, maps are eager but map is not which is where I got confused. Parallelizing the executions is something you could do in some form with https://clojuredocs.org/clojure.core/pmap
I have below line of code , How do you sort-by according to :uri ?
(def p (group-by :uri [{:user-id 1 :uri "/zinc"}
{:user-id 2 :uri "/account"}
{:user-id 1 :uri "/foo"}])
)
what do you want to sort? the result of group-by or values of the result?
basically if the map is as below , i would like to do a group-by first and then apply sort for uri which is of type string
(def a [{:user-id 1 :uri "/zinc"}
{:user-id 2 :uri "/account"}
{:user-id 1 :uri "/foo"}])
(group-by :uri a) will give you a map of uri to the list of maps. How do you expect it to be sorted?
i was looking into the article https://stackoverflow.com/questions/47847937/cleaner-way-to-sort-and-order-a-vector-of-maps-in-clojure
(def my-maps
[{:name "jess", :age 32}
{:name "ruxpin", :age 4}
{:name "jess", :age 35}
{:name "aero", :age 33}
{:name "banner", :age 4}])
(update-vals (group-by :name (sort-by :age my-maps)) last)
I can answer on the question from stackoverflow. But for your original question I still need more information 🙂Thanks @U04V4KLKC, What information you need ?
> (group-by :uri a) will give you a map of uri to the list of maps. How do you expect it to be sorted?
hmmmmm, how that is being sorted in stackoverflow? i am writing the code referring stack flow post,
This is going to be the result of your group-by
with uri
👇
{"/zinc" [{:user-id 1, :uri "/zinc"}],
"/account" [{:user-id 2, :uri "/account"}],
"/foo" [{:user-id 1, :uri "/foo"}]}
Sorting here by uri
doesn’t really make much sense since each vector will already be sorted by identical uri
values :thinking_face: If you want the map keys to be sorted than you can use a https://clojuredocs.org/clojure.core/sorted-map.
I think it’d be helpful if you write what your input data is and what you’d like your desired output data to be :thumbsup: