Calling a Clojure function with string inside swap?

The macro, transform!, as defined below seems to work for => (transform! ["foo" 1 2 3]). The purpose is to take in a list, with the first element being a string that represents a function in the namespace. Then wrapping everything into swap!.

The problem is that transform! doesn't work for => (transform! coll), where (def coll ["foo" 1 2 3]). I am getting this mystery exception:

#<UnsupportedOperationException java.lang.UnsupportedOperationException: nth not supported on this type: Symbol>

The function:

(defmacro transform! 
" Takes string input and update data with corresponding command function.
"  
  [[f & args]]  ;; note double brackets
 `(swap! *image* ~(ns-resolve *ns* (symbol f)) ~@args))

I find it strange that it works for one case and not the other.

Answers


Macros work at compile-time and operate on code, not on runtime data. In the case of (transform! coll), the macro is being passed a single, unevaluated argument: the symbol coll.

You don't actually need a macro; a regular function will suffice:

(defn transform! [[f & args]]
  (apply swap! *image* (resolve (symbol f)) args)))

Resolving vars at runtime could be considered a code smell, so think about whether you really need to do it.


Need Your Help

xmltasks get all child elemets

xml ant xmltask

We have an XML file test.xml like:

npm “error network getaddrinfo ENOTFOUND”

node.js express npm

I am trying to install different modules through npm.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.