Fork me on GitHub
#clojure
<
2022-03-22
>
Ben Sless07:03:12

Before I go stare at decompiled code for a while, how well do primitive functions compose? I have a function which returns a primitive function, and I want to compose a bunch of them, but the composition will also need to take this into account, otherwise the argument will be boxed back

Alex Miller (Clojure team)13:03:07

As long as the compiler knows that these are primitives coming in/out, that should work

Alex Miller (Clojure team)13:03:59

But it may depend on the exact scenario

Ben Sless13:03:22

I can verify it doesn't Given

(defn primf [^long x ^long y] (fn [^long z] ,,,))
(reduce (fn [f' f] (fn [^long x] (or (f x) (f' x)))) (map (fn [[x y]] (primf x y)) pairs-of-longs))
x is boxed before being passed around to f in the reducing function, 😞

Alex Miller (Clojure team)13:03:38

higher order functions are always boxed

Alex Miller (Clojure team)13:03:23

loop/recur (or macros that produce one like areduce etc) are really the only way to stay with primitives for something like this

Ben Sless14:03:57

Well, not the only way, but I cheated hard

Ben Sless14:03:11

After all, functions are just objects, and who am I to deny their nature? :thinking_face:

Ben Sless16:03:22

That's similar to what I ended up doing

p-himik17:03:23

There already exists "Rich Already Answered That!". Maybe there should be "Kevin Already Made a Gist on That!". :D

Ben Sless18:03:03

There's a thesis for a reflective tower of interpreters as a compiler for clojure just waiting to be written

helios11:03:20

What's a good way to add a docstring to a defmethod , aside from a comment in the line below the defmethod ?

helios11:03:51

I've seen (defmulti ^{:doc "some doc} foo ...) but not an example with defmethod

helios11:03:15

I could do the same, of course it doesn't show up when calling (doc foo) :thinking_face:

delaguardo11:03:39

hint ) look at arglists of defmulti

:arglists '([name docstring? attr-map? dispatch-fn & options])

delaguardo11:03:15

so you can add docstring placing a string right after the name

helios11:03:36

@U04V4KLKC yes but that's for defmulti, not defmethod

delaguardo11:03:33

my bad, sorry )

delaguardo11:03:47

so you are looking for a way to add a "comment" to defmethod? asking because docstring make sence only attached to var

delaguardo11:03:04

and for multimethod the var is the one declared via defmulti. defmethod is for mutating it.

p-himik11:03:02

Yes, since defmethod doesn't make a new var, there's nothing to attach the docstring to. But you can make that var yourself and attach the docstring to that:

(defmulti f identity)

(defn f-x
  "f-x does f on x"
  [x]
  ...)

(defmethod f :x [x] (f-x x))
But unless you really need a docstring for some reason (docs generation, code discovery, automatic tests, whatever else), I'd stick to a plain comment.

helios11:03:22

thanks 🙂 that clarifies my thought process

delaguardo11:03:27

@U2FRKM4TW you can achieve the same via (defmethod f :x "f-x does f on x" [x] ...) scratch that ) I was wrong

helios11:03:54

maybe with (defmethod ^{:doc "f x does f on x"} f :x [arg] ...)

p-himik11:03:09

You cannot. There are no vars involved with defmethod.

👍 1
Ferdinand Beyer12:03:08

It is actually a feature 🙂 After all, when calling a multi-method, you should regard it as just a method and don’t depend on implementation details such as the individual method implementations.

😃 1
diego.videco18:03:28

With a deps.edn setup, is there a configuration to have the repl automatically load a namespace in every file (just like the user ns)?

noisesmith18:03:57

not really what you're asking but related: (apply require clojure.main/repl-requires) (notice nothing is quoted) - this will put all the usual stuff that you get in the initial ns (pprint, doc, source, javadoc, etc.) into the current ns

Alex Miller (Clojure team)18:03:17

not really built-in, but you can customize your repl any way you like doing something like https://insideclojure.org/2020/02/11/custom-repl/

noisesmith18:03:23

interesting, running a require before evaluating every repl input

diego.videco20:03:19

That looks like just what I need @U064X3EF3, but I’ve been trying to put it into my deps file under :aliases :dev without success (using cider), seems like perhaps cider’s connection overwrites the :main-opts (?)

Alex Miller (Clojure team)20:03:55

hard for me to guess how it might interact with cider or what those aliases do

diego.videco20:03:24

The aliases just add :extra-paths and cider’s connection string looks like this:

/opt/homebrew/bin/clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version "0.9.0"} cider/cider-nrepl {:mvn/version "0.28.3"} refactor-nrepl/refactor-nrepl {:mvn/version "3.4.1"}} :aliases {:cider/nrepl {:main-opts ["-m" "nrepl.cmdline" "--middleware" "[refactor-nrepl.middleware/wrap-refactor,cider.nrepl/cider-middleware]"]}}}' -Mdev:test:cider/nrepl
so it adds it’s own :main-opts

dpsutton20:03:20

main opts cannot concatenate. CIDER needs nrepl’s main to start the nrepl server. But nrepl doesn’t play nice with subrepls clojure.main repls anyways so it won’t work after solving the main opts problem

dpsutton20:03:47

highlighting: :aliases {:cider/nrepl {:main-opts ["-m" "nrepl.cmdline" "--middleware" "[refactor-nrepl.middleware/wrap-refactor,cider.nrepl/cider-middleware]"]

dpsutton20:03:01

not sure if it is directly related to this but each evaluation in nrepl calls clojure.main/repl for the form sent: https://github.com/nrepl/nrepl/blob/master/src/clojure/nrepl/middleware/interruptible_eval.clj#L84

diego.videco20:03:40

I see @U11BV7MTK. Is there a way to solve the main problem in cider, i.e. autoloading a certain ns everywhere?

dpsutton21:03:00

i like things like a namespace called on an added classpath root

dpsutton21:03:07

then require once and then use fqn

Alex Miller (Clojure team)21:03:34

personally, I find this kind of stuff to be too much magic for me to remember - if I want something, I just require it

diego.videco21:03:01

Your probably right. Thanks for the responses.

noisesmith21:03:53

for a while I had a dev function in my user ns, so I could call (user/dev) to do all the requires/ refers in the current ns, but nowadays I just require as needed (including frequent usage of (apply require clojure.main/repl-requires) )

dpsutton21:03:41

i have the apply require clojure.main/repl-requires bound to C-c h and it evaluates that snippet. makes each namespace an IDE 🙂

diego.videco21:03:39

Great ideas, thanks!

Noah Moss23:03:04

I’m running into a File name too long error when using clojure.core.match/match with a large number of clauses. I assume I’m hitting https://clojure.atlassian.net/browse/CLJ-1852. Is there any workaround for this?

😞 2
hiredman23:03:27

If you don't aot compile, no files are written to disk, so you don't need to worry about filesystem file name length limits

Noah Moss23:03:39

that’s not an option for me, unfortunately…must AOT

vemv03:03:20

AOT in ci?