Re: apply for macros?

2009-10-03 Thread Meikel Brandmeyer

Hi,

Am 04.10.2009 um 00:50 schrieb b2m:


What macros do y'all have that you want to "apply" things to?


A sure code smell, IMO. It most likely is based on a misunderstanding  
what macros are capable to do.



I am using structs and functions for workings with these structs.

Just some very general example:

(defstruct department :name :head :members-l1 :members-l2 ...)

(defn process-department [department-struct]
 (reduce + (list
   (* members-l1-fee (:members-l1 department-
struct))
   (* members-l2-fee)))


The functions themselves can be easily made independent from the  
number of keys. Just save the keys in a constant.


(defn process-department
  [department-struct]
  (->> +payment-levels+
(map #(* (fee %) (department-struct %)))
(reduce +)))

So you actually don't need specific functions for different companies.  
You just need some metadata on the struct.



The number of "payment levels" are not the same for every "company",
so I thought of creating these structs and functions by macros.
Because they all have the same arguments these calls are wrapped in a
function:

(defn init-funs [name & levels]
  (do
   (apply-macro create-department-struct name levels)
   (apply-macro create-process-department name levels)
   nil))


Here we have the smell! You cannot define functions with a function.  
You have to use a macro! It would look like this:


(defmacro init-department
  [name & levels-and-fees]
  (let [fee (apply hash-map levels-and-fees)]
 `(do
   (def ~'fee ~fee)
   (defstruct ~name ~@(keys fee))
   (def ~'payment-levels ~(set (keys fee))

Note, the "apply-macro" in form of ~...@.


So I need macros for the benefit of individual strucs and functions
for every "company", and I need something like apply to pass
parameters from the inital function to the macros. This is working by
using (eval (apply-macro... as described in a previous posting in this
thread. It would be nice to have a solution without eval. But maybe my
attempt of writing code that writes code is too enthusiastic.


Requiring eval is a code stink. It is a sign, that some pieces of the  
puzzle don't really fit together. Whenever you arrive at the need of  
eval in "normal" code, you should step back and look where the  
misunderstanding is.


Hope this helps.

Sincerely
Meikel



smime.p7s
Description: S/MIME cryptographic signature


Re: Load Bitmap

2009-10-03 Thread Josh Daghlian

On Oct 3, 8:27 pm, Sean Devlin  wrote:
> Take a look at the javax.imageio.ImageIO class.  There are some useful
> static methods there for loading files.  Also, if you're going to be
> doing image editing, I'd suggest reading the AWT sections in Cornell &
> Horstmann.
>
> http://www.horstmann.com/corejava.html

If you're going to be doing *lots* of image editing, you may want to
look into ImageJ. It's used pretty widely for scientific image
analysis, and has a pretty robust plugin architecture, including
clojure code. There's a version called Fiji that I've not used myself
much, but which promises to be a good starting point:

http://groups.google.com/group/clojure/browse_thread/thread/15f566f12cc485a4

http://pacific.mpi-cbg.de


--~--~-~--~~~---~--~~
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: Load Bitmap

2009-10-03 Thread Sean Devlin

Take a look at the javax.imageio.ImageIO class.  There are some useful
static methods there for loading files.  Also, if you're going to be
doing image editing, I'd suggest reading the AWT sections in Cornell &
Horstmann.

http://www.horstmann.com/corejava.html

Both volumes should be required reading for aspiring Clojure hackers.

On Oct 3, 7:35 pm, John Newman  wrote:
> Actually, that code is rather dated.  An updated version might look like
> this.
>
> (Warning, not tested)
>
> ; grab-pixels :: BufferedImage -> [Integer]
> (defn grab-pixels
>   "Returns an array containing the pixel values of the image"
>   [image]
>   (let [w (.getWidth image)
>         h (.getHeight image)
>         pixels (make-array (Integer/TYPE) (* w h))]
>     (.grabPixels (PixelGrabber. image 0 0 w h pixels 0 w))
>     pixels))
>
>
>
>
>
> On Sun, Oct 4, 2009 at 3:52 AM, John Newman  wrote:
> > I don't know much about it, but Yann N. Dauphin's Mona Lisa program might
> > use what you're looking for:  Clojure: Genetic Mona Lisa problem in 250
> > beautiful 
> > lines
>
> > Here's the relevant code:
>
> >>    1. ; grab-pixels :: BufferedImage -> [Integer]
> >>    2. (defn grab-pixels
> >>    3.   "Returns an array containing the pixel values of image."
> >>    4.   [image]
> >>    5.   (let [w (. image (getWidth))
> >>    6.         h (. image (getHeight))
> >>    7.         pixels (make-array (. Integer TYPE) (* w h))]
> >>    8.     (doto (new PixelGrabber image 0 0 w h pixels 0 w)
> >>    9.       (.grabPixels))
> >>    10.     pixels))
>
> > On Sat, Oct 3, 2009 at 10:20 PM, prishvin  wrote:
>
> >> Dear friends,
>
> >> I have some experience in lisp and now want to write a small program
> >> in clojure.
> >> The question is,  how can I load a gray scale bitmap and how can i
> >> access individual pixels in it.
>
> >> Thank you in advance.
> >> M.Prishvin.
>
> > --
> > John
>
> --
> John
--~--~-~--~~~---~--~~
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: Load Bitmap

2009-10-03 Thread John Newman
Actually, that code is rather dated.  An updated version might look like
this.

(Warning, not tested)

; grab-pixels :: BufferedImage -> [Integer]
(defn grab-pixels
  "Returns an array containing the pixel values of the image"
  [image]
  (let [w (.getWidth image)
h (.getHeight image)
pixels (make-array (Integer/TYPE) (* w h))]
(.grabPixels (PixelGrabber. image 0 0 w h pixels 0 w))
pixels))


On Sun, Oct 4, 2009 at 3:52 AM, John Newman  wrote:

> I don't know much about it, but Yann N. Dauphin's Mona Lisa program might
> use what you're looking for:  Clojure: Genetic Mona Lisa problem in 250
> beautiful 
> lines
>
> Here's the relevant code:
>
>>
>>1. ; grab-pixels :: BufferedImage -> [Integer]
>>2. (defn grab-pixels
>>3.   "Returns an array containing the pixel values of image."
>>4.   [image]
>>5.   (let [w (. image (getWidth))
>>6. h (. image (getHeight))
>>7. pixels (make-array (. Integer TYPE) (* w h))]
>>8. (doto (new PixelGrabber image 0 0 w h pixels 0 w)
>>9.   (.grabPixels))
>>10. pixels))
>>
>>
>

> On Sat, Oct 3, 2009 at 10:20 PM, prishvin  wrote:
>
>>
>> Dear friends,
>>
>> I have some experience in lisp and now want to write a small program
>> in clojure.
>> The question is,  how can I load a gray scale bitmap and how can i
>> access individual pixels in it.
>>
>> Thank you in advance.
>> M.Prishvin.
>>
>> >>
>>
>
>
> --
> John
>



-- 
John

--~--~-~--~~~---~--~~
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: Load Bitmap

2009-10-03 Thread John Newman
I don't know much about it, but Yann N. Dauphin's Mona Lisa program might
use what you're looking for:  Clojure: Genetic Mona Lisa problem in 250
beautiful 
lines

Here's the relevant code:

>
>1. ; grab-pixels :: BufferedImage -> [Integer]
>2. (defn grab-pixels
>3.   "Returns an array containing the pixel values of image."
>4.   [image]
>5.   (let [w (. image (getWidth))
>6. h (. image (getHeight))
>7. pixels (make-array (. Integer TYPE) (* w h))]
>8. (doto (new PixelGrabber image 0 0 w h pixels 0 w)
>9.   (.grabPixels))
>10. pixels))
>
>

On Sat, Oct 3, 2009 at 10:20 PM, prishvin  wrote:

>
> Dear friends,
>
> I have some experience in lisp and now want to write a small program
> in clojure.
> The question is,  how can I load a gray scale bitmap and how can i
> access individual pixels in it.
>
> Thank you in advance.
> M.Prishvin.
>
> >
>


-- 
John

--~--~-~--~~~---~--~~
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: apply for macros?

2009-10-03 Thread b2m

> What macros do y'all have that you want to "apply" things to?

I am using structs and functions for workings with these structs.

Just some very general example:

(defstruct department :name :head :members-l1 :members-l2 ...)

(defn process-department [department-struct]
  (reduce + (list
(* members-l1-fee (:members-l1 department-
struct))
(* members-l2-fee)))

The number of "payment levels" are not the same for every "company",
so I thought of creating these structs and functions by macros.
Because they all have the same arguments these calls are wrapped in a
function:

(defn init-funs [name & levels]
   (do
(apply-macro create-department-struct name levels)
(apply-macro create-process-department name levels)
nil))

A call like
(init-funs "company1" "level1" "level2")
will result in
(defstruct company1-department :name :head :level1 :level2)
and so on.

So I need macros for the benefit of individual strucs and functions
for every "company", and I need something like apply to pass
parameters from the inital function to the macros. This is working by
using (eval (apply-macro... as described in a previous posting in this
thread. It would be nice to have a solution without eval. But maybe my
attempt of writing code that writes code is too enthusiastic.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Load Bitmap

2009-10-03 Thread prishvin

Dear friends,

I have some experience in lisp and now want to write a small program
in clojure.
The question is,  how can I load a gray scale bitmap and how can i
access individual pixels in it.

Thank you in advance.
M.Prishvin.

--~--~-~--~~~---~--~~
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: apply for macros?

2009-10-03 Thread John Harrop
In the specific cases of "and" and "or", I made utility functions that do
non-short-circuiting "and" and "or" for use with "apply" and a stream of
boolean data. (Not sure which implementation is more efficient though: a
version that returns its argument with one argument, punts to the
appropriate macro with two, and uses recur with more than two; a version
similar to that that uses reduce with more than two; or a higher-level
(every? identity args) / (some? identity args). The latter are clearly more
elegant, but speed is a concern with something that might be used often.)

What macros do y'all have that you want to "apply" things to? Most likely
what you need is to make a version that's a function, if you can live
without some deferred/selective evaluation of arguments type feature (like
the short-circuiting of "and").

--~--~-~--~~~---~--~~
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: Debugging questions

2009-10-03 Thread kyle smith

select returns a set, so you need to call seq/vec before calling nth.

user> (nth (seq (clojure.set/select #(zero? (mod % 2)) #{1 2 5 10}))
0)
2
--~--~-~--~~~---~--~~
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: Memory Characteristics of Persistent Datastructures

2009-10-03 Thread Elliott Slaughter

Thanks.

JVisualVM shows the initial memory usage is a couple hundred MB, then
decreases to levels about what you described. I'm using a 64-bit Java
with the -server option to increase the max heap size.

But in my actual application, the memory performance is much worse:
the heap size keeps growing until it gets up to about 1000MB (and
unlike the previous example, actually uses most of that space). Once
it hits about 1000MB, instead of running GC once every couple of
seconds, it runs GC almost constantly, causing my application to
basically freeze. (Since the application is a game, any long-running
GC is a problem.)

Here is a screenshot from JVisualVM. In the CPU graph, you can see the
purple spikes are where global GCs happen. At first, they happen
occasionally, but at the end, GC is running frequently and repeatedly.

http://imgur.com/fNFOr.png

Of course, my application is much more complex than the previous
example, and it's entirely possible I'm missing some sort of leak in
my code. But at the core of the program, I'm just using a tree of
nested maps and references, so theoretically they should have similar
performance characteristics.

Thanks again. Any further help would be appreciated.

On Oct 3, 1:24 am, hoeck  wrote:
> Hi,
>
> to measure memory requirements of java programs, it is better use a
> tool like jvisualvm. The JVM process aquires memory as it needs
> according to its max heap and max permgen settings.
> I ran your little snippet on my machine with java -Xmx5M -jar
> clojure.jar, and
> while jvisualvm reported around 2M heap usage (like a bare clojure
> REPL), top showed a memory usage of about 25 Megabytes.
>
> The default max heapsize is, at least on my machine with a 32bit JVM,
> at around 128M, and lots of it will be allocated up front, regardless
> how
> much heap the running java programm needs.
>
> Erik
--~--~-~--~~~---~--~~
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: Multimethod or Multiple Arg Lists

2009-10-03 Thread Sean Devlin

I would definitely prefer clear-cached-files2.  I usually use
multimethods when I need to change behavior on type of arguments, not
the argument count.

Let's take a look at reduce.  I would write the shorter form in terms
of the longer one.

(defn reduce
  ([f coll] (reduce f (first coll) (rest coll)))
  ([f init coll] (reduce stuff...)))

I find that this makes my code easier to maintain.

Just my $.02
Sean



On Oct 3, 3:11 pm, Robert Stehwien  wrote:
> I'm toying around with a file utility library as part of a vow to do all my
> "scripting" in clojure so I can learn the language - this is actually my
> first attempt at writing anything "real" with clojure.  I considered using
> memoize to cache file listing but wanted to be able to selectively clear the
> cache for specific file listings.
>
> I was trying to write a clear-cached-files function that would take nothing
> to clear them or take one or more items to clear that list of files.  So I
> have two questions:
>
> 1) What would be the right way to call dissoc with a single item or list.  I
> tried numerous things but what I have below is what I got to work and I'm
> not sure if is the best way.
>
> 2) (and the more important question).  When would you prefer using a
> multimethod (clear-cached-files) that switches based on argument count or a
> single method (clear-cached-files2) with multiple argument lists?
>
> Thanks,
> Robert
>
> I put the whole source to the file utils I've written below for reference.
>
> --
> (ns rstehwien.clojure.fileutils
>   (:gen-class)
>   (:import (java.io File)))
>
> (def file-seq-cache (ref {}))
>
> (defmulti clear-cached-files (fn[& arglist] (count arglist)))
>
> (defmethod clear-cached-files 0 [& arglist]
>   (dosync (alter file-seq-cache empty)))
>
> (defmethod clear-cached-files 1 [& arglist]
>   (dosync (alter file-seq-cache dissoc (first arglist
>
> (defmethod clear-cached-files :default [& arglist]
>   (doseq [arg arglist] (clear-cached-files arg)))
>
> (defn clear-cached-files2
>   ([]
>      (dosync (alter file-seq-cache empty)))
>   ([key]
>      (dosync (alter file-seq-cache dissoc key)))
>   ([key & ks]
>      (dosync
>       (clear-cached-files2 key)
>       (doseq [key2 ks] (clear-cached-files key2)
>
> (defn get-cached-files [path]
>   (dosync
>     (when-not (contains? @file-seq-cache path)
>       (alter file-seq-cache conj {path (file-seq (File. path))}))
>     (@file-seq-cache path)))
>
> (defn filter-files-ends-with [files suffix]
>   (filter #(.. % toString (endsWith suffix)) files))
>
> (defn files-ending-with [path suffix]
>   (filter-files-ends-with (get-cached-files path) suffix))
> --
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Multimethod or Multiple Arg Lists

2009-10-03 Thread Robert Stehwien
I'm toying around with a file utility library as part of a vow to do all my
"scripting" in clojure so I can learn the language - this is actually my
first attempt at writing anything "real" with clojure.  I considered using
memoize to cache file listing but wanted to be able to selectively clear the
cache for specific file listings.

I was trying to write a clear-cached-files function that would take nothing
to clear them or take one or more items to clear that list of files.  So I
have two questions:

1) What would be the right way to call dissoc with a single item or list.  I
tried numerous things but what I have below is what I got to work and I'm
not sure if is the best way.

2) (and the more important question).  When would you prefer using a
multimethod (clear-cached-files) that switches based on argument count or a
single method (clear-cached-files2) with multiple argument lists?

Thanks,
Robert

I put the whole source to the file utils I've written below for reference.

--
(ns rstehwien.clojure.fileutils
  (:gen-class)
  (:import (java.io File)))

(def file-seq-cache (ref {}))

(defmulti clear-cached-files (fn[& arglist] (count arglist)))

(defmethod clear-cached-files 0 [& arglist]
  (dosync (alter file-seq-cache empty)))

(defmethod clear-cached-files 1 [& arglist]
  (dosync (alter file-seq-cache dissoc (first arglist

(defmethod clear-cached-files :default [& arglist]
  (doseq [arg arglist] (clear-cached-files arg)))

(defn clear-cached-files2
  ([]
 (dosync (alter file-seq-cache empty)))
  ([key]
 (dosync (alter file-seq-cache dissoc key)))
  ([key & ks]
 (dosync
  (clear-cached-files2 key)
  (doseq [key2 ks] (clear-cached-files key2)

(defn get-cached-files [path]
  (dosync
(when-not (contains? @file-seq-cache path)
  (alter file-seq-cache conj {path (file-seq (File. path))}))
(@file-seq-cache path)))

(defn filter-files-ends-with [files suffix]
  (filter #(.. % toString (endsWith suffix)) files))

(defn files-ending-with [path suffix]
  (filter-files-ends-with (get-cached-files path) suffix))
--

--~--~-~--~~~---~--~~
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: apply for macros?

2009-10-03 Thread b2m

Hi
> Yes.  You build up the code/data structure and pass it to either
> `eval' or `macroexpand', depending on your exact goals...

thx

> user=> (def args '(true false true false))
> #'user/args
> user=> (def code `(and ~...@args))
> #'user/code
> user=> code
> (clojure.core/and true false true false)
> user=> (eval code)
> false
> user=> (macroexpand code)
> (let* [and__3981__auto__ true] (if and__3981__auto__ (clojure.core/and
> false true false) and__3981__auto__))

A solution not using eval would be great but when writing code that
writes code this maybe is impossible.

Greets, Benjamin
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



refs implement IFn, atoms and agents do not?

2009-10-03 Thread Stuart Halloway

Is there a principled reason for this? I have written some code that  
(unintentionally) limits itself to refs because it assumes that all  
reference types can sit in function position.

Thanks,
Stu

--~--~-~--~~~---~--~~
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: server-socket on exit event

2009-10-03 Thread ngocdaothanh

Emeka, good catch. It's just my mistake.

Another thing is I think there may be exception raised when on-msg
sends message to a closed socket. How would you solve this?


On Oct 3, 7:35 pm, Emeka  wrote:
> ngo,
> I was about doing this kind of client/server thing some  days ago, however
> now you are into it I would like to learn then. I am not quite clear why you
> have this:
> (.start (new Thread (fn [] (create-server 8080 chat-loop
>
> My concern is on  Thread, create-server function has a Thread inside
> create-server-aux function.
>
> Regards,
> Emeka
>
> On Fri, Oct 2, 2009 at 3:13 AM, ngocdaothanh  wrote:
>
> > > I'm not sure TCP/IP has a native facility for that.
>
> > I'm afraid John's statement is correct:
>
> >http://www.velocityreviews.com/forums/t125620-client-socket-disconnec...
> > and trying to read and write until something wrong happens as
> > demonstrated in Roger's code is the only way to check for
> > disconnection.
>
> > Below is my new code. Sorry it is rather long.
>
> > (import '[java.io BufferedReader InputStreamReader
> > OutputStreamWriter])
> > (use 'clojure.contrib.server-socket)
>
> > (def clients (ref []))  ; Each client is an *out*
>
> > (defn on-msg [from msg]
> >  (println msg)
> >  (doall
> >    (map
> >      (fn [client]
> >        (if-not (= from client)
> >          (binding [*out* client]
> >            (println msg)
> >            (flush
> >     �...@clients)))
>
> > (defn on-disconnect [client]
> >  (dosync
> >    (alter clients
> >      (fn [clients]
> >        (remove (fn [c] (= c client)) clients
> >  (on-msg client "A client has disconnected"))
>
> > (defn on-connect [client]
> >  (dosync (alter clients conj client))
> >  (on-msg client "A client has connected"))
>
> > (defn chat-loop [is os]
> >  (let [client (OutputStreamWriter. os)]
> >    (on-connect client)
> >    (binding [*in* (BufferedReader. (InputStreamReader. is))]
> >      (loop []
> >        (let [msg (read-line)]  ; msg is nil when the client
> > disconnects
> >          (if (nil? msg)
> >            (on-disconnect client)
> >            (do
> >              (on-msg client msg)
> >              (recur
>
> > (.start (new Thread (fn [] (create-server 8080 chat-loop
>
> > On Oct 2, 5:20 am, John Harrop  wrote:
> > > On Thu, Oct 1, 2009 at 4:02 PM, Roger Gilliar  wrote:
> > > > Am 01.10.2009 um 21:28 schrieb ngocdaothanh:
> > > > > Roger, your code is not event based.
> > > > What do you mean by not event based ?
>
> > > He means he wants automatic notification if a connection is dropped.
>
> > > I'm not sure TCP/IP has a native facility for that.
>
> > > What most chat type programs, multiplayer games, and suchlike do is send
> > a
> > > periodic ping from server to each connected client, which as part of the
> > > chat protocol the client is supposed to acknowledge. If a client stops
> > > responding for more than a few ping-intervals, it's assumed to have
> > > disconnected or otherwise become unreachable.
>
> > > This method has the advantage of being entirely under the control of the
> > > application layer, and the further advantage of also working with UDP
> > (which
> > > is crucial in the "multiplayer games" case at least).
--~--~-~--~~~---~--~~
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: apply for macros?

2009-10-03 Thread Rob



On Oct 2, 12:47 pm, b2m  wrote:
>
> But all the arguments are in a list e.g. (def arglist '(true false
> true false))
>
> Is there a simple way to 'apply' the macro to a list of arguments like
> it works for functions: (apply + '(1 2 3)) ?
>

Yes.  You build up the code/data structure and pass it to either
`eval' or `macroexpand', depending on your exact goals...

user=> (def args '(true false true false))
#'user/args
user=> (def code `(and ~...@args))
#'user/code
user=> code
(clojure.core/and true false true false)
user=> (eval code)
false
user=> (macroexpand code)
(let* [and__3981__auto__ true] (if and__3981__auto__ (clojure.core/and
false true false) and__3981__auto__))


Rob

--~--~-~--~~~---~--~~
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: Adding meta data to string?

2009-10-03 Thread pmf

On Oct 3, 9:17 am, Jung Ko  wrote:
> Is it possible to add meta-data to Strings?
>
> (with-meta "Hello" {:key 123})
>
> java.lang.String cannot be cast to clojure.lang.IObj
>   [Thrown class java.lang.ClassCastException]

And String is final, making deriving from it impossible.

One way (most probably not the best way) would be to wrap it in a Var
and attach the metadata to the Var, i.e.

  (def #^{:blah :foo} my-string "some string")

Note that to access the metadata of the Var later, you have to use var-
quote:

  ^#'my-string

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

2009-10-03 Thread Jung Ko

Is it possible to add meta-data to Strings?

(with-meta "Hello" {:key 123})

java.lang.String cannot be cast to clojure.lang.IObj
  [Thrown class java.lang.ClassCastException]

One solution that I found by Googling around is to extend the Java
class that I want to add meta data (String in this case) and have the
new class implement IObj. However, that solution requires me to
program in Java and seems like an overkill for the String class.

Is there a better & easier way to add meta-data to strings?

Sincerely,
Jung Ko

--~--~-~--~~~---~--~~
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: Debugging questions

2009-10-03 Thread Timothy Pratley

Hi Jose,

> ; trying to get the first element of an unordered set
> (nth (clojure.set/select #(= 0 (mod % 2)) #{1 2 5 10}) 0)
> Why do I get different messages running from the REPL or loading from a clj
> file ?

I tried it from REPL and file using the from source Clojure 1.1.0-
alpha-SNAPSHOT,
and for both it gave nth not supported exception, and for the file it
gave the line number.

Sorry I guess it doesn't help you much, but just letting you know :)




--~--~-~--~~~---~--~~
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: server-socket on exit event

2009-10-03 Thread Emeka
ngo,
I was about doing this kind of client/server thing some  days ago, however
now you are into it I would like to learn then. I am not quite clear why you
have this:
(.start (new Thread (fn [] (create-server 8080 chat-loop

My concern is on  Thread, create-server function has a Thread inside
create-server-aux function.

Regards,
Emeka



On Fri, Oct 2, 2009 at 3:13 AM, ngocdaothanh  wrote:

>
> > I'm not sure TCP/IP has a native facility for that.
>
> I'm afraid John's statement is correct:
>
> http://www.velocityreviews.com/forums/t125620-client-socket-disconnection-event-not-received-on-server-socket-java-nio.html
> and trying to read and write until something wrong happens as
> demonstrated in Roger's code is the only way to check for
> disconnection.
>
> Below is my new code. Sorry it is rather long.
>
> (import '[java.io BufferedReader InputStreamReader
> OutputStreamWriter])
> (use 'clojure.contrib.server-socket)
>
> (def clients (ref []))  ; Each client is an *out*
>
> (defn on-msg [from msg]
>  (println msg)
>  (doall
>(map
>  (fn [client]
>(if-not (= from client)
>  (binding [*out* client]
>(println msg)
>(flush
>  @clients)))
>
> (defn on-disconnect [client]
>  (dosync
>(alter clients
>  (fn [clients]
>(remove (fn [c] (= c client)) clients
>  (on-msg client "A client has disconnected"))
>
> (defn on-connect [client]
>  (dosync (alter clients conj client))
>  (on-msg client "A client has connected"))
>
> (defn chat-loop [is os]
>  (let [client (OutputStreamWriter. os)]
>(on-connect client)
>(binding [*in* (BufferedReader. (InputStreamReader. is))]
>  (loop []
>(let [msg (read-line)]  ; msg is nil when the client
> disconnects
>  (if (nil? msg)
>(on-disconnect client)
>(do
>  (on-msg client msg)
>  (recur
>
> (.start (new Thread (fn [] (create-server 8080 chat-loop
>
>
> On Oct 2, 5:20 am, John Harrop  wrote:
> > On Thu, Oct 1, 2009 at 4:02 PM, Roger Gilliar  wrote:
> > > Am 01.10.2009 um 21:28 schrieb ngocdaothanh:
> > > > Roger, your code is not event based.
> > > What do you mean by not event based ?
> >
> > He means he wants automatic notification if a connection is dropped.
> >
> > I'm not sure TCP/IP has a native facility for that.
> >
> > What most chat type programs, multiplayer games, and suchlike do is send
> a
> > periodic ping from server to each connected client, which as part of the
> > chat protocol the client is supposed to acknowledge. If a client stops
> > responding for more than a few ping-intervals, it's assumed to have
> > disconnected or otherwise become unreachable.
> >
> > This method has the advantage of being entirely under the control of the
> > application layer, and the further advantage of also working with UDP
> (which
> > is crucial in the "multiplayer games" case at least).
> >
>

--~--~-~--~~~---~--~~
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: apply for macros?

2009-10-03 Thread b2m

Hi,
some piece of code that helps me in this special case.
Here the "translated" version:

(defmacro apply-macro
  [m & args]
  `(list* ~m ~...@args))

(def li '(true false false))
(eval (apply-macro 'and true false li)) ; observe of the quote before
the macro

Or instead of trying sth. like:

(defn some-test [x & args] (if-not (nil? x) (apply and args))) ; this
will fail

writing this function as macro:

(defmacro some-test [x & args] `(if-not (nil? ~x) (and ~...@args) false))

Somebody with other ideas?

Greets, Benjamin
--~--~-~--~~~---~--~~
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: Memory Characteristics of Persistent Datastructures

2009-10-03 Thread hoeck


Hi,

to measure memory requirements of java programs, it is better use a
tool like jvisualvm. The JVM process aquires memory as it needs
according to its max heap and max permgen settings.
I ran your little snippet on my machine with java -Xmx5M -jar
clojure.jar, and
while jvisualvm reported around 2M heap usage (like a bare clojure
REPL), top showed a memory usage of about 25 Megabytes.

The default max heapsize is, at least on my machine with a 32bit JVM,
at around 128M, and lots of it will be allocated up front, regardless
how
much heap the running java programm needs.

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