Skip to content

Instantly share code, notes, and snippets.

@killme2008
Last active June 2, 2017 08:29
Show Gist options
  • Save killme2008/2bab92c1910fe3998464dc406dfc3258 to your computer and use it in GitHub Desktop.
Save killme2008/2bab92c1910fe3998464dc406dfc3258 to your computer and use it in GitHub Desktop.
A macro to refer java static method
(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