Tornado-like async (web) server framework?

2012-04-15 Thread Stefan Arentz
There is a lovely little web server for Python called Tornado.

Tornado is an async server that also includes an async http client that plugs 
right in the server's event loop. This makes it really simple to build scalable 
web services that call other web services, which is what I mostly use it for.

I would love to do the same in Clojure but I have no idea where to start.

Ideally I would use an async server or framework in the style of Tornado or 
Twisted. But since Java has excellent thread support I guess I could also use 
an http lib that allows me to run requests in parallel.

Who has some hints or pointers?

 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


Re: Using Compojure's defroutes in a macro

2009-11-08 Thread Stefan Arentz


On 2009-11-08, at 3:40 PM, Richard Newman wrote:

>
> Try
>
> (defmacro mydefroutes []
>   `(defroutes my-routes
> (GET "/api"
>   (my-code ~'request

Brilliant! I learn something new every day :-)

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



Using Compojure's defroutes in a macro

2009-11-08 Thread Stefan Arentz


I have this Compojure code that works fine:

(defroutes my-routes
   (GET "/api"
 (my-code request)))

I want this code to be generated by a macro. My real code is more  
complex but the error is the same.

(defmacro mydefroutes []
   `(defroutes my-routes
 (GET "/api"
   (my-code request

When I run this code, I get the following error:

(foo)
No such var: webservice/request
   [Thrown class java.lang.Exception]

(webservice is the namespace of my code)

When I macroexpand the code I see this:

webservice> (macroexpand-1 '(foo))
(compojure/defroutes webservice/my-routes
   (compojure/GET "/api" (webservice/my-code webservice/request)))

I'm not sure what to do here.

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



Re: Functions and vars and meta-data

2009-11-08 Thread Stefan Arentz

Hi Alex,

Wow! Thank you so much for this excellent explanation! It totally  
makes sense now :-)

  S.

On 2009-11-07, at 9:46 PM, Alex Osborne wrote:

>
> Stefan Arentz wrote:
>
>> I must admin that I don't fully understand the difference between foo
>> and #'foo. That is probably why I'm making this beginner mistake :-)
>
> The difference takes some explanation.  So without further ado...
>
> Functions and Metadata: in Vivacious Gory Detail
> 


--~--~-~--~~~---~--~~
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: Functions and vars and meta-data

2009-11-07 Thread Stefan Arentz


On 2009-11-07, at 8:28 PM, John Harrop wrote:

> On Sat, Nov 7, 2009 at 8:04 PM, Stefan Arentz   
> wrote:
> But I'm using this in a bigger macro that takes a bunch of functions
> as a parameter. Is there a way to make this work or should I
> 'translate' the functions that I take by name with (var foo)?
>
> You'll need to translate the symbols into vars using resolve, I think.

Hmmm

user=> (var foo)
#'user/foo

user=> (meta (var foo))
{:ns #, :name foo, :file "NO_SOURCE_FILE", :line  
1, :arglists ([]), :xxx 1}

user=> ((var foo))
"foo"

I'll give this a try in my macro later today :-)

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



Functions and vars and meta-data

2009-11-07 Thread Stefan Arentz

Another one related to my previous question about meta-data.

user> (defn #^{ :xxx 1} foo [] "foo")
#'user/foo
user> (defn #^{ :xxx 2} bar [] "bar")
#'user/bar

I need to do something similar to this:

user> (map #(:xxx (meta %)) [foo bar])
(nil nil)

Basically accessing the meta data of a function of which I only know  
the 'name'. (Because that is what my macro takes as one of its  
parameters)

I know it works when I use the #' reader macro.

user> (map #(:xxx (meta %)) [#'foo #'bar])
(1 2)

But I'm using this in a bigger macro that takes a bunch of functions  
as a parameter. Is there a way to make this work or should I  
'translate' the functions that I take by name with (var foo)?

I must admin that I don't fully understand the difference between foo  
and #'foo. That is probably why I'm making this beginner mistake :-)

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



Adding meta data to a function (from a macro)

2009-11-07 Thread Stefan Arentz


I'm trying to do this:

(defmacro my-defn [name & body]
   `(defn- #^{ :foo-tag "blah" } ~name []
  ~...@body))

The idea is that foo will be defined and that {:foo-tag "blah"} is  
added to its meta-data.

But that does not seem to work:

user> (my-defn foo (println "Hello"))
#'user/foo

user> (foo)
Hello
nil

user> (meta #'foo)
{:ns #, :name foo, :file "NO_SOURCE_FILE", :line  
1, :arglists ([]), :private true}

What is the proper way to do this?

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



Re: [ANN] clj-iter, an iteration macro for Clojure inspired by Common Lisp's Iterate

2009-11-05 Thread Stefan Arentz


On 2009-11-05, at 7:03 PM, Daniel Janus wrote:

>
> Dear all,
>
> I am happy to announce the public availability of clj-iter, an  
> Iterate-
> like iteration macro. It is free (available under the terms of MIT
> license) and can be found on GitHub: http://github.com/nathell/clj- 
> iter
>
> The design goal was to keep it as simple as possible, and make it
> blend well with the rest of Clojure.  In
> contrast to cl-loop, which uses mutable bindings, clj-iter has a
> functional flavour, and macroexpands to the kind of code you would
> write manually using loop/recur.  It is also very simple, having a
> fraction of Iterate's functionality, but I hope even the little there
> is will be sufficient in many cases.
>
> To avoid citing the entire README blurb, I'll just give you some
> examples:
>
>(iter (for x in [31 41 59 26])
>  (for y from 1)
>  (collect (+ x y)))
>==> (32 43 62 30)
>
>(iter (for s on [1 2 3 4 5])
>(for q initially () then (cons (first s) q))
>(collect (cons (first s) (concat (take 2 (rest s)) (take 2
> q)
>==> ((1 2 3) (2 3 4 1) (3 4 5 2 1) (4 5 3 2) (5 4 3))
>
> Please let me know whether it is of any use to you.  Feedback, and
> especially patches, are more than welcome.

This is awesome. Can this be made part of contrib?

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



Syntax highlighting clojure code

2009-11-02 Thread Stefan Arentz

I want to post some Clojure code to my blog. Does anyone know of a  
simple, preferably online, code highlighter for Clojure or Lisp that  
turns code into simple html with either a stylesheet or just  tags?

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



Memoize improvement

2009-10-30 Thread Stefan Arentz


This is some of my first Clojure code so it might not be the  
greatest ... yet!

Here is an improved memoize function that also takes a time-to-live  
argument. Useful when you want to expire results from the cache.

I'm using it to cache query results from a database. This probably  
breaks some rules about functional programming though :-)

(defn expire-cached-results [cached-results time-to-live]
   "Expire items from the cached function results."
   (into {} (filter (fn [[k v]] (> time-to-live (- (System/ 
currentTimeMillis) (:time v cached-results)))

(defn my-memoize
   "Returns a memoized version of a referentially transparent  
function. The
   memoized version of the function keeps a cache of the mapping from  
arguments
   to results and, when calls with the same arguments are repeated  
often, has
   higher performance at the expense of higher memory use. Cached  
results are
   removed from the cache when their time to live value expires."
   [function time-to-live]
   (let [cached-results (atom {})]
 (fn [& arguments]
   (swap! cached-results expire-cached-results time-to-live)
   (println cached-results)
   (if-let [entry (find @cached-results arguments)]
 (:result (val entry))
 (let [result (apply function arguments)]
   (swap! cached-results assoc arguments { :result  
result :time (System/currentTimeMillis)})
   result)



Sample usage:

(defn calculation [n]
   (println "Doing some bistromath")
   (* n 42))

(def foo (my-memoize calculation 5000))

;; First execution
user> (foo 10)
Doing some bistromath
420

;; Executed immediately after first time - cached result
user> (foo 10)
420

;; Executed after 5 seconds - not cached anymore
user> (foo 10)
Doing some bistromath
420



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



Periodic tasks

2009-10-30 Thread Stefan Arentz

What is a good and simple way to run periodic tasks in Clojure? I need  
to run a simple function every couple of minutes. And make sure that  
if it throws an exception that it won't kill the periodic task.

I come from a Spring world where XML, Timers, Jobs and Quartz rule the  
world, so am hoping for something small and elegant for Closure :-)

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