Fork me on GitHub
#clojure
<
2021-05-18
>
Ben Sless08:05:12

What's the reason loop forms inside let bindings get compiled to a function + instantiation + call? Can't they be inlined?

noisesmith13:05:26

wouldn't that mean reimplmenting binding, destructuring, etc.? as it is currently recur inside a function acts identically to a loop with the same binding list, in order to preserve that you'd need two different implementations agreeing on that behavior

grzm13:05:47

With clojure.tools.logging, can the clojure.tools.logging.impl/disabled-logger-factory be set via a Java system property (e.g., -Dclojure.tools.logging.factory=clojure.tools.logging.impl/disabled-logger-factory, per https://github.com/clojure/tools.logging/#selecting-a-logging-implementation)? It doesn’t look like it from some noodling, but perhaps I’m doing it wrong.

noisesmith14:05:48

it asks for a function should return a logger factory when called with no args, you are providing the logger factory itself

noisesmith14:05:50

try replacing clojure.tools.logging.impl/disabled-logger-factory with (constantly clojure.tools.logging.impl/disabled-logger-factory)

noisesmith14:05:32

that's a very weird API in a very java way, asking for a thunk that returns a factory

noisesmith14:05:42

I figured this out from your error: it tried to call the factory (created via reify which means the message printed is a bit obfuscated) as if it were a function. the docs you linked to specify that it wants a function as well.

grzm14:05:41

cheers. I was thrown by the name: the example in the docs uses -Dclojure.tools.logging.factory=clojure.tools.logging.impl/slf4j-factory, which indeed is a function with no args that returns a logger. As you rightly point out, disabled-logger-factory is not.

grzm14:05:52

I’m getting a dependency error. Could not resolve namespace for "user/my-logger-factory". Either it does not exist or it has a (circular) dependency on clojure.tools.logging. I appreciate the help. I’m just placing this here in case someone else tries something naïve like this and assumes that what I did worked.

noisesmith14:05:00

does it change if you add an ns declaration to the file? seems like this lib is doing some black magic with namespaces

noisesmith14:05:03

also I see that you are still misconfiguring the logger in the quiet-log function by providing a factory instead of a function returning a factory

grzm15:05:01

I think the binding in quiet-log is correct: (a) it’s working, and (b) *logger-factory* is an instance satisfying clojure.tools.logging.impl/LoggerFactory not a function.

grzm15:05:12

I did get this working by adding a namespace.

grzm15:05:13

Yeah, doesn’t seem to work if the factory is in the same namespace:

Ben Sless16:05:10

Today I realized eductions can be used to serialize directly to json

(defn iterduction
  [xf coll]
  (.iterator ^Iterable (->Eduction xf coll)))
Jackson ObjectMapper can take an iterator

hlship20:05:40

I'm finally getting started using deps and I have a question: when invoking clj to start a REPL, is there a hook or configuration to invoke an arbitrary function as part of setup. In my case, I'd like to have a :pretty alias that adds Pretty to the classpath, and invokes io.aviso.repl/install-pretty-exceptions before the REPL starts accepting user input.

hlship20:05:32

{:aliases {:pretty {:extra-deps {io.aviso/pretty {:mvn/version "1.1"}} :exec-fn clojure.main/repl :exec-args {:init io.aviso.repl/install-pretty-exceptions}}}

hlship20:05:23

... is close, but fails (because clojure.main/repl is a varargs function). Also, what if I want multiple init functions? Is there an alternate repl starter for deps that supports this?

borkdude21:05:31

@U04VDKC4G -X only accepts functions that has a map argument (or in clojure 1.11 a keys-destructured args list)

Alex Miller (Clojure team)21:05:37

in the last part of that, you don't need the commas anymore, you can just use normal spaces

borkdude21:05:41

@U04VDKC4G This seems to do the trick as well:

{:aliases
 {:pretty {:main-opts ["-e" "((requiring-resolve 'io.aviso.repl/install-pretty-exceptions))"
                       "-e" "(clojure.main/repl)"]
           :extra-deps {io.aviso/pretty {:mvn/version "1.1"}}}}}

👍 3
borkdude21:05:55

clojure -M:pretty

hlship22:05:16

This is cool ... to a point. For example, I like to have my pretty exceptions enabled when running tests but the above is only for starting a REPL; I'm either have to create my own test runner, or use one that makes use of Pretty as well.

hlship22:05:15

Just feels like there's room for an idea of "Scafollding" the environment prior to the main execution (of a REPL, of a test runner, of an arbitrary function). But that brings in questions about ordering and such ...

borkdude22:05:55

I guess the user namespace is a spot where you can apply this kind of init code?

Adam Kalisz21:05:58

@hiredman @ptaoussanis I have closed the issue and submitted a PR (linked in the issue comment): https://github.com/ptaoussanis/sente/issues/389 this seems to enable a workaround for us.

borkdude21:05:25

Are there any illustrating examples of transit/write-handler that use a function for the tag and rep and some of the other options? Typically I see (write-handler "foo" (fn [o] ...)) which makes me wonder what other stuff you can do with it. The documentation is pretty terse in my opinion. In which cases do you need the tag arg to be a function?

borkdude21:05:09

I did read that, but it also doesn't show an example of "more advanced" usage

borkdude21:05:30

I guess the fn lets you decide the tag based on something other than the type because you can inspect the value before it's written

borkdude21:05:53

But I never saw an example of that so far

borkdude21:05:29

Perhaps this comes in useful in the :default-handler

borkdude21:05:54

which btw is not documented at all (or I didn't look hard enough)

Alex Miller (Clojure team)22:05:36

I don't have time to dig in or say more useful things right now, but I may have some time end of week to look at it

borkdude22:05:06

appreciate the help, thanks

borkdude17:05:07

it's a bit unfortunate that the API requires to repeat logic like (.isArray ...) twice, one for the tag and once for the value which I can imagine isn't so good for performance, but ah well

bringe22:05:56

Is there something similar to Postman (or can Postman somehow be used) for testing requests to a Clojure web service that accepts/expects transit? For example, say my frontend and backend both use edn, but send transit+json over the wire. I'd like a tool that will: 1. Allow me to input edn and it will format and highlight it, as Postman does with json 2. Convert the edn to transit+json before sending the request 3. Read transit+json from the response and convert it to edn, and also prettify it as in step 1 I know I can write up some code to do this in a repl, and maybe that's even better (I've already done a bit of this), but I'm just wondering if some tooling around this exists.

borkdude22:05:35

@brandon.ringe This is pretty easy to do with babashka (can do http client + server, has transit, edn and json). There is also a puget CLI for colorization https://github.com/borkdude/puget-cli

borkdude22:05:04

You could even write a small web app which does this for you, to make a nice UI (not sure about the colorization in that case, I'm sure there is some JS plugin which can do basic highlighting)

bringe22:05:49

Oh, interesting. Thanks.

borkdude22:05:57

@brandon.ringe There is also jet which can do EDN -> Transit and vice versa on the command line for you as well.

👍 3