On Mon, Oct 22, 2012 at 2:30 PM, larry google groups
<lawrencecloj...@gmail.com> wrote:
>
>
> And the core.clj looks like this:
>
> (ns who-is-logged-in.core
>   (:gen-class)
>   (:import (java.util Date))
>   (:require clojure.string clojure.java.io who-is-logged-in.memory_display
>             [clojure.data.json :as json])
>   (:use   [net.cgrand.moustache :only [app delegate]]
>           [ring.util.response]
>           [ring.middleware.params]
>           [ring.adapter.jetty :only [run-jetty]]))
>
> (def registry (atom {}))
>
> (defn add-to-logged-in-registry [this-users-params]
>   "We assume some user is looking at a site such as wpquestions.com and the
> Javascript on that site is sending an Ajax request to this app, every 10
> seconds, with a map of information about the user, which we need to store in
> the registry."
>   (let [right-now (. (Date.) getTime)

Please do (System/currentTimeMillis) instead of allocating new Date
objects over and over.


>         new-user-entry (conj this-users-params { "updated" right-now })]
>     (if-not (nil? (get new-user-entry "username"))
>       (swap! registry assoc (get new-user-entry "username")
> new-user-entry))))
>
> (defn is-current? [this-users-map]
>   "If we have not received an Ajax request from a user during the last 15
> seconds then we can assume they have left the site. We need to remove them
> from registry."
>     (let [most-recent-ping (get this-users-map "updated")
>           allowed-cutoff (- (. (Date.) getTime) 15000)]
>       (if (> most-recent-ping allowed-cutoff)
>         true
>         false)))
>
> (defn remove-old-registrants []
>   "The registry should only show people who have pinged this app during the
> last 15 seconds. We rebuild the registry with only those users whose maps
> return true from is-current?"
>   (def updated-registry {})

This def is too ugly to live, and possibly a memory leak. Is it also
always empty?

>   (swap! registry (fn [map-of-all-user-maps]
>                     (into {}
>                           (doall

(Not likely to be a problem, but "into " and "doall" are redundant here.)

>                            (for [[username-as-key each-user-map]
> map-of-all-user-maps :when (is-current? each-user-map)]
>                              (conj updated-registry {(get each-user-map
> "username") each-user-map})))))))

I think that what you are doing here is something like the following?:

(into {} (keep #(is-current? %2) map-of-all-user-maps)

>
> (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)]
>     (add-to-logged-in-registry this-users-params)
>     (remove-old-registrants)
>     (response (apply str (json/write-str @registry)))))

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