Paste number 67182: jcall jfn

Index of paste annotations: 1

Paste number 67182: jcall jfn
Pasted by: rhickey
When:1 year, 11 months ago
Share:Tweet this! | http://paste.lisp.org/+1FU6
Channel:#clojure
Paste contents:
Raw Source | XML | Display As
(defn jcall [obj name & args]
  (clojure.lang.Reflector/invokeInstanceMethod obj (str name)
    (if args (to-array args) clojure.lang.RT/EMPTY_ARRAY)))

(defn jfn [name]
  #(apply jcall %1 name %&))

((jfn 'substring) "fred" 2 3)
((jfn 'toUpperCase) "fred") 

Annotations for this paste:

Annotation number 1: fn from method name without reflection
Pasted by: joubert
When:1 year, 11 months ago
Share:Tweet this! | http://paste.lisp.org/+1FU6/1
Paste contents:
Raw Source | Display As
(defmacro getFnFromMethodName
  "Given a 'methodName', returns a function that invokes that method."
  [methodName] `(fn [target# val#]
		  (. target# ~(symbol methodName) val#)))

Colorize as:
Show Line Numbers
Index of paste annotations: 1

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.