aaa ok I see what you mean...well, yes everything starting with def can be considered a 'global' var or even better a namespaced var (not really a variable in the traditional sense)...In Noir's case most of the atoms are maps so it could be that each Noir app registers its specific routes on a specific key in the map...wildly guessing here...perhaps someone else can provide more details...

In general however, this is how you would model small pieces of state in Clojure...for example, designing a gui includes a lot of state usually...so for example in most of my guis i have an atom at the top which holds a map with all he stateful aspects of the gui...but you're right - it's generally best to limit the amount of state in your program...

Jim

ps: defonce is not really related with singletons...there was a clojure.contrib ns for singletons wasn't there?


On 12/09/12 15:54, the80srobot wrote:
Sorry I wasn't more clear - I'm not talking about defonce, but about having refs (atoms) defined at the top level. When they're defined like this, they basically become global variables, don't they? Having two Noir apps would then mean they share the same routes, among other things. Or am I missing something?

On Wednesday, September 12, 2012 4:49:34 PM UTC+2, Jim foo.bar wrote:

    defonce simply means "do not bind the var if it already has a root
    value"... I think it only applies when reloading your
    namespace...to be
    honest, I'm not sure why Chris has coded it like that (creating an
    atom
    is not really expensive), but i find it hard to believe that you
    can' t
    have 2 Noir apps on the same project...basically defonce it is the
    same
    as def unless there is already a root value for that var...

    can anyone else add anything?

    Jim


    On 12/09/12 15:30, the80srobot wrote:
    > Hello,
    >
    > So I've been working on a project at work, that required me to
    code a
    > simple web interface. I considered going with Noir, and while
    reading
    > the code, I noticed a pattern that seems to repeat throughout
    most of
    > the code that Chris Granger has published in Clojure. This is
    what I'm
    > referring to:
    >
    >     ; these are at the top level in (ns noir.core)
    >     (defonce noir-routes (atom {}))
    >     (defonce route-funcs (atom {}))
    >     (defonce pre-routes (atom (sorted-map)))
    >     (defonce post-routes (atom []))
    >     (defonce compojure-routes (atom []))
    >
    > Now, I am new to Clojure, but I am not new to (functional)
    programming
    > and I'd like to think that I know a singleton when I see one. Is
    that
    > really what these are? If I'm right then defining your 'globals'
    (for
    > lack of a better word) like this would mean, among other things,
    that
    > you really can't have two independent Noir apps defined/running
    in the
    > same project - is that a correct assessment?
    >
    > Can someone more experienced shed some light on why it's done this
    > way? My experience in functional programming has taught me to
    always
    > limit my scope - I would think that either using thread-local
    bindings
    > (and then rebinding them to child threads) or relying on lexical
    scope
    > would be preferable to polluting the global state. Is this a
    Clojure
    > best practice?
    >
    > Thanks. I'm looking to use Clojure a lot at work, and I'm trying to
    > really understand the language before I throw it our production
    problems.
    >
    > ~Adam
    > --
    > You received this message because you are subscribed to the Google
    > Groups "Clojure" group.
    > To post to this group, send email to clo...@googlegroups.com
    <javascript:>
    > Note that posts from new members are moderated - please be patient
    > with your first post.
    > To unsubscribe from this group, send email to
    > clojure+u...@googlegroups.com <javascript:>
    > For more options, visit this group at
    > http://groups.google.com/group/clojure?hl=en
    <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

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