Re: Java Class Factory

2009-12-03 Thread sross
On Dec 3, 4:15 am, lazy1 miki.teb...@gmail.com wrote:
 Hello,
 What is the right way to do this?

As people have mentioned, new is a special form which evaluates it's
arguments at
compile time and cannot be used with a dynamic class name.

Fortunately you can use the reflection API and create instance of your
class
 using the newInstance method.

(defn new-container
  [type]
  (.newInstance (*containers* type)))


- sean

-- 
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: Oh, yeah, transients are fast!

2009-11-24 Thread sross
I believe that this is most likely a symptom of the Apple JVM and not
that of transients
as the change from persistents to transients is far more substantial
on one of our linux
servers than it is on my macbook pro (6x vs 2x speedup)

I'm not entirely sure as to why this is the case but I suspect that GC
has something to
do with it as the following example, which simply bumps a counter in a
map, exhibits
the more impressive performance improvement

(defn make-n [x n]
  (loop [i 0 v []]
(if ( i n)
  (recur (inc i) (conj v x))
  v)))

(defn make-n2 [x n]
  (loop [i (int 0) v (transient [])]
(if ( i n)
  (recur (inc i) (conj! v x))
  (persistent! v

(dotimes [x 10]
  (do (System/gc) (time (make-n 1 10)) 1))

(dotimes [x 10]
  (do (System/gc) (time (make-n2 1 10)) 1))



On Nov 23, 9:07 pm, Raoul Duke rao...@gmail.com wrote:
 i have tried:

 1.5
 1.6
 1.6 -server
 the last i did both in repl-in-emacs, and in a repl-in-straightup-shell.

 the numbers i get are weird. it does seem like v2 is faster than v,
 but never gets stupendously fast (never faster than 500 msec on a dual
 core macbook pro 2.2ghz core 2 duo 4gb ram), and v does a strange
 thing where i run v2 a bunch then the first time i run v it is like 2x
 v2, and then subsequent runs of timing v are like 8x v2. and here i
 thought hotspot was supposed to get faster, not slower, over time.

 oy veh.

-- 
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: Periodic tasks

2009-11-04 Thread sross

You could also try cron4j which, while less customisable than quartz,
is far simpler to use.
It also accepts Runnable's as as tasks which allows for much easier
clojure integration.

user= (def sched (it.sauronsoftware.cron4j.Scheduler.))
#'user/sched

user= (.schedule sched * * * * * #(println Hello, World!))

dca735307ae35bb7649a8a680124bec35ff926b54d06
user= (.start sched)



On Oct 30, 4:05 am, Stefan Arentz ste...@arentz.ca wrote:
 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
-~--~~~~--~~--~--~---



Re: Applying Java methods.

2009-09-23 Thread sross

On Sep 22, 2:30 pm, Chouser chou...@gmail.com wrote:
 On Mon, Sep 21, 2009 at 5:22 PM, sross ros...@gmail.com wrote:

  Hi All,

   I'm looking for a bit of advice for calling a java method which has a
  few different signatures (such as Connection.prepareStatement).
   Is there a cleaner way of passing a variable number of arguments to
  the method, such as

  (apply (memfn prepareStatement) (sql/connection) args)

   or is doing something like the  following the only approach.

  (defn prepare-statement
   ([sql] (.prepareStatement (sql/connection) sql))
   ([sql arg] (.prepareStatement (sql/connection) sql arg))
   ([sql arg arg2] (.prepareStatement (sql/connection) sql arg arg2))
   ([sql arg arg2 arg3] (.prepareStatement (sql/connection) sql arg
  arg2 arg3)))

 Your solution will provide the best performance (you may need
 to type-hint the args).  If performance doesn't matter, you
 can use Rich's jcall fn:

   (defn jcall [obj name  args]
     (clojure.lang.Reflector.invokeInstanceMethod
       obj
       (str name)
       (if args (to-array args) (clojure.lang.RT.EMPTY_ARRAY

 Then you could:

   (defn prepare-statement [ args]
     (apply jcall (sql/connection) prepareStatement args))

 Note that uses runtime reflection, array creation, etc. each
 time it's called.  You can expect it to be at least an order
 of magnitude slower than if it were directly compiled like
 your original solution could be.

 --Chouser


Thanks for a solution and the advice.


--~--~-~--~~~---~--~~
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: Applying Java methods.

2009-09-22 Thread sross

On Sep 21, 11:35 pm, pmf phil.fr...@gmx.de wrote:
 On Sep 21, 11:22 pm, sross ros...@gmail.com wrote:

   I'm looking for a bit of advice for calling a java method which has a
  few different signatures (such as Connection.prepareStatement).
   Is there a cleaner way of passing a variable number of arguments to
  the method, such as

  (apply (memfn prepareStatement) (sql/connection) args)

 Java's vararg-methods accept an array of Objects; try if this works:
 (.prepareStatement (sql/connection) sql (into-array Object [arg1 arg2
 arg3])


Yes. Unfortunately prepareStatement isn't a varargs method but 3
methods with
differing signatures so this approach cannot be used here.



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



Applying Java methods.

2009-09-21 Thread sross

Hi All,

 I'm looking for a bit of advice for calling a java method which has a
few different signatures (such as Connection.prepareStatement).
 Is there a cleaner way of passing a variable number of arguments to
the method, such as

(apply (memfn prepareStatement) (sql/connection) args)

 or is doing something like the  following the only approach.

(defn prepare-statement
  ([sql] (.prepareStatement (sql/connection) sql))
  ([sql arg] (.prepareStatement (sql/connection) sql arg))
  ([sql arg arg2] (.prepareStatement (sql/connection) sql arg arg2))
  ([sql arg arg2 arg3] (.prepareStatement (sql/connection) sql arg
arg2 arg3)))


Thanks,
 Sean.

--~--~-~--~~~---~--~~
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: How to splitt a string by words?

2009-08-11 Thread sross

clojure.contrib.str-utils/re-partition is what you are looking for;

(re-partition #\w+a word and another word1   )

- (a   word   and   another   word1)


On Aug 11, 12:57 pm, ogcraft ogcr...@gmail.com wrote:
 How can I split a string,  but not to drop witespaces.
 Like this:

    a word and another word1   -- (   , a,  , word,  ,
 and,  , another,  , word1,  ).

 Thanks in advance.
--~--~-~--~~~---~--~~
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: dosync ref-set and threads coordination problem

2009-06-15 Thread sross



On Jun 14, 12:20 am, vmargioulas vxm...@gmail.com wrote:
 In the example below 256 clients threads put a byte (0-255) to server-
 socket reference var items,
 then connect to server-socket, write the same byte to socket stream
 and close the connection.
 The server connections threads reads a byte from the socket stream and
 remove it from the var items.
 At the end of the run i expect the var items to be empty but it is
 not.
 It 's contains a random number of bytes.
 Where is the error?

 (ns
   test-dosync
   (:import (java.net Socket))
   (:use clojure.contrib.server-socket))

 (def server
   (let [items (ref nil)
         server-ref (ref nil)
         server-fn (fn [ins _]
                     (let [bt (.read ins)]
                       (dosync
                        (ref-set items (remove #(= % bt) @items)]

     {:enq (fn [item] (dosync (alter items conj item)))
      :list-items (fn [] @items)
      :start (fn [port]
               (let [socket-server (create-server port server-fn)]
                 (dosync
                  (ref-set items nil)
                  (ref-set server-ref socket-server
      :stop (fn [] (when @server-ref (close-server @server-ref)))
      }))

 (defn start-server [port] ((:start server) port))
 (defn stop-server [] ((:stop server)))
 (defn enq [id] ((:enq server) id))
 (defn list-items [] ((:list-items server)))

 (defn client-fn [port #^Byte byte-to-send]
   #(let [socket (Socket. localhost port)
          outs (.getOutputStream socket)]
      (try
       (do
         (enq byte-to-send)
         (doto outs (.write byte-to-send) (.flush)))
       (finally
        (.close socket)

 (defn run-test []
   (try
    (let [port 10002]
      (start-server port)
      (dorun (apply pcalls (map #(client-fn port %) (range 256)
    (finally (stop-server)))
   (list-items))

This is probably a lot simpler than you may think;

What is happening here is that your run-test function is returning
before the removes have been run since the client functions do not
wait for confirmation from the server before returning. This can
result in ordering like the following
(with 2 client threads)

Client 1 enqueues byte
Client 1 writes byte and flushes
Client 2 enqueues byte
Client 2 writes byte and flushes
Server Thread for Client1 reads byte
Server Thread for Client2 reads byte
Server gets stopped
run-test returns (0 1)
Server Thread for Client1 removes byte
Server Thread for Client2 removes byte.


Of course this is only an example of a multitude of ways that these
can combine, you can add a couple of logging
statements to your functions to see when these things are happening,
and I think you may be quite surprised at
the different orderings that can arise, but remember to take them with
a grain of salt as output isn't immediate either ;)

The crux here is that your are relying on the server functions running
to completion but you are not waiting on the client
side to confirm that this has happened.


- sean

--~--~-~--~~~---~--~~
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: What's the function that checks if an object is sequence-able?

2009-06-04 Thread sross

On Jun 3, 8:34 pm, Stephen C. Gilardi squee...@mac.com wrote:
 On Jun 3, 2009, at 12:53 PM, CuppoJava wrote:
  From that we could derive isSeqable in Java as

    static public bool isSeqable(Object coll){
        return
          (coll instanceof ISeq) ||
          (coll instanceof Seqable) ||
          (coll == null) ||
          (coll instanceof Iterable) ||
          (coll.getClass().isArray()) ||
          (coll instanceof String) ||
          (coll instanceof Map);
    }

 (ASeq and LazySeq both implement ISeq so their two tests can be  
 collapsed into one.)

wouldn't it be best to define seqable? in terms of seq ?

something like

(defn seqable? [x]
  (try
   (let [s (seq x)]
 (when (or s (= s nil))
   true))
   (catch IllegalArgumentException _ false)))

perhaps not quite as performant but will definitely solve the OP's
problem.

- sean

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