2010-10-03 6 views

Répondre

14

Une réponse simple:

(defn call [this & that] 
    (apply (resolve (symbol this)) that)) 

(call "zero?" 1) 
;=> false 

Juste pour le plaisir:

(defn call [this & that] 
    (cond 
    (string? this) (apply (resolve (symbol this)) that) 
    (fn? this)  (apply this that) 
    :else   (conj that this))) 

(call "+" 1 2 3) ;=> 6 
(call + 1 2 3) ;=> 6 
(call 1 2 3)  ;=> (1 2 3) 
28

Quelque chose comme:

(defn call [^String nm & args] 
    (when-let [fun (ns-resolve *ns* (symbol nm))] 
     (apply fun args))) 
+1

Merci, 'ns-resolve' dans particulier était ce que je cherchais. –

+6

il est préférable de combiner ns-resolve avec fn? pour vérifier, ce symbole est fonction –

Questions connexes