Re: Intern a var from outside namespace

2012-10-17 Thread David Jacobs
I can see the potential problems with this pattern, but it also seems like a 
nice way to metaprogram things like controllers or models in a web app. (In 
non-web Clojure dev, I haven't ever run into this issue.) Will have to think 
about this some more...

On Friday, October 12, 2012 at 11:33 AM, Stuart Sierra wrote:

 Sounds like a load-order issue. Make sure the code *creating* the 
 namespaces/vars is loaded before the code *using* them.
 
 But better yet, just don't do it. :)
 
 -S
 
 -- 
 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 
 (mailto: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 
 (mailto:clojure+unsubscr...@googlegroups.com)
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en 


-- 
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


Re: Intern a var from outside namespace

2012-10-12 Thread Arlen Christian Mart Cuss
On Friday, October 12, 2012 4:38:36 PM UTC+11, David Jacobs wrote:

 Having a map leads to pretty bad syntax for what I'm trying to do. That's 
 why I want to metaprogram here. 

 I want this ...

 (post/all api-key)

 … instead of this ...

 ((post/api :all) api-key) 


I'm not sure, but that second one looks pretty fine to me.  It's also a lot 
more obvious where the function comes from. 

-- 
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

Re: Intern a var from outside namespace

2012-10-12 Thread Stuart Sierra
Sounds like a load-order issue. Make sure the code *creating* the 
namespaces/vars is loaded before the code *using* them.

But better yet, just don't do it. :)

-S

-- 
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

Intern a var from outside namespace

2012-10-11 Thread David Jacobs
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

Re: Intern a var from outside namespace

2012-10-11 Thread Alan Malloy


On Thursday, October 11, 2012 9:03:38 PM UTC-7, David Jacobs wrote:

 I would like to create function names programmatically. So far, I have 
 code that works: 
 ...
 Where am I going wrong?

 David


Sentence one. Don't do it that way: namespaces are not very good hashmaps, 
but hashmaps are excellent at that. You already have the data that you need 
to make this handler, and there's no need to scatter it into dozens of 
functions: use that data to create a single function that does the 
dispatching logic that you need, and put that into your ring routes or 
whatever.

-- 
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

Re: Intern a var from outside namespace

2012-10-11 Thread David Jacobs
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

Re: Intern a var from outside namespace

2012-10-11 Thread David Jacobs
Having a map leads to pretty bad syntax for what I'm trying to do. That's why I 
want to metaprogram here.  

I want this ...

(post/all api-key)

… instead of this ...

((post/api :all) api-key)

Why can an ns not be passed around like anything else?

David  


On Thursday, October 11, 2012 at 9:18 PM, Alan Malloy wrote:

  
  
 On Thursday, October 11, 2012 9:03:38 PM UTC-7, David Jacobs wrote:
  I would like to create function names programmatically. So far, I have code 
  that works:  
  ...
  Where am I going wrong?
   
  David
  
 Sentence one. Don't do it that way: namespaces are not very good hashmaps, 
 but hashmaps are excellent at that. You already have the data that you need 
 to make this handler, and there's no need to scatter it into dozens of 
 functions: use that data to create a single function that does the 
 dispatching logic that you need, and put that into your ring routes or 
 whatever.
 --  
 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 
 (mailto: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 
 (mailto:clojure+unsubscr...@googlegroups.com)
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en  

-- 
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