This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-05-25
Channels
- # announcements (9)
- # asami (69)
- # babashka (151)
- # babashka-sci-dev (34)
- # beginners (90)
- # cider (21)
- # clj-on-windows (17)
- # clj-otel (4)
- # cljsrn (5)
- # clojure (27)
- # clojure-austin (3)
- # clojure-europe (87)
- # clojure-gamedev (1)
- # clojure-nl (3)
- # clojure-norway (8)
- # clojure-poland (2)
- # clojure-uk (3)
- # clojured (10)
- # clojurescript (50)
- # core-async (73)
- # cursive (28)
- # data-science (2)
- # datomic (17)
- # etaoin (1)
- # honeysql (6)
- # introduce-yourself (3)
- # jobs (1)
- # joyride (12)
- # malli (5)
- # nbb (14)
- # off-topic (18)
- # pathom (4)
- # podcasts (2)
- # polylith (30)
- # project-updates (3)
- # re-frame (33)
- # reitit (1)
- # remote-jobs (13)
- # shadow-cljs (59)
- # sql (12)
- # tools-build (7)
- # xtdb (36)
In next.jdbc . I use sql select one record data from db. if the someone column is null in db. the next.jdbc not return the column that it is null. How can I return the all columns
table test: id cola colb colc
1 a null null
(jdbc/execute-one! connectable
["select * from test where id=?" id])
=> {:id 1 :cola a}
how can I get {:id 1 :cola "a" :colb nil :colc nil}
That's not the default behavior. There's an optional builder that omit nil
columns, but you have to choose that, e.g., via with-options
to create a connectable
with that :builder-fn
.
The default behavior, using the standard builder, is to return all columns, even if they're nil
. If you're getting :id
and :cola
as keys, you're using a non-standard builder.
(because the default builder would return :test/id
and :test/cola
from that select)
and passed in some modifying function?
Why? Why not use the default builder?
I forgot the code I wrote earlier, I think the next.jdbc omit nil columns default:grin:
Well, it was doing exactly what you were telling it to do 😸
Yes It's very easy understand. Although my English is pool. I've been experimenting with Clojure for real projects.
Suppose one wants to take a look at intermediate results when using something like the thread-first (->) macro using println
. How is this done best?
Context:
I’m working through Pedestal’s Getting Started guides. In http://pedestal.io/guides/hello-world-content-types, as part of refactoring a function doing multiple things into a series of functions, this function is defined:
(defn coerce-to [response content-type]
(-> response
(update :body transform-content content-type)
(assoc-in [:headers "Content-Type"] content-type)))
Where transform-content
is another function.
One approach that occurs to me is to rewrite the thread-first macro as a let
with multiple bindings, each binding fed into the next function, and use println
in the let
body before returning the desired result. Using this approach, the function above becomes something like:
(defn coerce-to [response content-type]
(let [updated-response-1 (update response
:body
transform-content
content-type)
updated-response-2 (assoc-in updated-response-1
[:headers "Content-Type"]
content-type)]
(println "INTERMEDIATE RESULT")
(println updated-response-1)
(println "RETURNED RESULT")
(println updated-response-2)
updated-response-2))
I’d appreciate any feedback on The Clojure Way™ to approach this. Thanks!Wow, that’s elegant. Thanks @U04V4KLKC!
tap>
could be used instead of println, especially if the values are data structures, inspecting those vales in Portal for example
https://github.com/djblue/portal
@U04V4KLKC sweet! I was fumbling around with different ways of doing that. Your version is much better. Thank you. Unrelated, but I noticed the other day too that you include parens around single argument functions when doing threading. Is there a reason you prefer to do it that way instead of omitting the parens? > A bare symbol or keyword without parentheses is interpreted as a simple function invocation with a single argument. https://clojure.org/guides/threading_macros#thread-last I think your snippet could be shortened down to:
(-> m
foo
(doto println)
bar
(doto println))
@UPWHQK562 parens make it easier to me to read through and refactor the code. if you are looking at the line foo
what do you see? A symbol. But if this symbol surrounded with parens - it is no longer just a symbol, it is function call
@U04V4KLKC thanks for explaining your reasoning. I agree, it does seem to add some clarity at the cost of a few harmless characters.
Hey @stefcoetzee I’m not sure about others, but I use https://github.com/cloojure/tupelohttps://github.com/cloojure/tupelospy function for this kind of debugging
I was looking at spy the other day from this library. How does it compare to using the tap> functionality?
Based on the docs (https://clojuredocs.org/clojure.core/tap%3E), tap> appears to only return a boolean. Spy, will always return its argument, even when nil.
Hello Guys, I want to build my 1st production REST microservice in Clojure. Can someone recommend me a framework where I can implement TDD, validation of requests easily.
It is simple to implement TDD with a REST microservice in Clojure, as the handlers should be clojure functions Suggest looking at • https://github.com/metosin/reitit (and reitit-ring) for a data-oriented way to route requests in the REST service • https://github.com/ring-clojure/ring & https://github.com/weavejester/compojure for a macro-style approach Pretty much all libraries follow the ring protocol for turning http requests into Clojure maps, so the test data for TDD is also Clojure maps https://github.com/ring-clojure/ring-mock also provides a simple way to generate mock requests (although these are clojure maps, so not essential,, but could be useful if this is the first Clojure webapp There is some general info about writing Clojure web services and some project examples at https://practical.li/clojure-web-services/ (I'm working on adding Reiti example soon)
Any thoughts on http://Pedestal.io , its interceptors looks quite interesting
Here is a sketch of what you will do to "mock" requests, going through two interceptors:
(:response (io.pedestal.interceptor.chain/execute
{:request {:request-method :post
:headers {"authorization" "nope"}}}
[preflight-shortcircuit-interceptor jwt-decoding-interceptor]))
Thanks for helping @U9E8C7QRJ @U05254DQM
pedestal and reitit can both use the interceptor idiom I believe.
What is the easiest way to colorise output of edn data in reagent/shadow-cljs? Eg. from this`[:pre (with-out-str (cljs.pprint/pprint edn-source))]` . Without eg. codemirror6 is that possible? (example puget lib (https://github.com/greglook/puget) , just for reagent) I didn’t find on Github, but maybe somebody saw one.. or know.
If I have an application with multiple uses of bigdecimals spreaded through the codebase, and wanted to specify a default precision everywhere, and avoid having to use binding math-context in every case (also for performance reasons), is it fine to alter-var-root math-context to a default one for all the application? Are there any downsides to this?
Or, related, does the binding of math-context propagate to threads created from the thread with the binding? (Could have a top-level binding to solve my use-case if this is the case)
it propagates if the threads are created via future
(+ some other built in mechanisms)
if you want a precision for your whole app, chances are doing the alter var root is A-ok
hello, now i want to download a file to local dir, then i use
(with-open [in ( uri)
out ( file)]
( in out))
but, in the server i can not download the file directly, so i need to download it with proxy, then how can i do?What is the best way to define a global constant at runtime? I want to read some coordinate data, set this value based on that data, then use it in functions that I run to process the rest of the data.
I created this function for making a closure, but within the function that calls it, should I just (def m-per-deg-lat (m-per-deg-lat-gen some-lat))
so I can use the global from then on? Or is a closure the wrong way to do this?
(defn m-per-deg-lat-gen
[phi]
(fn [] (+ 111132.92
(* -559.82 (q/cos (* 2 phi)))
(* 1.175 (q/cos (* 4 phi)))
(* -0.0023 (q/cos (* 6 phi))))))
Wait, are you suggesting using def
from within a function? I would recommend against that
Nope!
(def m-per-deg-lat (m-per-deg-lat-gen some-lat))
;; 👆 And then use this value in other namespaces
I think OP was just asking whether using def
was a good way of declaring “constant” values, to which I say yes! Defs are intrinsically constants.> within the function that calls it, should I just (def
Don’t do that.
If you really want to define a global constant, use def
without a value:
(def global-constant nil)
and then later change its value with alter-var-root
:
(alter-var-root #'global-constant (constantly the-value))
@U022T96EFV3 of course, def
is the way to go to define constant values, absolutely. But on top-level, not dynamically within other functions
Yes, I agree. I guess I didn’t think OP was asking that! But, upon closer inspection, it seems that he may be 🔍 ?
One issue with that approach is that you will lose some tool / IDE support @U032LAD66SF
Since references to the var will not resolve
…until that dynamic function is called
Alternatively, depending on where you get the phi
from, you might consider just wrapping your function in memoize
to avoid duplicate calculation
Hi, My checkouts folder suddently does not work , I am using Calva and for no reaosn when i use go to definition hotkey , it takes me to the uneditble source code for one of my libraries . When I change something in checkouts it does not matter because it looks like my main project does not see it's checkouts folder
Can you test if it works if you disable clojure-lsp? (You can do that from the clojure-lsp item in the statusbar.)
Hi , How are you ? Thanks for replying , I disabled and reenabled clojure-lsp but before that I changed directory to my checkouts folder and executed lein clean lein deps both on my main dir and my checkouts subdirs and it worked.
with core.memozie, is there a way to constrain a given cache to a maximum size in bytes? for my use case, i would like to have a TTL cache with a maximum size in bytes.
I don't know the answer but I'm curious to know if you have an effective algorithm to calculate how much memory allocates some generic object?
It's tricky but probably possible. You'd have to first find some sort of way to measure the size of (Java) data structures -- there are libraries out there that can do this to some degree or another, and I think there are some Clojure-specific ones, or at least useful wrappers.
Then you'd have to implement the core.cache protocol to create a new type of cache that was size-limited, based on that. Then you could compose that with a TTL cache (core.cache stuff is composable). And then finally provide that to core.memoize.
See http://clojure-goes-fast.com/blog/introspection-tool-object-memory-meter/ for example, for measuring data structure sizes.
for some more context, i have used this in java where you can specify a resource pool of a given size: https://www.ehcache.org/documentation/3.10/getting-started.html
ah, this might have some more details: https://www.ehcache.org/documentation/3.10/tiering.html#byte-sized-heap
If you do end up building a sized cache on top of core.cache
, I suspect it would be useful to others and would be a good library to have up on GitHub/Clojars. It couldn't be included in core.cache
itself (assuming it depended on something like clj-memory-meter since Contrib libs cannot have "external" dependencies). I'd be happy to answer Qs about it and possibly help maintain it if it does materialize!

@UDXEK491P they seem to be using their own library for that: https://github.com/ehcache/ehcache3/blob/8e830d04d543b93f3d79cad60c6a2c9fb90be47a/ehcache-impl/src/main/java/org/ehcache/impl/internal/sizeof/DefaultSizeOfEngine.java#L52 -> https://github.com/ehcache/sizeof/blob/master/src/main/java/org/ehcache/sizeof/SizeOf.java#L69
thanks @U06BE1L6T! that's helpful to see
They have 3 implementations of SizeOf it would be interesting to see how they compare in terms of performance and precision (between each other and e.g. clj-memory-meter) - I think the first two of them could be relevant • https://github.com/ehcache/sizeof/blob/6bf8d35c4c58768a28c1cc4b517565e12efff737/src/main/java/org/ehcache/sizeof/impl/UnsafeSizeOf.java#L104 • https://github.com/ehcache/sizeof/blob/6bf8d35c4c58768a28c1cc4b517565e12efff737/src/main/java/org/ehcache/sizeof/impl/ReflectionSizeOf.java#L85 • https://github.com/ehcache/sizeof/blob/6bf8d35c4c58768a28c1cc4b517565e12efff737/src/main/java/org/ehcache/sizeof/impl/AgentSizeOf.java#L83
I have a question about macros:
(defmacro m []
`(let [~'x 12] ~'x))
How come inside this macro, I get an error if I don't use ~'
? Isn't it always safe, because the let
creates a new binding?
Or what exactly is causing the spec error to trigger? I suspect I am completely misunderstanding things 😅what error are you seeing?
symbols are qualified in `
so the expansion is (let [user/x 12] user/x)
and let only supports binding unqualified symbols
let-bound symbols are local and never qualified
Thanks, that makes sense. So in the scenario I posted above, ~'
is the correct thing to do because the binding created will always be local. But in a different scenario, we might have to use #
to gensym the symbol so we dont accidentally evaluate something from the outer scope that we didn't mean to evaluate, is that right?
regular quote does not expand symbols so something like this would be fine:
(defmacro m [] '(let [x 12] x))
but of course, it also does not do unquote evaluation
Good day, What's the simplest way to add a sprinkle of clojurescript to an otherwise server side rendered clojure app?
The simplest way to add a little ClojureScript to a server side webapp would be to write the cljs code and compile it to JavaScript with cljs-build. Then include that JavaScript in the html (or hiccup) pages served by the server side app Shadow-cljs or figwheel-main are excellent tools, but probably overkill for the stated problem
The https://clojurescript.org/guides/quick-start should cover the needs of the stated problem
Reagent components are ClojureScript functions who are managed by the reagent render function, so yes they can be included in the same way
If the amount of ClojureScript grows, then there is an advantage of using figwheel-main. And if there is a need to include many JavaScript libraries via npm (many npm packages have many dependencies), then shadow-cljs support that approach very well
An alternate approach is to use #scittle https://github.com/babashka/scittle