Fork me on GitHub
#clojure
<
2022-07-25
>
Carlo09:07:32

I'd like to provide a data-point on why I would find enabling :jvm-opts even outside of aliases (a previous discussion with @alexmiller mentioned that this feature lacked a compelling use-case). In the spock library, we need to interface with the underlying swi-prolog bindings, and to do that on linux, we symlink a .jar and an .so file from the swi distribution in the resources folder, and then invoke the program with -J-Djava.library.path=resources/. Since this library deals with prolog, I always want this flag to be set when using this package, so I either have to customize the aliases I start my cider repl or add a special invocation. While this is easy enough, it then becomes dependent on the peculiar way in which I use my editor, and so it's more difficult to explain the steps to a user which wants to try the library. If a new user doesn't use my setup, she now has to learn how to customize her editor so that it consistently does the right thing. Conversely, letting us set this flag globally wouldn't require any extra step for the user, and would be easily documented. That said, is there a workflow here I'm missing, and maybe the problem could be solved in another way without having to customize the repl invocation? Is there a change in perspective that I would need?

2
teodorlu13:07:41

is there a provided numeric Clojure type that can hold metadata?

(with-meta 123 {:unit :m})
;; errors with:
;; 
;;   class java.lang.Long cannot be cast to class clojure.lang.IObj
;;   (java.lang.Long is in module java.base of loader 'bootstrap';
;;   clojure.lang.IObj is in unnamed module of loader 'app')

(with-meta (Integer. 1) {:unit :m})

(with-meta
  (Long. 1)
  {:unit :m})
;; errors with:
;;
;; 1. Unhandled java.lang.ClassCastException

p-himik13:07:56

Clojure provides only 2 numeric types - BigInt and Ratio. And they don't support metadata.

👍 1
teodorlu13:07:35

Thanks! (though not the answer I was hoping for 😄 )

teodorlu13:07:43

@U2FRKM4TW have you got any experience with numeric types that are not provided by Clojure?

p-himik13:07:02

Only the ones that are provided by Java. :D

teodorlu13:07:52

Gotcha — thanks!

Joshua Suskalo17:07:49

@U3X7174KS Clojure does not support extensible numeric types. If you need a number with meta, I honestly recommend just sticking it in a single-element vector.

👍 1
teodorlu17:07:16

I was looking for something that works out of the box with +, *, etc. I guess I could either try to extend the built-in math operators for my new thing, or provide myns/+, myns/*, ...

Joshua Suskalo18:07:04

Right, but that's what I mean. If you attempt to extend the existing mechanisms on a new type it won't be a good time. You'll have to get very familiar with the way that ops are chosen. That code isn't too hard to understand, but it isn't designed in a way that makes it particularly achievable to have new types, and especially not ones that follow the commutative property on any operations.

👍 1
wombawomba17:07:29

What's the fastest (reasonable) way to compute an (integer) hash (for .hashCode) for a byte array of length 20?

wombawomba17:07:08

I've tried a number of different approaches, and thus far the fastest approach seems to be to extract 5 ints (using ByteBuffer) and xor those, which is... surprising

ghadi17:07:27

(java.util.Array/hashCode your-array)

ghadi17:07:52

you'll want to type hint as that method is overloaded

ghadi17:07:03

(java.util.Array/hashCode ^bytes your-array)

wombawomba17:07:24

hmm, I did try that and it was actually slower

wombawomba17:07:30

I forgot to type hint it though

wombawomba17:07:37

so I guess that's probably it 🙂