Note: I wrote this quickly. I should've taken the time to point out that the 
only difference between the first and second functions is the parameterized 
namespace, a-ns. When I say it "doesn't work", I mean that accessing the 
created fn from a third ns throws a "No such var" exception. 


On Thursday, October 11, 2012 at 9:03 PM, David Jacobs wrote:

> I would like to create function names programmatically. So far, I have code 
> that works: 
> 
> (defn create [endpoints]
>   (doseq [{:keys [action method url]} endpoints]
>     (let [endpoint-fn (if (re-matches #".*/:id(/.*)?" url)
>                         (fn [id session]
>                           (method (string/replace url #":id" id) session))
>                         (fn [session]
>                           (method url session)))]
>       (intern *ns* (symbol action) endpoint-fn))))
> 
> 
> This creates functions from an array of hashes of the form {:action "index" 
> :method api/get :url "url"}.
> 
> But I want to move this to a namespace where I can call it from multiple 
> other namespaces. As soon as I replace this function with the following, it 
> stops working:
> 
> (defn create [a-ns endpoints]
>   (doseq [{:keys [action method url]} endpoints]
>     (let [endpoint-fn (if (re-matches #".*/:id(/.*)?" url)
>                         (fn [id session]
>                           (method (string/replace url #":id" id) session))
>                         (fn [session]
>                           (method url session)))]
>       (intern a-ns (symbol action) endpoint-fn)))) 
> 
> 
> ;; From other ns:
> (other-ns/create *ns* endpoints)
> 
> 
> Where am I going wrong?
> 
> David 

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to