Re: Formatted printing?

2008-10-11 Thread Paul Stadig

I'm using a clojure source file as a database, by loading the file
modifying it then saving it back to the filesystem. I'm trying to keep
things simple.

It would be useful to pretty print it so I could inspect it and make
small changes, if necessary.

It seems like a rather simple problem, and I only need some line
breaks or something simple, some text editors have a problem with
1,000's of characters on one line. The other problem is that if there
is an error loading the database Clojure says there's an error on line
1, which is the only line. I'll just code up something simple.

Starting from some Scheme or CL code is a good idea.  I'll just adapt
it to support literal maps, vectors, and such.

Thanks for your reply,
Paul



On 10/11/08, Michel Salim <[EMAIL PROTECTED]> wrote:
>
>
>
> On Oct 10, 4:37 pm, "Paul Stadig" <[EMAIL PROTECTED]> wrote:
>> I have a "database" that I'm working with that I'm loading using
>>
>> (def *db* (load-file "path-to-file/db.clj"))
>>
>> and saving using by basically opening a PrintWriter and using (pr-str
>> *db*).
>> The output to the file is a clojure data structure, but it is all on one
>> line. Is there any existing code for printing out in a formatted way with
>> new lines and indents and such?
>>
> Porting an existing CLisp / Scheme pretty printing tool probably won't
> be hard. Is this for printing, or just for better readability?
>
> Regards,
>
> --
> Michel Salim
> >
>

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Get thread local bindings

2008-10-11 Thread ntupel

On Fri, 2008-10-10 at 12:46 -0700, Mike Hinchey wrote:
> It's usually advised to avoid eval.

Many thanks Mike. I would like to avoid eval, but I am too stupid.
However I would love to find out how to do it. If you could give me
hunch I would be more than happy.

> See dothread-keeping in 
> http://github.com/jochu/swank-clojure/tree/master/swank/util/concurrent/thread/thread.clj
> And keep-bindings in 
> http://github.com/jochu/swank-clojure/tree/master/swank/util/util.clj

Sweet. The only thing that stops me from using these is that I cannot
provide explicitly the values I want to inherit as this would require
knowledge of bindings deep down in code that shouldn't care.

To elaborate a bit. I have an application running that provides a Repl
for maintenance clients over sockets. I started using the Repl example
from the Clojure wikibook. However in my running application the
expressions evaluated by the Repl trigger forks of new threads which
eventually show a result that should be send back to the Repl user over
the socket. But as the binding to *out* is not known in these forked
threads I will never see anything.

If I would need to explicitly say "keep *out*" I would need to do that
in application code that forks off the thread and this detail should be
of no concern to the application code that also doesn't know about the
Repl.

So I had to get the list of thread bindings which currently is not
exposed in Clojure (and I would be happy if Rich could add that). My
next problem with the Repl is, that eval calls Compiler.eval which might
also push thread bindings if the ClassLoader is not initialized,
effectively hiding the thread bindings established by binding in the
Repl code, which forced me to create a class loader before running eval
in the Repl.

The whole result looks rather messy and I would really like to see
Clojure being changed to provide an easy out of the box way to inherit
thread locals.


Here is the Repl-Code (note the clojure.lang.RT/makeClassLoader call):


(defn repl [nspace in out]
  (clojure.lang.Var/pushThreadBindings {clojure.lang.Compiler/LOADER 
(clojure.lang.RT/makeClassLoader)})
  (binding [clojure/*ns* (or (find-ns nspace) (create-ns nspace))
clojure/*warn-on-reflection* false
clojure/*out* (new OutputStreamWriter out)
clojure/*err* clojure/*out*]
(let [eof (new Object)
  r (new LineNumberingPushbackReader (new InputStreamReader in))]
  (println "Welcome User.")
  (print (ns-name *ns*) "\b=> ")
  (flush)
  (loop [e (read r false eof)]
(when-not (= e eof)
  (try
(prn (eval e))
  (catch Exception e
(.printStackTrace e)))
  (print (ns-name *ns*) "\b=> ")
  (flush)
  (recur (read r false eof))
  (clojure.lang.Var/popThreadBindings))



In the running Repl I will do things like:

app.maintenance=> (client! :connect "localhost" 8000)

which invokes a connect method which forkes off a thread to read from
the connection and print it out. Using on-thread:


(defn bindings []
  (reduce
(fn [env [n v]] (conj env n (var-get v)))
[]
(clojure.lang.Var/getThreadBindings)))

(defmacro on-thread
  ([f]
`(let [env# (bindings)]
   (eval `(on-thread ~env# ~~f
  ([env f]
`(doto (new Thread #(binding ~env (~f))) (start


"bindings" depends on the newly introduced Var.getThreadBindings()
method I posted last time. Again I don't like the eval there but I was
not able to get it working without it.

If I am completely off track here I would be grateful for corrections.
The only requirement I have is that my app code should not need to say
"keep just these bindings".

Thanks for reading this far ;-)



--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



#' use in clojure-contrib macros

2008-10-11 Thread Meikel Brandmeyer

Hi,

I also ran in the problem with #' in a macro with clojure-contrib.
The attached patch changes all macros in clojure-contrib to the
suggested way with (var).

Sincerely
Meikel


var-vs-reader.patch
Description: Binary data




smime.p7s
Description: S/MIME cryptographic signature


Re: Formatted printing?

2008-10-11 Thread Rich Hickey



On Oct 11, 8:54 am, "Paul Stadig" <[EMAIL PROTECTED]> wrote:
> I'm using a clojure source file as a database, by loading the file
> modifying it then saving it back to the filesystem. I'm trying to keep
> things simple.
>
> It would be useful to pretty print it so I could inspect it and make
> small changes, if necessary.
>
> It seems like a rather simple problem, and I only need some line
> breaks or something simple, some text editors have a problem with
> 1,000's of characters on one line. The other problem is that if there
> is an error loading the database Clojure says there's an error on line
> 1, which is the only line. I'll just code up something simple.
>
> Starting from some Scheme or CL code is a good idea.  I'll just adapt
> it to support literal maps, vectors, and such.
>

A pretty-print for Clojure would be a welcome contribution. In order
to be an acceptable contribution, though, it would have to be free of
connections to other code.

Rich


--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: #' use in clojure-contrib macros

2008-10-11 Thread Rich Hickey



On Oct 11, 9:26 am, Meikel Brandmeyer <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I also ran in the problem with #' in a macro with clojure-contrib.
> The attached patch changes all macros in clojure-contrib to the
> suggested way with (var).
>

Did you have a problem after rev 1059? I'm in the middle of making
some reader changes - if rev 1059 works then I recommend leaving your
code alone until I am done.

At that point, I will know if I am going to change the promise of #'x
becomes (var x), and let everyone know.

Rich

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Metadata on defmulti

2008-10-11 Thread Rich Hickey

On Oct 11, 2:33 am, Jeff V <[EMAIL PROTECTED]> wrote:
> Do multimethods support doc strings or other metadata?

defmulti had been replacing any metadata on the name with a type hint.
I've changed it (SVN rev 1060) so that it adds to any metadata on the
name.

So, for now, defmulti doesn't have the same doc string support as
defn, but you can get metadata on the resulting var by putting
metadata on the name, as with def:

(defmulti #^{:bar :baz :doc "foo's doc"} foo class)

Rich

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Clojure, emacs, slime, swank tutorial for Ubuntu

2008-10-11 Thread Aaron Bedra

rlwrap seems to be a much better solution.  I do use emacs as my daily
driver, but it's nice to have a backup on the command line.  I wrote a
getting started article for Ubuntu a while back if you want to check it out.

http://aaronbedra.com/2008/8/17/adventures-in-clojure-getting-started

I made it with Hardy Heron as the focus.  I have yet to see if it all
works on Intrepid Ibex.

Aaron

solkis wrote:
> I haven't tried configuring the REPL scripts to use rlwrap. I'm mostly
> learning within emacs where it may be a moot point anyway (not sure.)
> I know in emacs I use Alt-p get get past forms. I'm definitely open to
> modifying this config tutorial to use rlwrap if it's more stable when
> needed (like directly in a shell-based REPL.) I not sure in what
> situations that JLine is iffy as you mentioned. Can you give any quick
> suggestions on what sections I would need to modify to switch in
> rlwrap? As you can see I'm using the scripts as closely as possible
> from clojure-extra so that the tutorial stays basic.
>
> Thanks for the feedback,
>
> -Tim
>
> On Oct 10, 5:09 pm, James Reeves <[EMAIL PROTECTED]> wrote:
>   
>> On Oct 10, 10:22 pm, solkis <[EMAIL PROTECTED]> wrote:
>>
>> 
>>> I just created a basic clojure, emacs, slime, swank configuration
>>> tutorial for Ubuntu users
>>>   
>> I notice your tutorial uses JLine, which I've found to be rather iffy
>> under Linux. Have you tried the REPL with rlwrap? Or haven't you had
>> any problems with JLine?
>>
>> - James
>> 
> >
>
>   


--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Event Listeners in Functional Programming

2008-10-11 Thread CuppoJava

Yeah, Clojure provides all that I need for GUI programming.
I was just wondering if there's an elegant way of doing GUI in a pure-
functional way, without mutability.

On Oct 10, 7:58 pm, Chouser <[EMAIL PROTECTED]> wrote:
> On Fri, Oct 10, 2008 at 8:14 PM, CuppoJava <[EMAIL PROTECTED]> wrote:
>
> > The Java "idiom" for eg. a button, is to create a button object which
> > contains a list of "event listeners (closures/callback functions)"
> > that get called when the button is clicked.
>
> So what is it you want the button to do?  Mutability is no sin, and
> Clojure gives you tools to manage it.
>
> For example the click listener could dispatch an action to an agent,
> which would allow the GUI to continue promptly while the action works
> in another thread.  Would that be sufficient for what you want to
> accomplish?
>
> --Chouser
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: #' use in clojure-contrib macros

2008-10-11 Thread Meikel Brandmeyer

Hi,


Did you have a problem after rev 1059? I'm in the middle of making
some reader changes - if rev 1059 works then I recommend leaving your
code alone until I am done.

At that point, I will know if I am going to change the promise of #'x
becomes (var x), and let everyone know.

Checked. The problem is gone with >= 1059.

Sincerely
Meikel



smime.p7s
Description: S/MIME cryptographic signature


BUG: resultset-seq breaks on duplicate column names

2008-10-11 Thread Allen Rohner

If you create a SQL query that returns duplicate names, resultset-seq
throws an exception: "java.lang.RuntimeException:
java.lang.IllegalArgumentException: Too many arguments to struct
constructor"

i.e.

(let [rs (query "select description,description from table")]
   (resultset-seq rs)

The following patch adds a method duplicate-col-names? which returns
true if the resultset contains duplicate column names, and modifies
resultset-seq to throw an exception if there are duplicate column
names.

As an aside, now that clojure has real libraries, it seems that
boot.clj is not the best place for the resultset code.

Allen

Index: src/clj/clojure/boot.clj
===
--- src/clj/clojure/boot.clj(revision 1060)
+++ src/clj/clojure/boot.clj(working copy)
@@ -1901,10 +1901,26 @@
 (clojure.lang.LineNumberingPushbackReader.))]
 (load-reader rdr)))

+(defn set
+  "Returns a set of the distinct elements of coll."
+  [coll] (apply hash-set coll))
+
+(defn duplicate-col-names? [#^java.sql.ResultSet rs]
+  "returns true if the columns in the result set contain duplicate
names"
+  (let [rsmeta (. rs (getMetaData))
+   idxs (range 1 (inc (. rsmeta (getColumnCount
+   keys (map (comp keyword (memfn toLowerCase))
+ (map (fn [i] (. rsmeta (getColumnName i))) idxs))
+   unique-keys (set keys)]
+(not (= (count keys) (count unique-keys)
+
+
 (defn resultset-seq
   "Creates and returns a lazy sequence of structmaps corresponding to
   the rows in the java.sql.ResultSet rs"
   [#^java.sql.ResultSet rs]
+  (when (duplicate-col-names? rs)
+(throw (Exception. "resultset-seq does not handle queries with
duplicate column names")))
 (let [rsmeta (. rs (getMetaData))
   idxs (range 1 (inc (. rsmeta (getColumnCount
   keys (map (comp keyword (memfn toLowerCase))
@@ -1916,10 +1932,6 @@
 (lazy-cons (apply struct row-struct (row-values)) 
(thisfn]
   (rows)))

-(defn set
-  "Returns a set of the distinct elements of coll."
-  [coll] (apply hash-set coll))
-
 (defn #^{:private true}
   filter-key [keyfn pred amap]
 (loop [ret {} es (seq amap)]

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



REPL does not print stack trace in some cases

2008-10-11 Thread Allen Rohner

If you exercise the bug in my previous post about resultset-seq, the
repl will not print a stack trace, it will only print the name of the
exception and the message. The following patch modifies the repl to
print the stack trace.

Allen


Index: src/jvm/clojure/lang/Repl.java
===
--- src/jvm/clojure/lang/Repl.java  (revision 1060)
+++ src/jvm/clojure/lang/Repl.java  (working copy)
@@ -97,7 +97,10 @@
Throwable c = e;
while(c.getCause() != null)
c = c.getCause();
-   ((PrintWriter) RT.ERR.get()).println(e 
instanceof
Compiler.CompilerException ? e : c);
+  if(e instanceof
Compiler.CompilerException)
+  e.printStackTrace((PrintWriter)
RT.ERR.get());
+  else
+  c.printStackTrace((PrintWriter)
RT.ERR.get());
stare.set(e);
}
}

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



What Windows IDE are you using?

2008-10-11 Thread CuppoJava

Hi guys,
If anyone is using Windows, please share what environment you're using
to program in.

Currently I'm using Enclojure for Netbeans, but it's still new and a
few features are broken.

All I need is an IDE with:
  -method/parameter lookup (for clojure and java code)
  -syntax highlighting

Thank you very much.
  -Patrick

PS: I'm attempting to install Vim and the clojure plugin for windows
right now, but can't find a tutorial for windows and am having lots of
trouble. A link would be really helpful.
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Clojure Repl forcing lazy lists?

2008-10-11 Thread R. P. Dillon

I was noticing that a lazy list seems to get forced as soon as you
create it on the Repl because printing it forces evaluation.  (range 1
100), for example, produces:

(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
96 97 98 99)

Coming from Scala, If you create something like Stream.range(1,100) on
Scala's Repl, you'll get something back along the lines of:

res0: Stream[Int] = Stream(1, ?)

The question mark indicates that there is more to the Stream that
hasn't yet been calculated.  There are some other neat properties of
this.  It also allows you to see how far a Stream has been forced.
Continuing from above:

scala> res0.tail.head
res1: Int = 2
scala> res0
res2: Stream[Int] = Stream(1, 2, ?)

Would Clojure benefit from having (repl-print) or (lazy-print)
function that would not force lazy lists?  It could be activated by
default when on the Repl.

Cheers,
Rick

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: REPL does not print stack trace in some cases

2008-10-11 Thread Chouser

On Sat, Oct 11, 2008 at 1:26 PM, Allen Rohner <[EMAIL PROTECTED]> wrote:
>
> If you exercise the bug in my previous post about resultset-seq, the
> repl will not print a stack trace, it will only print the name of the
> exception and the message.

That's a feature!  I recent one, no less.

Seriously, people were complaining about giant "meaningless" stack
traces at the REPL, so the default now is to just print the exception
itself.  To get the stack trace after an exception has occurred, try:

(.printStackTrace *e)

--Chouser

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: REPL does not print stack trace in some cases

2008-10-11 Thread Allen Rohner

It's odd to me that that the stack traces were only removed in a few
instances. There are still plenty of places that do print traces.

I'm all in favor of getting rid of the "meaningless" stack traces,
once we have better error reporting.

Allen

On Oct 11, 2:36 pm, Chouser <[EMAIL PROTECTED]> wrote:
> On Sat, Oct 11, 2008 at 1:26 PM, Allen Rohner <[EMAIL PROTECTED]> wrote:
>
> > If you exercise the bug in my previous post about resultset-seq, the
> > repl will not print a stack trace, it will only print the name of the
> > exception and the message.
>
> That's a feature!  I recent one, no less.
>
> Seriously, people were complaining about giant "meaningless" stack
> traces at the REPL, so the default now is to just print the exception
> itself.  To get the stack trace after an exception has occurred, try:
>
> (.printStackTrace *e)
>
> --Chouser
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Clojure Repl forcing lazy lists?

2008-10-11 Thread Rich Hickey

On Sat, Oct 11, 2008 at 2:40 PM, R. P. Dillon <[EMAIL PROTECTED]> wrote:
>
> I was noticing that a lazy list seems to get forced as soon as you
> create it on the Repl because printing it forces evaluation.  (range 1
> 100), for example, produces:
>
> (1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
> 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
> 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
> 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
> 96 97 98 99)
>
> Coming from Scala, If you create something like Stream.range(1,100) on
> Scala's Repl, you'll get something back along the lines of:
>
> res0: Stream[Int] = Stream(1, ?)
>
> The question mark indicates that there is more to the Stream that
> hasn't yet been calculated.  There are some other neat properties of
> this.  It also allows you to see how far a Stream has been forced.
> Continuing from above:
>
> scala> res0.tail.head
> res1: Int = 2
> scala> res0
> res2: Stream[Int] = Stream(1, 2, ?)
>
> Would Clojure benefit from having (repl-print) or (lazy-print)
> function that would not force lazy lists?  It could be activated by
> default when on the Repl.
>

I think one problem with that for Clojure is that virtually all of the
sequence functions return lazy seqs and people often use them at the
repl where they intend to see all of the results. There isn't a
separate streams lib.

Rich

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: REPL does not print stack trace in some cases

2008-10-11 Thread Chouser

On Sat, Oct 11, 2008 at 3:50 PM, Allen Rohner <[EMAIL PROTECTED]> wrote:
>
> It's odd to me that that the stack traces were only removed in a few
> instances. There are still plenty of places that do print traces.

Such as where?  I haven't seen any at the repl in a while.  If a .clj
file loaded from the command line throws an exception, then I see the
stack trace, but that's a sort of "batch mode" where I guess people
were less unhappy about the long stack trace.

--Chouser

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: REPL does not print stack trace in some cases

2008-10-11 Thread Jim Menard

On Sat, Oct 11, 2008 at 6:14 PM, Chouser <[EMAIL PROTECTED]> wrote:
>
> On Sat, Oct 11, 2008 at 3:50 PM, Allen Rohner <[EMAIL PROTECTED]> wrote:
>>
>> It's odd to me that that the stack traces were only removed in a few
>> instances. There are still plenty of places that do print traces.
>
> Such as where?  I haven't seen any at the repl in a while.  If a .clj
> file loaded from the command line throws an exception, then I see the
> stack trace, but that's a sort of "batch mode" where I guess people
> were less unhappy about the long stack trace.

I see them all the time. I'm using the 20080906 download. Are you all
using the latest git/svn builds?

Jim

>
> --Chouser
>
> >
>



-- 
Jim Menard, [EMAIL PROTECTED], [EMAIL PROTECTED]
http://www.io.com/~jimm/

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



A workaround for thread-local binding.

2008-10-11 Thread CuppoJava

Hi,
I'm trying to do the following in Clojure, and I'm wondering if
there's a better way...
This creates a little window, with a button that prints "my env"
whenever it is clicked.

(ns main
  (:import
(javax.swing JFrame JButton)
(java.awt.event ActionListener)))

(def env)
(defn main [args]
  (binding [env "my env"]
(println env);< prints
fine. Because we're still in correct thread.
(let [frame (new JFrame)
  button (new JButton)]
  (. button addActionListener (proxy [ActionListener] []
(actionPerformed [evt] (println
env   ;<--- IllegalStateException: env is unbound
  (doto frame
(add button)
(setVisible true

This can be resolved by doing the following:

(def env)
(defn main [args]
  (binding [env "my env"]
(println env);< prints
fine. Because we're still in correct thread.
(let [frame (new JFrame)
  button (new JButton)
  env env]  ;<- Must include this line
  (. button addActionListener (proxy [ActionListener] []
(actionPerformed [evt] (println
env   ;<--- Works properly now.
  (doto frame
(add button)
(setVisible true

IllegalStateException is being thrown because the ActionListener code
is actually called from the Swing event loop which runs in a different
thread, where env is not bound. This is easily fixed by introducing a
variable inside a let, that is captured by the ActionListener
closure).

So this requires that the coder be aware of the circumstances in which
his closures are called (ie. in the same thread, in a different
thread, etc...). Is this a detail that the coder should be responsible
for?

Is it possible for closures to automatically capture the bindings for
the thread in which the closure was declared? Or does this have some
terrible consequences that I'm not yet seeing?

Anyway, it's something to think about.
  Thanks for your help.
 -Patrick
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



TransactionalHashMap

2008-10-11 Thread Mark McGranaghan

I was just wondering what the intended use is for
TransactionalHashMap. Its a Java class defined in the Clojure source,
but not used anywhere else in the core code.  How does this class fit
in with Clojure's PersistentHashMap and Java's ConcurrentHashMap?

- Mark
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: TransactionalHashMap

2008-10-11 Thread Chouser

On Sat, Oct 11, 2008 at 9:50 PM, Mark McGranaghan <[EMAIL PROTECTED]> wrote:
>
> I was just wondering what the intended use is for
> TransactionalHashMap. Its a Java class defined in the Clojure source,
> but not used anywhere else in the core code.  How does this class fit
> in with Clojure's PersistentHashMap and Java's ConcurrentHashMap?

"rhickey added TransactionalHashMap, an implementation of
java.util.ConcurrentHashMap that works in/with transactions"

http://clojure-log.n01se.net/date/2008-08-01.html#09:36

It looks to me like it's mutable, but all the data is actually stored
in refs, so you can only do your mutations in a transaction:

(def x
  (dosync
(doto (clojure.lang.TransactionalHashMap.)
  (put :a 1)
  (put :b 2

So: thread safe, transactional, mutable ConcurrentHashMap.  Maybe it'd
be useful for passing into Java libs?

Unfortunately you apparently can't print them in SVN 1063:

java.lang.ClassCastException: clojure.lang.PersistentHashMap cannot be
cast to java.util.Collection
at 
clojure.lang.TransactionalHashMap.entrySet(TransactionalHashMap.java:127)
at clojure.lang.RT.seqFrom(RT.java:463)
at clojure.lang.RT.seq(RT.java:452)
at clojure.seq__28.invoke(boot.clj:92)
at clojure.fn__1952$fn__1954.invoke(boot.clj:3524)
at clojure.print_ctor__1889.invoke(boot.clj:3433)
at clojure.fn__1952.invoke(boot.clj:3517)
at clojure.lang.MultiFn.invoke(MultiFn.java:152)
at clojure.lang.Var.invoke(Var.java:323)
at clojure.lang.RT.print(RT.java:1165)
at clojure.lang.Repl.main(Repl.java:88)

--Chouser

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: two dimensional arrays

2008-10-11 Thread Mark H.

On Oct 10, 10:56 am, Martin DeMello <[EMAIL PROTECTED]> wrote:
> On Oct 9, 12:29 pm, "Mark H." <[EMAIL PROTECTED]> wrote:
>
> > a mapping.  However, if you find yourself doing this a lot, you might
> > want to think about a more Clojure-like idiom that doesn't require
> > destructive updates and minimizes consing for local changes (e.g., a
> > quadtree).
>
> Interesting idea, but typical operations will be iterating along the
> cells in a given row or column, so a 2d doubly-linked list would
> probably work better than any sort of space-partitioning tree. But
> then I lose easy random access to a cell. Vector(-of-vectors) and {[x
> y] => cell} do seem like my best bets here.

Depends on what you want with the data structure.  Some people might
want to take slices of matrices -- e.g., the following (in Matlab
notation):

A( 1:2:end, 1:3:end )

which is a matrix containing every second row and every third column
of A.  Implementing this is much easier and more efficient if all the
data is in a single flat array, than if it's a vector of vectors.
Also, imagine the pain of matrix-matrix multiplication with a list-of-
lists.  But if you always mean to iterate rowwise over your 2-D array,
then list-of-lists is just fine.

mfh

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: two dimensional arrays

2008-10-11 Thread Mark H.

On Oct 11, 10:23 pm, "Mark H." <[EMAIL PROTECTED]> wrote:
> Some people might want to take slices of matrices -- e.g., the following
> (in Matlab notation):
>
> A( 1:2:end, 1:3:end )
>
> which is a matrix containing every second row and every third column
> of A.  

Speaking of which, what's the right idiom to take a 2-D slice of a 2-D
array in Clojure?  It's not really a seq because the slice implements
the same interface as an array (with random access to elements).
Also, a seq of seqs would constrain iteration either to a rowwise or
columnwise direction, whereas the user might want to iterate both ways
over the same slice (e.g., for the matrix-matrix multiplication C <- A
* A, one might iterate rowwise over the left A and columnwise over the
right A).

mfh
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---