Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Java object methods call results to Clojure map

Tags:

java

clojure

I'm new to Clojure and have created a simple macro to call certain methods on a Java class and return results into a Clojure map and this all works fine. (Also i'm aware of the bean function however the classes I'm using aren't beans).

My question is in regard to the calling code. Is it better form to pass the Java 'methods' as symbols?

I'm not even sure what type .toString is at the moment (in the calling code)? It all works but not sure its idiomatic Clojure.

(defmacro obj-to-map 
  [obj & body]
  (let [afn (fn [[method kw]]
              `(~kw (~method ~obj)))]
  `(assoc {} ~@(mapcat afn (partition 2 body)))))

(obj-to-map "hello" .length :length .toString :value)  

=>  {:value "hello", :length 5}
like image 797
tgrrr Avatar asked Jun 29 '26 02:06

tgrrr


1 Answers

The .toString etc are symbols in the calling code.

I think it would be better to pass the method name and invoke it using ".".

(defmacro obj-to-map 
  [obj & body]
  (let [afn (fn [[method kw]]
              `(~kw (. ~obj ~method)))]
  `(assoc {} ~@(mapcat afn (partition 2 body)))))

(obj-to-map "hello" length :length toString :value)  

=>  {:value "hello", :length 5}
like image 118
Alex Miller Avatar answered Jun 30 '26 15:06

Alex Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!