在Clojure中并置Java实例方法

Chr*_*utz 1 java clojure

是否可以juxt在Clojure中与Java对象的方法结合使用?

基本上我想要实现的是

((juxt .method1 .method2) myinstance)
Run Code Online (Sandbox Code Playgroud)

with .method1method2是实例方法myinstance,是一些Java类的实例.

谢谢你的帮助!

lee*_*ski 5

或者只是为此创建一个宏,它将正常juxt行为与.method行为结合起来.像这样的东西:

user> (defmacro juxt+ [& fns]
        (let [x (gensym)]
          `(fn [~x] ~(mapv #(list % x) fns))))
#'user/juxt+
Run Code Online (Sandbox Code Playgroud)

例如:

(juxt+ .getName (partial str "string val: ") .getAbsolutePath vector)
Run Code Online (Sandbox Code Playgroud)

扩展到以下内容:

(fn*
  ([G__19829]
    [(. G__19829 getName)
     ((partial str "string val: ") G__19829)
     (. G__19829 getAbsolutePath)
     (vector G__19829)]))
Run Code Online (Sandbox Code Playgroud)

在repl中:

user> ((juxt+ .getName 
              (partial str "string val: ") 
              .getAbsolutePath 
              vector) 
         (java.io.File. "aaa"))

["aaa" 
 "string val: aaa" 
 "/Users/.../aaa" 
 [#object[java.io.File 0x34c3af49 "aaa"]]]
Run Code Online (Sandbox Code Playgroud)