Fork me on GitHub
#beginners
<
2022-02-15
>
quan xing00:02:58

1. How can I use connection pool ?

(defn insert!
  [db-spec table record-data]
    (with-open [connection (jdbc/get-connection db-spec)]
    (jdbc-sql/insert! connection
                      table
                      record-data
                      jdbc/snake-kebab-opts)))
2. How do I constrain that record-data is a Student data structure. Student is like java type class Student{ String name,int age ..}

seancorfield01:02:08

Not quite sure what you're asking for 2.? You have a hash map and you want to ensure only the required keys are passed to insert!? You could use select-keys. Is it likely that your record-data has extra keys? Where did it get constructed/come from?

quan xing02:02:17

I mean how do I verify the validity of record-data data, such as what fields must be included in reocrd-data

seancorfield02:02:32

@imxingquan Spec is the "standard" for declaring/checking the structure of data in Clojure, and can specify required and optional fields in a map. Note that it's idiomatic in Clojure for maps to be "open", i.e., they're usually allowed to have additional keys which code just ignores. That matters when you're inserting a hash map into a database but otherwise mostly doesn't.

seancorfield02:02:52

(there are community alternatives to Spec that some people seem to prefer)

quan xing02:02:52

ok, thanks @seancorfield. and how can I print execute sql statement to console log in next.jdbc?

seancorfield03:02:29

https://cljdoc.org/d/com.github.seancorfield/next.jdbc/1.2.772/doc/getting-started#logging - in the docs, just below the connection pooling section I linked you to before. May be worth reading all the docs - there are lots of good hints and tips in there.

quan xing02:02:17

How to concatenate complex query conditions in next.jdbc/query? for example: select col1,col2,col3 from demo where col1= ? how to dynamically add and col2 = ? or and col3=?

dorab02:02:10

You might want to take a look at honeysql which allows you to treat SQL statements as Clojure datastructures. So, you can build up a SQL statement by building up Clojure data structures.

quan xing05:02:35

(def a [{:id 123 :name "name1" :age 10},{:id 456 :name "name2" :age 11}]) #m3{:name "name3" :age 12} how can I group by m3's keys like this (group-by m3-keys a)

dpsutton05:02:47

given that a is [{:id 123 :name "name1" :age 10} {:id 456 :name "name2" :age 11}], what output do you want? And what is m3? (ps, use a single backtick before and after forms to make them appear a bit nicer to parse

seancorfield06:02:28

@imxingquan It's not clear what you're asking but perhaps:

user=> (def a [{:id 123 :name "name1" :age 10},{:id 456 :name "name2" :age 11}])
#'user/a
user=> (def m3 {:name "name3" :age 12})
#'user/m3
user=> (group-by (apply juxt (keys m3)) a)
{["name1" 10] [{:id 123, :name "name1", :age 10}], ["name2" 11] [{:id 456, :name "name2", :age 11}]}
user=> 

seancorfield06:02:56

(if you formatted your code examples and showed what you had tried in the REPL, you might get better answers)

Roee Mazor06:02:05

Hello, what should be my goto http server? simple rest api, no crazy throughput needed, server is expected to stay idle most of the time.

seancorfield06:02:35

@roeemz Just use the embedded Jetty server that comes with Ring.

🙏 1
Roee Mazor06:02:55

Awesome, thanks!

quan xing06:02:25

yes. I wrote (group-by (juxt (keys m3)) a) and show error. but why it use apply . how can I understand (apply (just (keys m3)) @seancorfield

seancorfield06:02:33

juxt is variadic. Given m3, you want (juxt :name :age), yes? (keys m3) gives you [:name :age] so you need to apply juxt to that vector.

seancorfield06:02:59

(but be aware that keys does not necessarily produce the keys in any specific order)

seancorfield06:02:39

So, depending on exactly what you want as the result, you might either want (apply juxt (sort (keys m3))) or (comp set (apply juxt (keys m3)))

seancorfield06:02:50

(the former will produce the values for :age and :name respectively to group by; the latter will create sets of the name (string) and age (number) -- in both cases items with the same age and name will group together)

quan xing06:02:25

OK, I'm clear. thank you very much @seancorfield

quan xing07:02:38

I use ide with the vscode + calva . use ctrl+c+j start REPL, I need ctrl+c load every file alone. Is there any way to load all the files you need

flowthing07:02:16

If you have:

(ns foo.bar
  (:require [baz.quux :as quux]))
And you evaluate the ns form or use the "Load current file" command, baz.quux will also be loaded.

☝️ 1
quan xing08:02:19

How do I check whether the keys of a nested map exist

noisesmith15:02:00

are you looking to check if in nested maps, a specific key occurs in any of them?

noisesmith15:02:56

if so:

(ins)user=> (defn contains-key?
              [nest k]
              (let [maps (->> nest
                              (tree-seq coll? seq)
                              (filter map?))]
                (some #(contains? % k)
                      maps)))
#'user/contains-key?
(cmd)user=> (contains-key? {:a {:b {:c 0}}} :b)
true
(cmd)user=> (contains-key? {:a {:b {:c 0}}} :z)
nil
that's how I would do it at least

quan xing09:02:08

which use the library for schedue like java quartz

Matej Šarlija09:02:16

Question - How come

(defn foo [x] (list x (+ x 2) (* x 2)))
and
(defn foo [x] '(x (+x 2) (* x 2)))
are not the same?

sb09:02:43

you use quote syntax at the second, back-tick is “maybe” the correct here

sb09:02:15

user=> (= (list 1 2) `(1 2)) true

Matej Šarlija09:02:54

Thanks, bit clunky.

flowthing14:02:31

The quote (`'`) suppresses evaluation:

user=> (def x 1)
#'user/x
user=> 'x
x
user=> x
1

flowthing14:02:51

What are you trying to achieve?

noisesmith15:02:27

(x (+ x 2) ~(* x 2))` would make the same list, but it's silly imho to use ` when you have that many unquotes in it normally we'd just do [x (+ x 2) (* x 2)] unless eg. building a macro where you really need lists and not just sequential collections

Matej Šarlija17:02:20

@U4ZDX466T nothing much, just wondering

quan xing09:02:52

(def cfg (Config.))
(.setId cfg 123)
how can I write code like cfg.inst_var="value" in Clojure

J10:02:57

I think ! should be good!

delaguardo12:02:48

(def cfg (doto (Config.) (.setId "value")))

Roee Mazor14:02:55

Hi, I have this odd situation, I have a map with a list as a key and I want to remove the key and add it back with the elements of the list as the new keys:

(def m {[:a :b] 1})
=> #'ns.core/m
m
=> {[:a :b] 1}
(def wanted {:a 1 :b 1})
=> #'ns.core/wanted
(def m2 {[:a :b] 1})
=> #'ns.core/m2
(def m2 {[:a :b] 1
         :c 2
         [:d :e] "something"})
=> #'ns.core/m2
m2
=> {[:a :b] 1, :c 2, [:d :e] "something"}
(def wanted2 {:a 1 :b 1 :c 2 :d "something" :e "something"})
=> #'ns.core/wanted2
wanted2
=> {:a 1, :b 1, :c 2, :d "something", :e "something"}

Godwin Ko14:02:34

(reduce-kv (fn [m k v] (if (coll? k)
                         (reduce #(assoc %1 %2 v) m k)
                         (assoc m k v)))
  {}
  {[:a :b] 1, :c 2, [:d :e] "something"})

🙏 1
Godwin Ko14:02:45

may not be the optimal solution, but it works anyway 😉

Roee Mazor14:02:38

Thanks a lot! ❤️

1
Ben Sless15:02:09

Slight improvement is in the "else" case just return the map

🙏 1
quan xing15:02:03

what's mean with-open [^HikariDataSource ds (connection/->pool HikariDataSource db-spec) ^HikariDataSource or other ^String ^:dynamic What's mean

noisesmith15:02:38

@imxingquan as @dpsutton notes those are mostly type hints, but ^:dynamic for example is a metadata provided to def / defn which changes the rules for var evaluation - the general term here is metadata, see for example with-meta - type hints are provided as metadata on the binding symbol, but there are a few other things done via metadata too

noisesmith15:02:31

@imxingquan I think it's appropriate to "go meta" here 😄

(ins)user=> (doc with-meta)
-------------------------
clojure.core/with-meta
([obj m])
  Returns an object of the same type and value as obj, with
    map m as its metadata.
nil
(ins)user=> (doc meta)
-------------------------
clojure.core/meta
([obj])
  Returns the metadata of obj, returns nil if there is no metadata.
nil
(ins)user=> (-> #'with-meta meta :doc)
"Returns an object of the same type and value as obj, with\n    map m as its metadata."

Alex Miller (Clojure team)21:02:41

If anyone has beginner opinions here, would be happy to hear them https://clojurians.slack.com/archives/C03S1KBA2/p1644956810077679

Roee Mazor10:02:58

not having a debugger 😄