Fork me on GitHub
#beginners
<
2022-10-21
>
quan xing04:10:57

I use the next.jdbc insert data with mysql. but the column's name is Rank . The Rank is mysql function. So I call in jdbc/insert! insert map to msyql show Execution error (SQLSyntaxErrorException)

(jdbc/insert! conn :test {:ID "ID" :Rank 123})
and generate insert sql is :
insert into test (id,rank) values ("id", 123) 
The rank is mysql's function, so It's output SQLSyntax , how can I generate SQL like this:
insert into test (`id`,`rank`) values ("id" 123)

seancorfield05:10:14

Look at :table-fn and :column-fn in the documentation and the quoted namespace.

quan xing05:10:57

Ok, I got it. Thanks.

Abhi Saxena04:10:28

Hi All, I have a vector (of vectors) that looks like: [[:a 560] [:b "09/22/2018"]] How can I destruct it to generate below string: a = 560, b = '09/22/2018' I am able to achieve this with below code by I have to use destruction to generate this, here values in the input vector

(join "," (map #(format "%s = %s" (name (nth % 0)) (nth % 1)) values))

Martin Půda05:10:57

But there are no apostrophes around "09/22/2018":

(->> [[:a 560] [:b "09/22/2018"]]
     (map (fn [[kw v]] (format "%s = %s" (name kw) v)))
     (s/join ", "))

=> "a = 560, b = 09/22/2018"
You can also read https://clojure.org/guides/destructuring.

craftybones05:10:01

I'd pull the anonymous function to its own function. Reads better, easier to isolate, test etc.

👍 1
skylize12:10:35

Nested destructuring is ugly and hard to read, but it does work.

(let [[[_ a] [_ b]] [[:a 560] [:b "09/22/2018"]]]
  (str "a = " a ", b = '" b "'"))

👍 1
Rambabu Patina11:10:54

Hi Team, I need help with shadow-cljs. I want to exclude particular library while building. The shadow-cljs.edn looks such as

:app {:target :browser
   ....
   :modules {:shared {:entries []}
             :other {:entries ..}
   ...
My assumption is if the entries is empty (i.e []) then bundling all dependencies in public/shared.js file. I want to exclude a library which is not required at client side. I appreciate your time. Thanks

skylize21:10:55

For questions specifically about shadow, I definitely suggest asking in #C6N245JGG. @U05224H0W is absolutely amazing with providing thorough (and usually very fast) support. Your assumption about {:shared {:entries []} ... } is pretty far off. > You can leave the :entries of the :shared module empty to let the compiler figure out which namespaces are shared between the other modules. -- https://shadow-cljs.github.io/docs/UsersGuide.html#CodeSplitting So with this config, shadow will follow the entry points defined by your other modules in order to calculate for you what is shared. The result of that calculation will be included in the shared module.

👍 1
stopa13:10:11

Hey team — Say I have a collection, and I want to run an an http request per item What would you think if I wrote

(pmap fetch-thing coll)
pmap is so good, but I am a bit wary about futures and what this could mean for the efficiency of the system. So maybe another way to phrase the question: How should I think about the efficiency tradeoff of pmap? If I have a system with 1GB of ram for example, at what point will these pmap calls take me out? Would you think about this differently?

rolt13:10:51

you can have a look at https://github.com/clj-commons/claypoole if you want a bit more control the hard part of this problem is usually handling network failures core async's pipeline could help too

❤️ 1
MegaMatt13:10:52

pmap doesn't start that many processes. It is "calibrated to suit the number of CPU cores available (https://www.oreilly.com/library/view/clojure-programming/9781449310387/ch04.html)". I have used it a lot as a dirty solution for exactly this use case and it has worked great.

❤️ 1
stopa13:10:01

Thanks @U02F0C62TC1 @U02SD8PATK2 — this was helpful!

ghadi14:10:15

pmap is simple but suffers from head-of-line blocking

👍 1
Benjamin14:10:44

what is a good way of allowing both namespaced and un namespaced keys as input to a function? Or would it be bad style to want such a thing?

Benjamin14:10:36

I don't mean a mix but allowing both :foo/foo and :foo. Specifically I would destructure an input map

Alex Miller (Clojure team)14:10:05

I think it's generally something to avoid as it's confusing to both callers and in the receiving code

Alex Miller (Clojure team)14:10:33

but you can do so like:

(let [{unfoo :foo, :foo/keys [foo]} m
      the-real-foo (or unfoo foo)] ... )

Eric16:10:03

I’m looking for a function like filter, but it should stop early when it finds the first match. Or is it okay to just do (first (filter pred collection)) because of lazy evaluation?

ghadi16:10:05

you are correct about the lazy

👍 1
tomd16:10:23

or some with when:

(some #(when (pred %) %) collection)

fappy17:10:15

hi 🙂 Is there a value you can pass to a keyword that could cause an exception?

(:foo some-value)
I tried string and integers. Context: I’m inside a Kafka consumer and I have a message that my consumer could not handle. Normally the data I care about is inside the message and I get it like this
(:data message)
The Kafka partition, offset, etc are normally pulled out of the message like this
(:meta message)
I’m wondering if I can safely do these two operations when logging my consumer’s failure ---- or if applying :data and :meta could fail, killing my Kafka consumer. I suppose I could wrap that itself in a try if the answer to the original question is “Yes”. But if the answer is “No there’s no such value that could cause a keyword to throw”, that’d be nice to know. TIA

hiredman17:10:24

user=> (:foo (reify clojure.lang.ILookup (valAt [_ _] (throw (Exception.)))))
Execution error at user$eval1$reify__137/valAt (REPL:1).
null
user=>

fappy17:10:48

heh, is that a kind of value you can read off of a kafka topic without using reify in the consumer?

hiredman17:10:25

dunno, my point it is depends on what message is, not so much the keyword

pppaul17:10:41

if you want to be safe, use get

hiredman17:10:58

I mean, get will do the same thing

pppaul17:10:21

destructuring is also ok. using keyword syntax is not the same as get and it has bitten me in the ass a few times.

hiredman17:10:41

user=> (get (reify clojure.lang.ILookup (valAt [_ _] (throw (Exception.)))) :foo)
Execution error at user$eval1$reify__137/valAt (REPL:1).
null
user=>

pppaul17:10:16

@U0NCTKEV8 try a non ILookup

pppaul17:10:29

that part is dynamic, you don't really know the param type being given to :foo or get

hiredman17:10:48

my point, for either keywords as functions or using clojure.core/get is that the collection is really in control

hiredman17:10:57

and it can do whatever it likes

hiredman17:10:12

so that is what you need to know about if you what to see if it throws or not

hiredman17:10:41

what what "message" is in the question is not really specified

pppaul17:10:14

I think what matters here may be what the desired behaviour is on the lookup when it's in a fail case. ignore and process message, or throw + do error handling stuff?

pppaul17:10:35

doing something like if-let on a get that returns nil is pretty convenient, compared to keyword lookup that throws cus you gave it something like a string or something else bad

pppaul17:10:45

but, for the very first question (are there values that make keyword lookups throw), yes, and actually it's most values that do that, but typically you restrict the types you use in a clojure program to just a handful that do work with keyword lookup

hiredman17:10:04

user=> (get (reify java.util.Map (get [_ _] (throw (Exception.)))) :foo)
Execution error at user$eval1$reify__137/get (REPL:1).
null
user=> (:foo (reify java.util.Map (get [_ _] (throw (Exception.)))))
Execution error at user$eval140$reify__141/get (REPL:1).
null
user=>

pppaul17:10:43

i'm pretty sure (:foo "my string") doesn't work

pppaul17:10:08

i'm not sure what values keyword lookup doesn't like, but i have run into them a few times and they make very weird errors (just because they are unexpected)

hiredman17:10:10

there is some complicated stuff with keywords as functions, the compiler will insert inline caches for those because they might be record field lookps

pppaul17:10:32

maybe i shouldn't be so worried about them. just tried a lot of different test cases i thought should fail and they didn't. makes me very curious as to the ones i ran into. maybe they are specific to yada or something

Grimgog17:10:57

Hey folks, I've seen recently following formulation:

(fn [report-instance {:account/keys [id]}]
             <some code...>)
It is an action inside a button in fulcro but I don't understand what does the params definition mean and how does this work? Why can I define report-instance AND {:account/keys [id]} ? I suppose that report-instance and id are coming from the button, or?

Ed17:10:30

that's a function that takes 2 arguments. The first is bound to report-instance inside the function body and the second arg is destructuded (because it's assumed to be a map) so that id is bound to the :account/id key in the map. Is this in clojurescript?

Grimgog17:10:23

yes it is in cljs. ok so there isn't something special. I've tried to delete a form with a "delete-button". But there are only following functions: form/delete!, form/delete-entity. In form/delete! I have to provide [this id-key entity-id] whereas this is report-instance I guess and id-key is the uuid from my account. But what is entity-id and where to get it?

Ed17:10:48

I'm not sure I know what form/delete-entity is ... you might have better luck asking in #C68M60S4F ?

Grimgog17:10:30

thanks so far, I will do that!

👍 1
sb20:10:12

Somebody can help me how to possible add this configuation to deps.edn? (I rewrite my lein project to deps.edn.. and is that possible?) https://github.com/Otann/garden-gnome

:garden {:builds [{;; Source path where to watch for changes:
                   :source-path "dev/sample"
                   ;; The var containing your stylesheet:
                   :stylesheet  sample.styles/screen
                   ;; Compiler flags passed to `garden.core/css`:
                   :compiler    {:output-to     "resources/public/screen.css"
                                 :pretty-print? true}}]}

sb20:10:31

I added as extra-deps `

{:dev
 {:extra-paths ["src/dev"]
  :extra-deps  {garden-gnome/garden-gnome {:mvn/version "0.1.0"}}}
but the configuration part.. where is the right place?

robertfw21:10:45

Looking at the source code, it looks like you can put your config into a garden.edn file in lieu of having a project.clj https://github.com/Otann/garden-gnome/blob/master/src/garden_gnome/watcher.clj#L70

👍 2
🍻 1
sb21:10:27

true, I totally forget.

sb21:10:44

I didn’t use a lot time ago in this way. Thanks!

robertfw21:10:15

Happy to help 🙂

Eric23:10:06

For finding the maximum item in a seq, is there any reason to prefer one option over the other: (reduce max my-sequence) vs (apply max my-sequence) ?

Alex Miller (Clojure team)23:10:21

I think the former is more direct. The latter is going to gather up all the inputs to make one big function call, which is going to walk through everything anyways. The reduce has a lot of different optimizations built in too depending on what my-sequence is

hiredman23:10:23

Clojure 1.11.1
user=> (apply max [])
Execution error (ArityException) at user/eval1 (REPL:1).
Wrong number of args (0) passed to: clojure.core/max
user=> (reduce max [])
Execution error (ArityException) at user/eval3 (REPL:1).
Wrong number of args (0) passed to: clojure.core/max
user=>

hiredman23:10:42

user=> (apply max 0 [])
0
user=> (reduce max 0 [])
0
user=>

Bob B23:10:09

personally, apart from performance implications, I generally prefer to use reduce when I think of the operation as logically a reduce (typically operating on two items at a time), and apply when using a collection as function arguments (e.g. (apply partition [2 [1 2 3 4]]) )

Sam Ritchie01:10:32

In #C01ECA9AA74 I switched to apply for examples like this because 1. Certain functions like * that I would usually reduce with can short circuit and stop consuming the sequence if they hit a zero 2. The multi arity version reduces internally anyway But I’m curious about @U064X3EF3 ‘s comment here about gathering up all inputs

hiredman01:10:09

Short circuit on zero is intriguing

hiredman01:10:08

(defn saturates [f v] (fn [a x] (let [r (f a x)] (if (= r v) (reduced r) r))))

Sam Ritchie01:10:57

“and” saturates on false, “or” on true

hiredman23:10:19

I would always throwing in a starting element in both