我能想到的最简单的事情是将其包装在函数中,但我不完全确定这是否是最好/最惯用的方式:
user> (apply (fn [a b] (Integer/rotateRight a b)) [0 0])
0
或者,稍短但等效:
user> (apply #(Integer/rotateRight %1 %2) [0 0])
0
或者,您可以为 java 方法调用创建适当的包装器函数:
(defn rotate-right [a b]
(Integer/rotateRight a b))
你可以这样使用它:
user> (apply rotate-right [0 0])
0
编辑:只是为了好玩,灵感来自iradik关于效率的评论,三种不同调用此方法的方式之间的时间比较:
;; direct method call (x 1 million)
user> (time (dorun (repeatedly 1E6 #(Integer/rotateRight 2 3))))
"Elapsed time: 441.326 msecs"
nil
;; method call inside function (x 1 million)
user> (time (dorun (repeatedly 1E6 #((fn [a b] (Integer/rotateRight a b)) 2 3))))
"Elapsed time: 451.749 msecs"
nil
;; method call in function using apply (x 1 million)
user> (time (dorun (repeatedly 1E6 #(apply (fn [a b] (Integer/rotateRight a b)) [2 3]))))
"Elapsed time: 609.556 msecs"
nil