Fork me on GitHub
#clojure
<
2019-11-03
>
michaelteter01:11:07

Hi. Sorry that I learn a little Clojure, then life takes me away and I forget. So I'm here with a remedial question 🙂. I want to define a function that can take separate args or a vector and another arg. For example, the two ways I would like to be able to call foo are: (foo color num) (foo r g b num). Is there some clean way using destructuring that I can define the function?

michaelteter01:11:56

I think I could rearrange the args to do (foo num & color), but in this case I think the color vector (or 3 separate args) should be first.

michaelteter02:11:12

I know I can do this below, but I think there must be a nicer way...

(defn foo
  ([color num] (foo (nth color 0) (nth color 1) (nth color 2) num))
  ([r g b num] ... do the normal stuff))

manutter5102:11:46

You can nest destructuring

manutter5102:11:24

(defn foo
  ([[r g b] num] (foo r g b num))
  ([r g b num] ... do the normal stuff))

michaelteter02:11:17

Ahh yes, that's what I was looking for. Thanks!

michaelteter02:11:38

so rather than accepting a vector called "color", I take [r g b] and it is destructured for me.

michaelteter02:11:45

although I guess I could also do it the other way around, where the first variant is [r g b num] and it calls (foo [r g b] num)

michaelteter02:11:56

but what you described is what I was trying to remember.

andy.fingerhut03:11:39

I know how in Java (and from Clojure) to access fields or methods in another class that have been declared private, using the setAccessible method and the Java reflection API. I have some code declaring a private inner class, which has methods in it that I'd like to call on an instance of that private class, but I haven't found a way to do it yet, despite a bit of Google searching. Anyone know?

hiredman03:11:56

I am surprised if the reflection thing didn't just work, I didn't think methods got transformed at all

hiredman03:11:55

If I recall, javac turns inner classes into just classes, adding an extra parameter to the constructor to take a reference to the parent class. So if you are constructing the inner object yourself you'll need to pass in the outer. And I guess the class name will contain some dollar signs

andy.fingerhut08:11:57

I've got an instance of the class that I just called (class inst) on, and I can call getDeclaredMethod on that class and the method name, but then the invoke fails when called with an instance of that class and the Method object.

murtaza5205:11:10

(Long/toHexString -126)
;; => "ffffffffffffff82"
however 0xffffffffffffff82evaluates to a different base 10 number. Shouldnt both be the same ?

dpsutton05:11:46

> The unsigned long value is the argument plus 2^64 if the argument is negative; otherwise, it is equal to the argument.

murtaza5207:11:58

Given a byte of any length ex - 2r1111111111111111111111111111111111111111111111111111111110000010, how do I just get its last 8 bits 2r10000010.

andy.fingerhut08:11:20

(bit-and x 0xff)

andy.fingerhut08:11:05

And usually people use the word 'byte' for a quantity that fits within 8 bits, which the value you show above is much larger than.

murtaza5208:11:52

@andy.fingerhut thanks, had missed that bit-and can be used to cut off higher bits too.

Brandon17:11:47

it looks like deftype with :unsycnhronized-mutable fields makes those fields private. is there any way to make public mutable fields? I essentially want to write a mutable "struct" with 0 overhead compared to a Java class

Brandon17:11:36

to be even more specific, I'm dealing with a gen-class with a state that gets updated in a tight loop, so shoving a map in an atom/transient val is too much overhead - I want the state to be a mutable struct instead

andy.fingerhut17:11:10

Clojure does not strive to enable creating arbitrary Java classes from within Clojure. It embraces using Java source code when the Java class you want goes outside of the options that Clojure provides, then using that Java class from Clojure.

👍 8
andy.fingerhut17:11:21

If you have a multi-threaded program, even :unsynchronized-mutable has brief warning language in the Clojure docs that you should know what you are doing to enable thread safety in your code, e.g. you have ingested and keep in mind the relevant parts of a book like "Java Concurrency in Practice". The same warning apply if you make your own Java class with such fields in a multi-threaded program.

alexbaranosky21:11:55

@brandon268 I've never used this library, but might be worth checking out: https://github.com/ztellman/vertigo