Last active
June 2, 2017 08:29
-
-
Save killme2008/2bab92c1910fe3998464dc406dfc3258 to your computer and use it in GitHub Desktop.
A macro to refer java static method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro sfn | |
"A macro that wraps java static method into a function, for example: | |
((sfn Integer/valueOf) \"1\") | |
(map (sfn Math/sqrt) [1,2,3]) | |
(map (sfn Math/pow.2) (range 1 10) (range 1 10)) | |
The java static method passed in must be in the form of | |
'Class/Method.arity'. If '.arity' is not present, it's 1 by default. | |
" | |
[f] | |
(let [[f arity] (clojure.string/split (str f) #"\.") | |
f (symbol f) | |
args (repeatedly (Integer/parseInt (or arity "1")) gensym)] | |
`(fn [~@args] | |
(~f ~@args)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment