the 2 previous responses answered your question perfectly ...I'm just a bit amazed that you would go away and write clojure code to consume JSON and all that, without realising that data-structures in Clojure are immutable! I think we can all agree they are *the* cornerstone of Clojure. It is a bit alien at first but it does pay off in the long run... If you absolutely need to stick with your code style (mutability not-recommended in general) use a java HashMap instead...

Jim

ps: i recently used a cheshire without any problems :-)

On 27/09/12 18:53, gaz jones wrote:
Couple of initial things, Clojure has immutable data structures so
when you call for example 'assoc' it will return you a new map with
the new values assoc'd. It will not mutate the original, so:

(let [foo {}]
   (assoc foo :a 1)
   (assoc foo :b 2)
   foo)

Will return {}. You need to do something like:

(-> {}
      (assoc :a 1)
      (assoc :b 2))

=> {:a 1 :b 2}

FYI, assoc takes multiple kvps:

(assoc {} :a 1 :b 2)

Also, to return valid JSON, you cannot simply call 'str' on the map.
You need to use a library like https://github.com/dakrone/cheshire or
https://github.com/clojure/data.json and encode the map as JSON.

Perhaps you could illustrate the data structure you are holding inside
of @registry, and the structure of the JSON you would like to emit.
Laziness is not an issue here.


On Thu, Sep 27, 2012 at 12:02 PM, larry google groups
<lawrencecloj...@gmail.com> wrote:
I would like 2 types of advice:

1.) an answer to this specific question

2.) advice on how one is suppose to debug mysteries likes this

I have a simple web app that serves some data (hopefully in JSON
format, but at the moment I will accept anything at all). The app uses
Ring and Moustache and outputs the data.

We start with a simple atom:

(def registry (atom {}))

We put some data in this atom. And then we output it. But I have had
great difficulty getting anything to appear on the screen. Assuming
the problem was with the fact the main sequence was lazy, I added in
doall everywhere it made sense. But I still can not get anything to
work:

(defn current-users [request]
   "The default action of this app. Add new users to the registry, and
delete the ones that are more than 15 seconds old"
   (let [this-users-params (:params request)
         final-map-for-output {}]
   (add-to-logged-in-registry this-users-params)
   (remove-old-registrants)
   (response (apply str (into {}
                              (doall
                               (map (fn [each-user-map]
                                      (doall
                                       (let [inner-details (second each-
user-map)]
                                         (assoc final-map-for-output
"username" (get inner-details "username" "nothing found for user"))
                                         (assoc final-map-for-output
"updated" (get inner-details "updated" "nothing found for updated"))
                                         final-map-for-output)))
                                    @registry)))))))

The various variations I have tried on this have either given me a
blank white page or:

{}

Nothing else.

I used to do simply:

   (response (apply str (doall @registry)))))

This worked fine. But it did not output valid JSON, so I wanted to
change the format. But I have not been able to get anything to appear
on screen.

Suggestions?

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