This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-02-15
Channels
- # announcements (8)
- # architecture (9)
- # autochrome-github (1)
- # babashka (48)
- # beginners (55)
- # calva (36)
- # cider (16)
- # clj-commons (1)
- # clj-kondo (38)
- # cljs-dev (44)
- # cljsrn (1)
- # clojure (164)
- # clojure-europe (35)
- # clojure-nl (2)
- # clojure-norway (10)
- # clojure-uk (23)
- # clojurescript (50)
- # conjure (24)
- # core-async (1)
- # cryogen (2)
- # cursive (38)
- # datalevin (11)
- # datascript (2)
- # datomic (13)
- # duct (1)
- # emacs (16)
- # events (12)
- # exercism (3)
- # figwheel-main (7)
- # fulcro (26)
- # honeysql (5)
- # integrant (1)
- # jobs (3)
- # kaocha (6)
- # lsp (72)
- # malli (22)
- # nextjournal (35)
- # nrepl (1)
- # off-topic (34)
- # pathom (5)
- # polylith (8)
- # portal (40)
- # re-frame (14)
- # reagent (42)
- # reitit (1)
- # releases (1)
- # remote-jobs (1)
- # reveal (9)
- # sci (2)
- # shadow-cljs (13)
- # sql (3)
- # tools-deps (33)
- # vim (25)
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 ..}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?
I mean how do I verify the validity of record-data
data, such as what fields must be included in reocrd-data
@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.
(there are community alternatives to Spec that some people seem to prefer)
See https://clojure.org/guides/spec to get you started.
ok, thanks @seancorfield. and how can I print execute sql statement to console log in next.jdbc?
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.
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=?
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.
ok thank you @U0AT6MBUL
(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)
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
@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=>
(if you formatted your code examples and showed what you had tried in the REPL, you might get better answers)
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.
Awesome, thanks!
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
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.
(but be aware that keys
does not necessarily produce the keys in any specific order)
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)))
(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)
OK, I'm clear. thank you very much @seancorfield
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
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.are you looking to check if in nested maps, a specific key occurs in any of them?
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 leastQuestion - How come
(defn foo [x] (list x (+ x 2) (* x 2)))
and
(defn foo [x] '(x (+x 2) (* x 2)))
are not the same?Thanks, bit clunky.
The quote (`'`) suppresses evaluation:
user=> (def x 1)
#'user/x
user=> 'x
x
user=> x
1
(
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
@U4ZDX466T nothing much, just wondering
(def cfg (Config.))
(.setId cfg 123)
how can I write code like cfg.inst_var="value"
in Clojure(def cfg (doto (Config.) (.setId "value")))
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"}
(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"})
what's mean with-open [^HikariDataSource ds (connection/->pool HikariDataSource db-spec)
^HikariDataSource or other ^String ^:dynamic What's mean
@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
@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."
@imxingquan the official docs for metadata https://clojure.org/reference/metadata
If anyone has beginner opinions here, would be happy to hear them https://clojurians.slack.com/archives/C03S1KBA2/p1644956810077679
not having a debugger 😄