Fork me on GitHub

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?


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.


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))


You can nest destructuring


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


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


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


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)


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


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?


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


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


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.


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


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


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


(bit-and x 0xff)


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.


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


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


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


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

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.


@brandon268 I've never used this library, but might be worth checking out: