Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to defn a function from string in Clojure?

I'd like to do this (in REPL or anywhere)

(defn (symbol "print-string") [k] (println k))

and then be able to do

(print-string "lol")

Or, if there is any other way to create defn from custom strings in macroses, could you push me into the right direction please?

like image 877
mannicken Avatar asked Mar 24 '09 19:03

mannicken


2 Answers

(defmacro defn-with-str [string args & body]
 `(defn ~(symbol string) ~args ~@body))

(defn-with-str "print-string" [k] (println k))

(print-string "lol")
like image 68
dnolen Avatar answered Oct 19 '22 07:10

dnolen


dnolen's solution works at macro expansion time, Brian Carper's at read-time. Now, here's one for run-time:

(intern *ns* (symbol "a") (fn [k] (println k)))
like image 38
Matthias Benkard Avatar answered Oct 19 '22 06:10

Matthias Benkard