Fork me on GitHub
#clojure
<
2022-08-17
>
mbertheau09:08:06

In https://clojuredocs.org/clojure.core/doseq it says "doseq does not retain the head of the sequence." What does that mean?

p-himik09:08:53

The sequence's head might be garbage collected if there are no references to it.

1
rmxm10:08:01

Since clojure builds uber jars. I am wondering. I have 2 projects A, B. Project B depends on project A. Both project A and B have a dependency C, however in different version. Now, in project B I have C as managed dependency. To graph this crap out :): B -> A -> C (v1) B -> C (v2 as managed dep) I am noticing project A fails when being upgraded to use C(v2), will project B work correctly with a managed dependency? I think yes, as project A is uberjar providing its own deps (so the managed dependency is only for nonuberjar deps in dependencies). Right?

delaguardo11:08:55

uberjar is just an archive with all the source and necessary class files. when you add uberjar as a dependency there is a very high chance it will not work because those files might collide if root project trying to add different version of the same dep

djanus11:08:29

this ☝️ also if you need both A and B to coexist within the same jvm and each use their particular version of C, https://github.com/benedekfazekas/mrandersonhelps with that

rmxm11:08:42

Thanks a lot, how do that relate to shaded/unshaded. Like I have some bit of knowledge that java artifacts can be in both forms (shaded/unshaded).

respatialized15:08:27

Is there a way to evaluate / refer a Clojure namespace with "instrumentation" applied to each form? I'm wondering if I can evaluate a namespace in order and measure the execution time of every form without wrapping every single form in a (time form) call.

vemv15:08:47

I've done it in the past, I applied https://github.com/ptaoussanis/tufte over each public var of a given ns sadly it's closed-source but it shouldn't be hard to implement

respatialized15:08:21

Yeah I figured I could cobble together something using ns-publics; that requires me to have everything I want to measure bound to a var, but that's probably good practice for what I'm doing anyway for a fair number of reasons!

respatialized15:08:29

Only problem with that approach as far as I can see is that it may require two passes over a namespace that might have expensive computation in it (my use case is data science code): one to produce all the public vars, and a second to profile them.

vemv15:08:27

> that requires me to have everything I want to measure bound to a var not really, tufte can also work at the form level. You could read forms from a .clj file, walk over them, wrap them in tufte, and then eval each top-level form now the question is: do you really want that? you'd get an exhaustingly detailed result

vemv15:08:00

so it's better to start at the var level, and only focus on the slow vars, then split them, recur

respatialized15:08:30

it's not entirely about just identifying bottlenecks / hot spots. ML performance metrics + result coefficients might change on each eval too, so for those erring on the side of "exhaustingly detailed" is maybe OK

👀 1
vemv15:08:02

> Only problem with that approach as far as I can see is that it may require two passes over a namespace that might have expensive computation in it (my use case is data science code): one to produce all the public vars, and a second to profile them. Producing a public var is generally cheap - that's clojure compiler's job. Not data scientists' :) The profiling itself is very cheap - it's part of tufte's pitch So your code is compiled once, and ran once - just as usual

Kevin17:08:27

Hey all, if I wanted to run something like npx node-sass in a background thread within a .clj file, would that be possible with only using Java’s ProcessBuilder (https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html) or will I have to use Clojure’s future as well? Thanks!

rolt19:08:55

you don't need too, it will be a background process, not a jvm thread. You may want to spawn a thread that wait for completion though

thanks3 1
Colin P. Hill19:08:59

I remember seeing a project that's like core.specs.alpha, but maintained by the community and (at present) covering more functions. Anyone know what I'm talking about and where to find it?

Alex Miller (Clojure team)19:08:35

note that using it in all its fullness may have a significant performance impact

Colin P. Hill19:08:45

That's fine – I'm actually just looking at it for examples on how to write certain kinds of fdefs

Colin P. Hill19:08:05

And the ones I write will of course only be instrumented during development

Eugen20:08:09

what is the clojure way of generating a clojure file from xml? I use str now. I have db entities defined in xml. I want to build specs from them. write to file + format, comets. perhaps runtime only as well

rolt21:08:12

zprint has a lot of options and sane default. For code generation, just building the data structure and writing with pr should be enough. I don't know any code generation tool in clojure (except rewrite-clj but it could be too complex) if it's for runtime only, you can have a look at how hugsql does it with def-db-fns

Eugen10:08:16

thanks @U02F0C62TC1. I wonder how I can support comments. ;;

Eugen10:08:26

but it's not critical

Eugen12:08:47

yeah, it does, thanks for the link

kwladyka21:08:10

hey, do you have an example of the simplest “do action” when app detect is closing by killing docker container? I don’t need to detect reason. I mention about docker container to show the goal.

kwladyka21:08:47

(.addShutdownHook (Runtime/getRuntime) (Thread. #(println "shutdown")))
Something better, than this? Or it is the good one.

rolt21:08:27

that's the good one

👍 1
dpsutton21:08:03

I found this one recently:

clojure.repl/set-break-handler!
([] [f])
  Register INT signal handler.  After calling this, Ctrl-C will cause
  the given function f to be called with a single argument, the signal.
  Uses thread-stopper if no function given.
not sure if that could help

rolt21:08:45

not sure shutdownhook will work when killing container though

kwladyka21:08:22

I don’t know, but clojure.repl doesn’t sound like something to use in production application.

dpsutton21:08:38

read the source. it’s very simple.

👍 1
kwladyka21:08:39

> not sure shutdownhook will work when killing container though yeah and I am curious how much time the app have

dpsutton21:08:02

(defn set-break-handler!
  "Register INT signal handler.  After calling this, Ctrl-C will cause
  the given function f to be called with a single argument, the signal.
  Uses thread-stopper if no function given."
  ([] (set-break-handler! (thread-stopper)))
  ([f]
   (sun.misc.Signal/handle
     (sun.misc.Signal. "INT")
     (proxy [sun.misc.SignalHandler] []
       (handle [signal]
         (f (str "-- caught signal " signal)))))))
regardless of namespace that seems straightforward

kwladyka21:08:19

> 10s Is it serious answer?

rolt21:08:09

i think so, but it's configurable

kwladyka21:08:42

looks like it works. thank you

dpsutton21:08:48

Has anyone ever used clojure.repl/set-break-handler! ?

Alex Miller (Clojure team)21:08:19

pretty sure almost no has ever used that :) also, per https://ask.clojure.org/index.php/11576/clojure-repl-set-break-handler-bitrotted I think it does not actually work

dpsutton21:08:12

❯ clj
Clojure 1.11.1
user=> (clojure.repl/set-break-handler! (fn [sig] (println "received: " sig)))
#object[sun.misc.Signal$SunMiscHandler 0x773dab28 "java.lang.Terminator$1@af78c87"]
user=> received:  -- caught signal SIGINT
received:  -- caught signal SIGINT
received:  -- caught signal SIGINT
received:  -- caught signal SIGINT

user=> ;; i hit Ctrl-c for the above
user=> (System/getProperty "java.version")
"17.0.1"
user=>
/shrug

dpsutton21:08:43

i was sending some coworkers my favorites from there and was surprised by it in (dir clojure.repl)

lispyclouds22:08:31

I have been using it to clean up my servers: https://github.com/bob-cd/bob/blob/main/apiserver/src/apiserver/main.clj#L23 but now I'm doubting it. Works well for my needs.

lispyclouds22:08:07

Should I not be using it?

dpsutton22:08:49

ha. that’s like the one organic usage that http://grep.app found

SK22:08:15

Which fn splits up a collection by a predicate? By splitting I do not mean to cut it into 2 like split-with, but evaluate each element and return it in the according collection (similar to what split-with does). In the old clojure.contrib seq-utils, there was a fn named "separate", I'm looking for something like that (I've tried using group-by, but that is awkward, because if one of the lists would be empty, then the key is skipped completely, instead of having the key, but with empty collection)

dpsutton22:08:54

(answered too quickly with group-by sorry)

isak22:08:22

group-by shouldn't really be a problem if you use the normal clojure idioms, e.g., (if (seq (get results true)) :something ...)

dpsutton22:08:31

you could also make a little wrapper around group-by that wraps the pred in a (comp boolean f) so you always get back true and false. Can then return whatever structure you want: a vector of true coll and false coll, keep the map but ensure it has both true and false keys, etc

isak22:08:10

And or something like this if you absolutely must have all the possible keys:

(merge-with into 
  {true [] false []} 
  (group-by odd? [1 3 5]))

dpsutton22:08:56

I'd just merge not merge with. Seems extra work

1