Re: ANN: Clojure live-repl

2009-10-30 Thread Jeff Rose

Awesome!  Thanks a lot.  I've been needing this.

-Jeff

P.S. Maven is annoying.

On Oct 18, 6:53 pm, "David Powell"  wrote:
> Hi,
>
> I just posted a project at .
>
> It uses the Java Attach API to let you connect a Clojure REPL to any running
> Java or Clojure process, without them requiring any special startup.
>
> It probably requires a Sun 1.6 JDK.  And currently the startup script is a
> batch file, so if anyone can knock a Bourne shell script together that would
> be cool.
>
> --
> Dave
--~--~-~--~~~---~--~~
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: Clojure live-repl

2009-10-30 Thread Rick Moynihan

2009/10/18 David Powell :
>
> Hi,
>
> I just posted a project at .
>
> It uses the Java Attach API to let you connect a Clojure REPL to any running
> Java or Clojure process, without them requiring any special startup.
>

This is really cool and seems very useful for clojure projects...  You
mention it being usable for Java processes too though...  However it's
not very clear how useful this is.  Is there anything you can do to
interact with the process in any meaningful way?

Can you use the clojure REPL as an alternative to something like
jconsole?  Are there ways to browse mbeans/objects in the remote java
process?

R.

--~--~-~--~~~---~--~~
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: Newcomer's question about Clojure's compatibility with common lisp

2009-10-30 Thread Stefan Fehrenbach

Hi Julien.
On Thursday 29 October 2009 bal...@gmail.com wrote:
> Hello,
> 
> Next is my question: can I run common lisp programs on the jvm using
> clojure? I was thinking specifically to Maxima - (the formal calculus
> program)?
> 
Clojure is not source compatible to Common Lisp (and not intended to be).
If you want Common Lisp on the JVM you might want to look at ABCL 
(http://common-lisp.net/project/armedbear/). I don't know though whether it 
runs Maxima or not.
> 
> Thanks in advance,
> 
> Julien.
> 
HTH,
Stefan

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



Clojure contrib http-agent hangs when making a POST request

2009-10-30 Thread Alex

Hi,

I'm getting some strange errors when trying to make a POST request
using the Clojure contrib http-agent library (http://
richhickey.github.com/clojure-contrib/http.agent-api.html).

When I run:

(use 'clojure.contrib.http.agent)

(println (string (http-agent "http://www.google.com"; :method
"POST" :body "param=true")))

The REPL simply hangs forever.

However, when I run the following:

(def agt (http-agent "http://www.google.com"; :method "POST" :body
"param=true"))
;...wait a bit or add a (Thread/sleep 1000)
(println (string agt))

I get a correct response (Google saying it doesn't like POST requests)

Also, the (result ... ) function appears to work fine also:

(println (result (http-agent "http://www.google.com"; :method
"POST" :body "param=true")))

So it looks like something in the (string ... ) function is causing it
to hang if the response has not yet completed. Anyone have any idea
what might be causing this?

Thanks,

Alex

Does anyone have any ideas why

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



Generating Java and C# wrappers

2009-10-30 Thread John Ky
Hello,

I've been wondering if there was a way to specify the Java and C# wrapper
classes/interfaces to wrap Clojure code in Clojure, and then writing out
them to a file so that they can ge compiled by their respective compilers.

I'm asking this because in my work, I need to support these two languages,
but don't want to have to maintain two code bases.  At the same time, I want
to have an API that feels natural to that particular language: for instance,
that would meen getter/setter methods in Java and properties/events in C#.

Cheers,

-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: invoking macros from Java

2009-10-30 Thread Rich Hickey

On Wed, Oct 28, 2009 at 12:21 AM, Alex Osborne  wrote:
>
> Jeff Brown wrote:
>
>> I can invoke a function using Java code that looks something like this...
>>
>> Reader reader = new FileReader("clj/demo.clj");
>> Compiler.load(reader);
>> Var var = RT.var("demo", "add_numbers");
>> Object result = var.invoke(4, 7);
>> System.out.println("Result: " + result);
>>
>> I am not sure how to invoke even_sillier_adder.  Any help would be
>> appreciated.
>>
>
> Maybe I'm missing the point of the question, but can't you just do it
> exactly the same way?
>
>   Reader reader = new FileReader("clj/demo.clj");
>   Compiler.load(reader);
>   Var var = RT.var("demo", "even_sillier_adder");
>
>   Object result = var.invoke(4);
>   System.out.println("Result: " + result);
>   System.out.println("Result evaluated: " + Compiler.eval(result));
>
> Output:
>
>   Result: (foo/silly_adder 9)
>   Result evaluated: 9
>
> A macro is just a function that takes some code and returns some other
> code, so "invoking" a macro will return the code it evaluates to.  If
> you want that to be then evaluated, then just ask the compiler to
> evaluate it. ;-)
>

It is best not to rely on the fact that macros are implemented as
functions. They are like functions, and may be actual functions now,
but that is an implementation detail.

The way to do macroexpansion is via macroexpand and macroexpand-1,
both of which can be called from Java via binding RT.vars as above. If
you then want to evaluate the expansion, use eval.

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
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: Observations from a real-world Clojure project

2009-10-30 Thread Rich Hickey

On Thu, Oct 29, 2009 at 11:57 AM, Jamie  wrote:
>
> First, thank you Rich & Co for doing Clojure.
>
> We've been using Clojure for a real-world project, and I thought I'd
> share some observations.
>

Thanks - these kinds of experience reports are very useful.

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
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: Constructing Java Interop calls

2009-10-30 Thread Tiago Antão

On Thu, Oct 29, 2009 at 1:38 PM, Meikel Brandmeyer  wrote:
>> All good here, but, if I do the eval variation,
>> user=> (eval (list (symbol ".setFileSelectionMode") jfc 1))
>
> Another example which shows that eval is not worth the trouble. It is
> better to use reflection. You cannot embed the JFileChooser as a
> object into the code. You have to construct it in your eval. (eval `
> (let [chooser# (JFileChooser.)] (~(symbol ".setFileSelectionMode")
> chooser# 1) chosser#)).

Thanks for all the people that helped. But I just wonder, why?
I mean, why one cannot embed an object in the eval code?

Tiago

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



How to make lazy seq from socket input?

2009-10-30 Thread timc

Can someone suggest how to make the packets received on a socket into
a lazy sequence?
The application I'm making is a log file parser. A (Java) program is
producing log output using log4j, the output can go to file(s) or be
sent to a socket. So, the program has this outline:

(defn fileLines [fileNameSeq]
"Return a lazy sequence of lines from the given lazy sequence of file
names.
Return nil at end of file." ...)

(defn socketLines [ipAddr portN]
"Return a lazy sequence of lines from a server socket at the given
place.
Return nil if socket error." ...)

(defn parseLine [line] ... )

(defn parseLines [lineSeq]
"Parse the lines from lineSeq."
(loop [line (first lineSeq) tail (rest lineSeq)]
(when line
(parseLine line)
(recur (first tail) (rest tail)

I think I know how to do fileLines, but not socketLines. (Each
received packet should contain one line as it would have been written
to a file, I think). My problem is not how to manage a server socket,
but how to make an ISeq -- i.e. what exactly is the contract that ISeq
defines (the source code has no comments).

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: Newcomer's question about Clojure's compatibility with common lisp

2009-10-30 Thread Tassilo Horn

Daniel Simms  writes:

> On Thu, Oct 29, 2009 at 4:34 PM, Rayne  wrote:
>> but I would highly recommend that you just pull it from the github
>> repository.
>
> Especially if you're going to use clojure-contrib ...or is there some
> "release" of contrib synch'd to clojure releases that I missed
> somewhere?

Rayne suggested to get *ABCL* from github instead of using the
experimental ubuntu packages. ;-)

Bye,
Tassilo

--~--~-~--~~~---~--~~
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 make lazy seq from socket input?

2009-10-30 Thread Alex Osborne

timc wrote:

> I think I know how to do fileLines, but not socketLines. (Each
> received packet should contain one line as it would have been written
> to a file, I think).

Something like this?

(use 'clojure.contrib.duck-streams)

(read-lines (.getInputStream socket))

> My problem is not how to manage a server socket,
> but how to make an ISeq -- i.e. what exactly is the contract that ISeq
> defines (the source code has no comments).

You wouldn't usually implement ISeq directly in Clojure code, you'd use 
the lazy-seq function.  A simple example from clojure.core:

(defn repeat
   "Returns a lazy (infinite!, or length n if supplied) sequence of xs."
   ([x] (lazy-seq (cons x (repeat x
   ([n x] (take n (repeat x

It's lazy because lazy-seq doesn't evaluate it's body until you ask for 
it (by calling 'seq', 'first' or 'next' on it).  As you access the seq a 
linked list is generated.  So initially there's just a LazySeq object:

  

Then the body of lazy-seq is evaluated creating a cons cell of the value 
x and (repeat x) which evaluates to another LazySeq:

first next
  +---+---+
  | x | |> 
  +---+---+

And so on it repeats:

first nextfirst next
  +---+---++---+---+
  | x | |> | x | |> 
  +---+---++---+---+

So we could make a simple function that returns a sequence of lines by 
calling .readLine over and over on an object:

(defn readline-seq [rdr]
   (lazy-seq (cons (.readLine rdr) (readline-seq rdr

However, we also want to stop at the end of the file, so we put in a 
check for the line being null:

(defn readline-seq [rdr]
   (lazy-seq
 (when-let [line (.readLine rdr)]
   (cons line (readline-seq rdr)

This is exactly how core/line-seq is defined, and 
duck-streams/read-lines is the same except it also takes care of 
adapting whatever you pass in (like a File or InputStream) to a 
BufferedReader (which provides the readLine method).

As for ISeq, take a look at Range.java for a simplish example.  You just 
need to implement the two methods, first() which returns the first thing 
in the seq and next() which returns a new seq (ie another instance of 
your class) representing the next part of the seq (so seq.next().first() 
is the second item, seq.next().next().first() is the third item and so on).

Hope that explanation was clear enough.

--~--~-~--~~~---~--~~
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-10-30 Thread John Harrop
On Fri, Oct 30, 2009 at 12:05 AM, Stefan Arentz  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 :-)
>

See

http://groups.google.com/group/clojure/browse_thread/thread/5b197faf820ab77c/


or another recent thread about "actors". You'll need to wrap your function
body in (try ... (catch Throwable _ xxx)) to halt exceptions. (The "xxx" can
be any logging action you want to take, or nothing.)

--~--~-~--~~~---~--~~
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: Generating Java and C# wrappers

2009-10-30 Thread Stuart Sierra

On Oct 30, 6:18 am, John Ky  wrote:
> I've been wondering if there was a way to specify the Java and C# wrapper
> classes/interfaces to wrap Clojure code in Clojure, and then writing out
> them to a file so that they can ge compiled by their respective compilers.

I'm not sure I understand your question.  There are two distinct
versions of Clojure: the original, which targets the JVM, and
ClojureCLR, which targets the CLR.  Neither one generates Java or C#
source code; both generate bytecode for their respective runtimes.

ClojureCLR is at http://github.com/richhickey/clojure-clr

-SS
--~--~-~--~~~---~--~~
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 make lazy seq from socket input?

2009-10-30 Thread Meikel Brandmeyer

Hi,

if you have a stream, you can basically do:

(defn stream-seq
  [stream]
  (take-while #(<= 0 %) (repeatedly #(.read stream

This will give a character at a time. From there you can build the
lines and turn it into a seq of lines. Or can you can to the lower
level and use lazy-seq directly.

(defn stream-line-seq
  [stream]
  (lazy-seq
(loop [line []]
  (let [ch (.read stream)]
(cond
  (= \newline ch) (cons (apply str line) (stream-line-seq
stream))
  (<= 0 ch) (recur (conj line ch))
  :else (when (< 0 (count line)) (cons (apply str line)
nil)))

Probably not the best implementation, but well... Of course lines is
only an example. This can be used for any type of record.

Beware of laziness in combination with resources like streams!
(premature close etc.)

Hope this helps.

Sincerely
Meikel

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



Is running a script without leaving Clojure?

2009-10-30 Thread Arie van Wingerden
Hi,

when I run a Clojure script Clojure ends directly when the script is
finished.

I would like an option to keep the Clojure REPL open when the script has
finished. Is this possible somehow?

Currently I use something like this (in WinXP):
   set jar1="C:\Clojure\clojure.jar"
   set jar2="C:\Clojure\clojure-contrib.jar"
   set jar3="D:\data\src\Clojure\Book-ProgrammingClojure"
   java -cp %jar1%;%jar2%;%jar3% clojure.main %1 %2 %3 %4 %5 %6 %7 %8 %9

Hope anybody can help :-)

TIA,
   Arie

--~--~-~--~~~---~--~~
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-10-30 Thread Albert Cardona

How about:

(import '(java.util.concurrent Executors TimeUnit))

(let [s (Executors/newSingleThreadScheduledExecutor)]
  (.scheduleAtFixedRate s
#(try
  (println "I did it again")
  (catch Exception e
(.printStackTrace e)))
(long 0)
(long 1)
TimeUnit/SECONDS))


Admittedly very java-ish.

Albert
--
http://albert.rierol.net

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



Implementation of zipmap

2009-10-30 Thread John Harrop
What's up with this?

(defn zipmap
  "Returns a map with the keys mapped to the corresponding vals."
  [keys vals]
(loop [map {}
   ks (seq keys)
   vs (seq vals)]
  (if (and ks vs)
(recur (assoc map (first ks) (first vs))
   (next ks)
   (next vs))
map)))

Was something wrong with this?:

(defn my-zipmap
  "Returns a map with the keys mapped to the corresponding vals."
  [keys vals]
  (into {} (map vec (partition 2 (interleave keys vals)

:)

(Even snappier is (apply hash-map (interleave keys vals)) but that always
produces a hashmap, whereas both of the above produce an arraymap for small
enough sets of keys.)

--~--~-~--~~~---~--~~
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: Implementation of zipmap

2009-10-30 Thread Alex Osborne

John Harrop wrote:
> Was something wrong with this?:
> 
> (defn my-zipmap
>   "Returns a map with the keys mapped to the corresponding vals."
>   [keys vals]
>   (into {} (map vec (partition 2 (interleave keys vals)
> 
> :)

One reason might be that the original zipmap is 5-10 times faster for 
large numbers of entries as it doesn't create all the temporary seqs.

--~--~-~--~~~---~--~~
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: Implementation of zipmap

2009-10-30 Thread Chouser

On Fri, Oct 30, 2009 at 11:30 AM, Alex Osborne  wrote:
>
> John Harrop wrote:
>> Was something wrong with this?:
>>
>> (defn my-zipmap
>>   "Returns a map with the keys mapped to the corresponding vals."
>>   [keys vals]
>>   (into {} (map vec (partition 2 (interleave keys vals)
>>
>> :)
>
> One reason might be that the original zipmap is 5-10 times faster for
> large numbers of entries as it doesn't create all the temporary seqs.

Another might be that into, partition and interleave don't exist
yet when zipmap is defined.

--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
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: Implementation of zipmap

2009-10-30 Thread John Harrop
On Fri, Oct 30, 2009 at 11:37 AM, Chouser  wrote:

> On Fri, Oct 30, 2009 at 11:30 AM, Alex Osborne  wrote:
> >
> > John Harrop wrote:
> >> Was something wrong with this?:
> >>
> >> (defn my-zipmap
> >>   "Returns a map with the keys mapped to the corresponding vals."
> >>   [keys vals]
> >>   (into {} (map vec (partition 2 (interleave keys vals)
> >>
> >> :)
> >
> > One reason might be that the original zipmap is 5-10 times faster for
> > large numbers of entries as it doesn't create all the temporary seqs.
>
> Another might be that into, partition and interleave don't exist
> yet when zipmap is defined.


Eh. Implementation details.

That got me to poke in the source for into and:

(def
 #^{:arglists '([coll x] [coll x & xs])
:doc "conj[oin]. Returns a new collection with the xs
'added'. (conj nil item) returns (item).  The 'addition' may
happen at different 'places' depending on the concrete type."}
 conj (fn conj
([coll x] (. clojure.lang.RT (conj coll x)))
([coll x & xs]
 (if xs
   (recur (conj coll x) (first xs) (next xs))
   (conj coll x)

This is especially odd: the two-argument case appears to call itself
recursively to get the argument for a Java call.

--~--~-~--~~~---~--~~
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: Implementation of zipmap

2009-10-30 Thread Chouser

On Fri, Oct 30, 2009 at 11:44 AM, John Harrop  wrote:
> On Fri, Oct 30, 2009 at 11:37 AM, Chouser  wrote:
>>
>> On Fri, Oct 30, 2009 at 11:30 AM, Alex Osborne  wrote:
>> >
>> > John Harrop wrote:
>> >> Was something wrong with this?:
>> >>
>> >> (defn my-zipmap
>> >>   "Returns a map with the keys mapped to the corresponding vals."
>> >>   [keys vals]
>> >>   (into {} (map vec (partition 2 (interleave keys vals)
>> >>
>> >> :)
>> >
>> > One reason might be that the original zipmap is 5-10 times faster for
>> > large numbers of entries as it doesn't create all the temporary seqs.
>>
>> Another might be that into, partition and interleave don't exist
>> yet when zipmap is defined.
>
> Eh. Implementation details.

I thought that's what we were discussing: the implementation
and its details. :-)

> That got me to poke in the source for into and:
> (def
>  #^{:arglists '([coll x] [coll x & xs])
>     :doc "conj[oin]. Returns a new collection with the xs
>     'added'. (conj nil item) returns (item).  The 'addition' may
>     happen at different 'places' depending on the concrete type."}
>  conj (fn conj
>         ([coll x] (. clojure.lang.RT (conj coll x)))
>         ([coll x & xs]
>          (if xs
>            (recur (conj coll x) (first xs) (next xs))
>            (conj coll x)
> This is especially odd: the two-argument case appears to call itself
> recursively to get the argument for a Java call.

That's the old-style interop syntax playing tricks on you.

(. clojure.lang.RT (conj coll x))

is the same as

(clojure.lang.RT/conj coll x)

--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
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: Implementation of zipmap

2009-10-30 Thread Alex Osborne

Chouser wrote:
> On Fri, Oct 30, 2009 at 11:30 AM, Alex Osborne  wrote:
>> John Harrop wrote:
>>> Was something wrong with this?:
>>>
>>> (defn my-zipmap
>>>   "Returns a map with the keys mapped to the corresponding vals."
>>>   [keys vals]
>>>   (into {} (map vec (partition 2 (interleave keys vals)
>>>
>>> :)
>> One reason might be that the original zipmap is 5-10 times faster for
>> large numbers of entries as it doesn't create all the temporary seqs.
> 
> Another might be that into, partition and interleave don't exist
> yet when zipmap is defined.

Yep, although nothing else in core uses zipmap, so it could be moved 
after them.

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



Seq wrappers for arrays can morph

2009-10-30 Thread John Harrop
user=> (def x (int-array 3))
#'user/x
user=> x
[0, 0, 0]
user=> (def y (seq x))
#'user/y
user=> (first y)
0
user=> (aset x 1 3)
3
user=> x
[0, 3, 0]
user=> (second y)
3
user=> (aset x 0 2)
2
user=> x
[2, 3, 0]
user=> (first y)
2

Here, (first y) returned first 0, then 2 without y being rebound in between.
This is a bit disturbing; some seqs can't be counted on to remain constant.
I'd expected (first y) once produced as a 0 to be carved in stone, while
(second y) would be 3 because it would be lazily produced from the array
only after the corresponding array cell was changed. For this, the seq
implementation would be something analogous to

(defn lazy-array-seq
  ([arr]
(lazy-array-seq arr 0))
  ([arr from-idx]
(lazy-array-seq arr 0 (count arr)))
  ([arr from-idx end-idx]
(if-not (= from-idx end-idx)
  (lazy-seq (aget arr from-idx) (lazy-array-seq arr (inc from-idx)
end-idx)

but apparently it's actually just a very thin wrapper around the array,
something like a "Java Iterator with copy-on-next semantics".

Perhaps it should be changed to behave something more like the
lazy-array-seq function above?

Probably the seq wrappers for java.util collections exhibit the same ... I
hesitate to call it a "bug", but I'm not sure it's the best choice of
semantics. My feeling is that once realized seq values should never change,
however the seq was produced.

--~--~-~--~~~---~--~~
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 make lazy seq from socket input?

2009-10-30 Thread timc

Thanks for the help.

On Oct 30, 1:23 pm, Meikel Brandmeyer  wrote:
> Hi,
>
> if you have a stream, you can basically do:
>
> (defn stream-seq
>   [stream]
>   (take-while #(<= 0 %) (repeatedly #(.read stream
>
> This will give a character at a time. From there you can build the
> lines and turn it into a seq of lines. Or can you can to the lower
> level and use lazy-seq directly.
>
> (defn stream-line-seq
>   [stream]
>   (lazy-seq
>     (loop [line []]
>       (let [ch (.read stream)]
>         (cond
>           (= \newline ch) (cons (apply str line) (stream-line-seq
> stream))
>           (<= 0 ch) (recur (conj line ch))
>           :else (when (< 0 (count line)) (cons (apply str line)
> nil)))
>
> Probably not the best implementation, but well... Of course lines is
> only an example. This can be used for any type of record.
>
> Beware of laziness in combination with resources like streams!
> (premature close etc.)
>
> Hope this helps.
>
> Sincerely
> Meikel
--~--~-~--~~~---~--~~
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: Newcomer's question about Clojure's compatibility with common lisp

2009-10-30 Thread Rayne

No, I didn't.

On Oct 30, 8:28 am, Tassilo Horn  wrote:
> Daniel Simms  writes:
> > On Thu, Oct 29, 2009 at 4:34 PM, Rayne  wrote:
> >> but I would highly recommend that you just pull it from the github
> >> repository.
>
> > Especially if you're going to use clojure-contrib ...or is there some
> > "release" of contrib synch'd to clojure releases that I missed
> > somewhere?
>
> Rayne suggested to get *ABCL* from github instead of using the
> experimental ubuntu packages. ;-)
>
> Bye,
> Tassilo
--~--~-~--~~~---~--~~
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: Is running a script without leaving Clojure?

2009-10-30 Thread Chouser

On Fri, Oct 30, 2009 at 10:23 AM, Arie van Wingerden  wrote:
> Hi,
>
> when I run a Clojure script Clojure ends directly when the script is
> finished.
>
> I would like an option to keep the Clojure REPL open when the script has
> finished. Is this possible somehow?
>
> Currently I use something like this (in WinXP):
>    set jar1="C:\Clojure\clojure.jar"
>    set jar2="C:\Clojure\clojure-contrib.jar"
>    set jar3="D:\data\src\Clojure\Book-ProgrammingClojure"
>    java -cp %jar1%;%jar2%;%jar3% clojure.main %1 %2 %3 %4 %5 %6 %7 %8 %9
>
> Hope anybody can help :-)

java -cp ~/build/clojure/clojure.jar clojure.main -i some_script.clj -r

--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
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: Constructing Java Interop calls

2009-10-30 Thread Kevin Downey

eval calls read for somethings.

2009/10/30 Tiago Antão :
>
> On Thu, Oct 29, 2009 at 1:38 PM, Meikel Brandmeyer  wrote:
>>> All good here, but, if I do the eval variation,
>>> user=> (eval (list (symbol ".setFileSelectionMode") jfc 1))
>>
>> Another example which shows that eval is not worth the trouble. It is
>> better to use reflection. You cannot embed the JFileChooser as a
>> object into the code. You have to construct it in your eval. (eval `
>> (let [chooser# (JFileChooser.)] (~(symbol ".setFileSelectionMode")
>> chooser# 1) chosser#)).
>
> Thanks for all the people that helped. But I just wonder, why?
> I mean, why one cannot embed an object in the eval code?
>
> Tiago
>
> >
>



-- 
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

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



Running out of memory when using loop/recur and destructuring

2009-10-30 Thread Paul Mooser

I was working with a large data set earlier today, and I had written a
loop/recur where I was passing in a huge seq to the first iteration,
and I was surprised when I ran out of heap space, because I was very
careful not to hold on to the head of the seq, and I though that loop
ended up rebinding all of its params (and didn't hold on to the
initial values).

When I run the following code:

(defn loop-test []
  (loop [[head & tail] (repeat 1)]
(recur tail)))

It will blow the heap on a 1.5 JDK, but I can't seem to make that
happen under a 1.6 JDK. I ran into a previous issue that was similar
(with respect to only manifesting on an older JDK), and it turned out
to be a bug in clojure (fixed in svn 1153).

I noticed that this doesn't occur if I don't use destructuring:

(defn loop-test2 []
  (loop [s (repeat 1)]
(recur (rest s

This version seems to run forever on either JDK, using a constant
amount of heap space.

Is this behavior due to some artifact of destructuring I'm not aware
of (or something else I'm missing), or is there a bug? If it sounds
like a bug, can anyone else reproduce?

Thanks!
--~--~-~--~~~---~--~~
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: Observations from a real-world Clojure project

2009-10-30 Thread Spencer Schumann

On Oct 29, 9:57 am, Jamie  wrote:
> 5. The functionality of the docs hasn't kept up with Clojure.  We
> often resorted to text searches of the various sources.  Need links
> and see-also's.  Clojure has grown/matured so much that it needs a doc
> system of some sort.

I recently started learning Clojure myself, and I agree that the docs
could use more work.  The book "Programming Clojure" was a good
introduction to the language, but it's not a complete reference.

I'd like to see something like Lua's language manual for Clojure.
Lua's manual (http://www.lua.org/manual/5.1) is concise, complete, and
accurate.  It's extremely helpful to have a single, de-facto guide to
every feature in the language.  I wish every language had a freely
available document of similar quality, and perhaps it could serve as a
model for a Clojure language manual.

--~--~-~--~~~---~--~~
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-10-30 Thread AndrewC.



On 30 Oct, 16:18, Albert Cardona  wrote:
> How about:
>
> (import '(java.util.concurrent Executors TimeUnit))

..

> Admittedly very java-ish.

Personally, I think Java-ish is the way to go here. John's actor lib
is pretty nifty, but it is relying on implementation details of the
threading of agent send handlers to try and ensure timely activity.
This seems like a case where we need certain functionality, Java has a
good library (the Executor framework, or Quartz), therefore we should
just use it.

--~--~-~--~~~---~--~~
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: Newcomer's question about Clojure's compatibility with common lisp

2009-10-30 Thread rob

Clojure is actually an entirely different language in the Lisp family
of languages.  In addition to ABCL (Common Lisp on the JVM), there are
also 2 or 3 Scheme implementations on the JVM.

On Oct 29, 8:09 am, "bal...@gmail.com"  wrote:
> Hello,
>
> First let me congratulate the clojure team on this wonderful
> initiative: porting lisp to the jvm.
>
> Next is my question: can I run common lisp programs on the jvm using
> clojure? I was thinking specifically to Maxima - (the formal calculus
> program)?
>
> And finally another question: are there any ubuntu packages for
> clojure available at this time?
>
> Thanks in advance,
>
> Julien.
--~--~-~--~~~---~--~~
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: Seq wrappers for arrays can morph

2009-10-30 Thread John Newman
When someone knowingly dips into arrays, though, aren't doing so because
they require java's semantics?  For speed, interop, or whatever?

We want to champion functional programming, but on the other hand we want to
preserve the smooth java-interop use-cases. Not an easy balancing act, I
suppose.

On Fri, Oct 30, 2009 at 9:10 PM, John Harrop  wrote:

> user=> (def x (int-array 3))
> #'user/x
> user=> x
> [0, 0, 0]
> user=> (def y (seq x))
> #'user/y
> user=> (first y)
> 0
> user=> (aset x 1 3)
> 3
> user=> x
> [0, 3, 0]
> user=> (second y)
> 3
> user=> (aset x 0 2)
> 2
> user=> x
> [2, 3, 0]
> user=> (first y)
> 2
>
> Here, (first y) returned first 0, then 2 without y being rebound in
> between. This is a bit disturbing; some seqs can't be counted on to remain
> constant. I'd expected (first y) once produced as a 0 to be carved in stone,
> while (second y) would be 3 because it would be lazily produced from the
> array only after the corresponding array cell was changed. For this, the seq
> implementation would be something analogous to
>
> (defn lazy-array-seq
>   ([arr]
> (lazy-array-seq arr 0))
>   ([arr from-idx]
> (lazy-array-seq arr 0 (count arr)))
>   ([arr from-idx end-idx]
> (if-not (= from-idx end-idx)
>   (lazy-seq (aget arr from-idx) (lazy-array-seq arr (inc from-idx)
> end-idx)
>
> but apparently it's actually just a very thin wrapper around the array,
> something like a "Java Iterator with copy-on-next semantics".
>
> Perhaps it should be changed to behave something more like the
> lazy-array-seq function above?
>
> Probably the seq wrappers for java.util collections exhibit the same ... I
> hesitate to call it a "bug", but I'm not sure it's the best choice of
> semantics. My feeling is that once realized seq values should never change,
> however the seq was produced.
>
>
> >
>


-- 
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: Newcomer's question about Clojure's compatibility with common lisp

2009-10-30 Thread Daniel Simms

On Thu, Oct 29, 2009 at 5:18 PM, Alex Osborne  wrote:
> There's a 1.0 compatible branch on github.  [...]

Thanks, I missed that branch.

--~--~-~--~~~---~--~~
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: Clojure contrib http-agent hangs when making a POST request

2009-10-30 Thread Rob Wolfe

Alex  writes:

> Hi,
>
> I'm getting some strange errors when trying to make a POST request
> using the Clojure contrib http-agent library (http://
> richhickey.github.com/clojure-contrib/http.agent-api.html).
>
> When I run:
>
> (use 'clojure.contrib.http.agent)
>
> (println (string (http-agent "http://www.google.com"; :method
> "POST" :body "param=true")))
>
> The REPL simply hangs forever.
>
> However, when I run the following:
>
> (def agt (http-agent "http://www.google.com"; :method "POST" :body
> "param=true"))
> ;...wait a bit or add a (Thread/sleep 1000)
> (println (string agt))
>
> I get a correct response (Google saying it doesn't like POST requests)
>
> Also, the (result ... ) function appears to work fine also:
>
> (println (result (http-agent "http://www.google.com"; :method
> "POST" :body "param=true")))
>
> So it looks like something in the (string ... ) function is causing it
> to hang if the response has not yet completed. Anyone have any idea
> what might be causing this?

I guess the problem is in "string" function, because it tries
to get "content encoding" of stream which is not available.
In this case output stream was presumably closed by server.
So I can see two solutions:

1) always waiting until request is completed using "result" function


(ns test2
  (:require [clojure.contrib.http.agent :as http]
[clojure.contrib.duck-streams :as ds]))

(let [agnt
  (http/http-agent "http://www.google.com"; 
   :method "POST" 
   :body "param=true")]
  (http/result agnt)
  (println "string: " (http/string agnt)))
(shutdown-agents)


2) applying this patch on original clojure.contrib.http.agent,
   which imho solves this problem


--- a/src/clojure/contrib/http/agent.clj
+++ b/src/clojure/contrib/http/agent.clj
@@ -263,9 +263,12 @@
   headers, or clojure.contrib.duck-streams/*default-encoding* if it is
   not specified."
   ([http-agnt]
- (string http-agnt (or (.getContentEncoding
-#^HttpURLConnection (::connection @http-agnt))
-   duck/*default-encoding*)))
+ (let [a @http-agnt]
+   (if (= (::state a) ::receiving)
+ (string http-agnt (or (.getContentEncoding
+#^HttpURLConnection (::connection @http-agnt))
+   duck/*default-encoding*))
+ (string http-agnt duck/*default-encoding*
   ([http-agnt #^String encoding]
  (.toString (get-byte-buffer http-agnt) encoding)))


HTH,
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: Observations from a real-world Clojure project

2009-10-30 Thread Tom Faulhaber


>
> 5. The functionality of the docs hasn't kept up with Clojure.  We
> often resorted to text searches of the various sources.  Need links
> and see-also's.  Clojure has grown/matured so much that it needs a doc
> system of some sort.
>


This has been recognized for awhile now. I have promised to extend the
documentation system we use for clojure.contrib (see
http://richhickey.github.com/clojure-contrib/) to clojure itself. This
will mean that we have up-to-the-minute doc on the latest Clojure (as
well as info for historical versions and, eventually, all the
branches) with links to the source code, the ability to attach more
detailed docs, link to URLs, full index, etc.

This has been taking awhile because my real life (both work and
personal) has been ridiculously crazy the last few months. But I have
begun to lean in on it and think I should have a preliminary version
running with the next couple of weeks.

The goal is that this system will be generalizable for all projects
and can be used as a sort of "javadoc" for Clojure libraries (with
extra bonuses like direct github support).

Tom
--~--~-~--~~~---~--~~
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 make lazy seq from socket input?

2009-10-30 Thread Tom Faulhaber

One thing to keep in mind, when using sockets, is that TCP does not
guarantee to keep packetization across the network. That is, just
because you're writing lines, doesn't mean that read will return
lines. TCP can put writes together or break them into parts or some
combination of both.

So if you need a seq of lines, you need to look at the individual
bytes and find the line breaks. But you probably won't want to read
each byte separately, use a BufferedReader or some such.

HTH,

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



idiom questions

2009-10-30 Thread Chick Corea

Is everything in Clojure immutable?  For example, w/ this code-snippet

   (let [x nil]
  ;; do something and modify 'x'
 )

how does one modify the value of 'x' ?

(let [x nil] (def x true))

this doesn't work.  the "def' interns and defines a (dynamic) root-
binding
for 'x' which is accessible outside the lexical closure.

I read in this page (http://clojure.org/special_forms) that lexical
bindings
are write-once/read-many.

  (in bold print)
"Locals created with let are not variables. Once created their
values never change!"

This description of 'set!' (http://clojure.org/vars#set)  reinforces
that statement.
Sure enough - it's true.

user=> (let [x nil] (set! x true))
java.lang.IllegalArgumentException: Invalid assignment target
(NO_SOURCE_FILE:7)

This leads me to observe that one cannot simply declare a name/symbol
as lexical
as one does in other lisps, a la

(let (x y) (setq x 1 y 2))

One could use the idiom that I used above, i.e., to assign "nil".  But
that's useless
b/c it's read-only beyond that expression.

So I want to ask, am I in the minority in thinking that read-only
local bindings limit
the programmer a lot.  The only alternative that I see (w/ my limited
imagination)
is to create a "let" scope at the exact place that one needs it.  But,
IMO, that
sucks - sorry for the vernacular; but it is wildly inconvenient for me
for many reasons.

Do both of these statements intern a symbol?  Or does the 2nd set the
currently
interned symbol to a new value ?

  (def x 1)
  (def x 2)

Is the proper way to change the value bound to a symbol to use
"set!" ?

  (set! x 3)

If I understand correctly, the "(binding ...)" macro specifically
applies to
"Variables", one of Clojure's STM types, thus has dynamic, thead-local
scope.  Is that right?  In which case, it may hide a lexical binding ?

Thanks for any help.  These details stop me from reaching that
critical
point in which code flows from my mind into the computer.


CHICKEE

--~--~-~--~~~---~--~~
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: Is running a script without leaving Clojure?

2009-10-30 Thread Chick Corea



On Oct 30, 1:37 pm, Chouser  wrote:
> On Fri, Oct 30, 2009 at 10:23 AM, Arie van Wingerden  
> wrote:
>
> > Hi,
>
> > when I run a Clojure script Clojure ends directly when the script is
> > finished.
>
>
> java -cp ~/build/clojure/clojure.jar clojure.main -i some_script.clj -r
>

Where are those and other CLI options documented?  I have not found
them.

A "usage" or "help" message could help w/ this, too.


CHICKEE

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



can I make this faster (and leaner) ?

2009-10-30 Thread Chick Corea

How do I make this code faster?  And leaner, i.e., use less memory ?

It's a simple function to generate NUMKEYS strings of length
KEYLENGTH,
store them in a Java array then store them in a HashMap
hash-map.  [it doesn't store them in the hash-map yet; but it
allocates it]

(set! *warn-on-reflection* true)

(def *valid-chars* [ \a \b \c \d \e \f \g \h \i \j \k \l \m
 \n \o \p \q \r \s \t \u \v \w \x \u \z
 \0 \1 \2 \3 \4 \5 \6 \7 \8 \9 ] )

(defn generate-random-keys [ numkeys keylength ]
  (time
   (let [ rand (new java.util.Random)
 key (make-array Character/TYPE keylength) ;;  :initial-
element \a))
 keys (make-array String numkeys)
 data (new java.util.HashMap (int numkeys))
 ]
 (.println System/out (format "generating [%d] keys of length
[%d]" numkeys keylength ))
 (dotimes [i numkeys]
   (dotimes [j keylength]
 (aset key j (nth *valid-chars* (. rand nextInt (int 36)
   (let [tmp (String. #^chars key) ]
 ;;   (.println System/out (format "keys[%d] => [%s]" i
tmp))
 (aset keys i tmp)))
 (.println System/out (format "inserting [%d] keys into HashMap"
numkeys)

(generate-random-keys 10 1024)  ;; 100k strings, 1024-chars each

The corresponding Java code (w/ the hash-insert omitted in the clojure
version)
runs in 5.5sec and uses 290MB.

This code runs (which omits the hash-insert) runs in 17.8sec and uses
353MB.

I thought that I added all of the casts and "warn-on-reflections" that
would help.
Also, I thought using Java data structures directly would bypass some
of Clojure's
overhead.  What am I missing?

Granted, the initial memory allocated is

* for POJ, ~12MB
* for Clojure, ~90MB

That may or may  not be relevant.  It's hard to know w/ auto mem-mgmt.


CHICKEE


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

2009-10-30 Thread David Nolen
On Fri, Oct 30, 2009 at 4:42 PM, Chick Corea  wrote:

>
> Is everything in Clojure immutable?  For example, w/ this code-snippet
>
>   (let [x nil]
>  ;; do something and modify 'x'
> )
>
> how does one modify the value of 'x' ?
>
>(let [x nil] (def x true))
>
> this doesn't work.  the "def' interns and defines a (dynamic) root-
> binding
> for 'x' which is accessible outside the lexical closure.
>

Welcome to functional programming :) Yes defs like that are generally looked
down upon because all defs are top-level anyway.


> So I want to ask, am I in the minority in thinking that read-only
> local bindings limit
> the programmer a lot.  The only alternative that I see (w/ my limited
> imagination)
> is to create a "let" scope at the exact place that one needs it.  But,
> IMO, that
> sucks - sorry for the vernacular; but it is wildly inconvenient for me
> for many reasons.
>

That's the whole point of FP, referential transparency. With assignment you
lose that. Clojure provides a great number of tools for dealing with FP
style programming. If you really need mutability use a reference type- atom,
ref.

(def x (atom 0))
(reset! x 1)
@x -> 1


> Do both of these statements intern a symbol?  Or does the 2nd set the
> currently
> interned symbol to a new value ?
>
>  (def x 1)
>  (def x 2)
>

I believe this means:

define a new var with the symbol 'x, bind it to the value 1.
bind that var to the value 2.

While it takes some time to get your head around the effect on programming
style, you'll be pleasantly surprised when you decide to add concurrency
into your program. Even if you don't, I think you'll find your programs
generally easier to debug.

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

2009-10-30 Thread Howard Lewis Ship

When I first looked at Clojure, I didn't get it (I scanned the docs
for 10 - 15 minutes).  A few month later, Stu Halloway said to give it
a second look and boy am I glad I did. Go read Stu's book, or at least
the first couple of chapters online at Manning.  Digest for a bit.
It'll be an eye opener.

On Fri, Oct 30, 2009 at 4:20 PM, David Nolen  wrote:
> On Fri, Oct 30, 2009 at 4:42 PM, Chick Corea  wrote:
>>
>> Is everything in Clojure immutable?  For example, w/ this code-snippet
>>
>>       (let [x nil]
>>              ;; do something and modify 'x'
>>             )
>>
>> how does one modify the value of 'x' ?
>>
>>        (let [x nil] (def x true))
>>
>> this doesn't work.  the "def' interns and defines a (dynamic) root-
>> binding
>> for 'x' which is accessible outside the lexical closure.
>
> Welcome to functional programming :) Yes defs like that are generally looked
> down upon because all defs are top-level anyway.
>
>>
>> So I want to ask, am I in the minority in thinking that read-only
>> local bindings limit
>> the programmer a lot.  The only alternative that I see (w/ my limited
>> imagination)
>> is to create a "let" scope at the exact place that one needs it.  But,
>> IMO, that
>> sucks - sorry for the vernacular; but it is wildly inconvenient for me
>> for many reasons.
>
> That's the whole point of FP, referential transparency. With assignment you
> lose that. Clojure provides a great number of tools for dealing with FP
> style programming. If you really need mutability use a reference type- atom,
> ref.
> (def x (atom 0))
> (reset! x 1)
> @x -> 1
>
>>
>> Do both of these statements intern a symbol?  Or does the 2nd set the
>> currently
>> interned symbol to a new value ?
>>
>>      (def x 1)
>>      (def x 2)
>
> I believe this means:
> define a new var with the symbol 'x, bind it to the value 1.
> bind that var to the value 2.
> While it takes some time to get your head around the effect on programming
> style, you'll be pleasantly surprised when you decide to add concurrency
> into your program. Even if you don't, I think you'll find your programs
> generally easier to debug.
>
> >
>



-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

--~--~-~--~~~---~--~~
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: Is running a script without leaving Clojure?

2009-10-30 Thread Chouser

On Fri, Oct 30, 2009 at 4:44 PM, Chick Corea  wrote:
>
> Where are those and other CLI options documented?  I have not found
> them.
>
> A "usage" or "help" message could help w/ this, too.

java -cp clojure.jar clojure.main --help

--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
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: can I make this faster (and leaner) ?

2009-10-30 Thread David Nolen
The better question to ask is this real code you intend on using? Are you
really going to generate 100,000 random keys of 1024 chars each all at once
in a real running program?

(def *valid-chars* [\a \b \c \d \e \f \g \h \i \j \k \l \m
\n \o \p \q \r \s \t \u \v \w \x \u \z
\0 \1 \2 \3 \4 \5 \6 \7 \8 \9])
(def *len* (count *valid-chars*))
(defn rand-char [] (nth *valid-chars* (rand-int *len*)))
(defn generate-key [keylength]
  (take keylength (repeatedly rand-char)))

(apply str (generate-key 1024)))

This program is about twice as slow as your optimized Clojure.

If you actually need to generate all these keys at once then why not just
write it in Java and make a nice Clojure function wrapper for it? If you're
aren't going to generate all these keys at once then this cost is
distributed over the actual runtime behavior of your program and in that
case, does it really matter for your specific use case?

On Fri, Oct 30, 2009 at 4:56 PM, Chick Corea  wrote:

>
> How do I make this code faster?  And leaner, i.e., use less memory ?
>
> It's a simple function to generate NUMKEYS strings of length
> KEYLENGTH,
> store them in a Java array then store them in a HashMap
> hash-map.  [it doesn't store them in the hash-map yet; but it
> allocates it]
>
> (set! *warn-on-reflection* true)
>
> (def *valid-chars* [ \a \b \c \d \e \f \g \h \i \j \k \l \m
> \n \o \p \q \r \s \t \u \v \w \x \u \z
> \0 \1 \2 \3 \4 \5 \6 \7 \8 \9 ] )
>
> (defn generate-random-keys [ numkeys keylength ]
>  (time
>   (let [ rand (new java.util.Random)
> key (make-array Character/TYPE keylength) ;;  :initial-
> element \a))
> keys (make-array String numkeys)
> data (new java.util.HashMap (int numkeys))
> ]
> (.println System/out (format "generating [%d] keys of length
> [%d]" numkeys keylength ))
> (dotimes [i numkeys]
>   (dotimes [j keylength]
> (aset key j (nth *valid-chars* (. rand nextInt (int 36)
>   (let [tmp (String. #^chars key) ]
> ;;   (.println System/out (format "keys[%d] => [%s]" i
> tmp))
> (aset keys i tmp)))
> (.println System/out (format "inserting [%d] keys into HashMap"
> numkeys)
>
> (generate-random-keys 10 1024)  ;; 100k strings, 1024-chars each
>
> The corresponding Java code (w/ the hash-insert omitted in the clojure
> version)
> runs in 5.5sec and uses 290MB.
>
> This code runs (which omits the hash-insert) runs in 17.8sec and uses
> 353MB.
>
> I thought that I added all of the casts and "warn-on-reflections" that
> would help.
> Also, I thought using Java data structures directly would bypass some
> of Clojure's
> overhead.  What am I missing?
>
> Granted, the initial memory allocated is
>
>* for POJ, ~12MB
>* for Clojure, ~90MB
>
> That may or may  not be relevant.  It's hard to know w/ auto mem-mgmt.
>
>
> CHICKEE
>
>
> >
>

--~--~-~--~~~---~--~~
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: Seq wrappers for arrays can morph

2009-10-30 Thread John Harrop
On Fri, Oct 30, 2009 at 12:40 PM, John Harrop  wrote:

> (defn lazy-array-seq
>   ([arr]
> (lazy-array-seq arr 0))
>   ([arr from-idx]
> (lazy-array-seq arr 0 (count arr)))
>   ([arr from-idx end-idx]
> (if-not (= from-idx end-idx)
>   (lazy-seq (aget arr from-idx) (lazy-array-seq arr (inc from-idx)
> end-idx)
>

Pardon me. I was in a bit of a hurry just on my way out the door. Try:

(defn lazy-array-seq
  ([arr]
(lazy-array-seq arr 0))
  ([arr from-idx]
(lazy-array-seq arr 0 (count arr)))
  ([arr from-idx end-idx]
(lazy-seq
  (if-not (= from-idx end-idx)
(cons (aget arr from-idx) (lazy-array-seq arr (inc from-idx)
end-idx))

user=> (def x (int-array 3))
#'user/x
user=> x
[0, 0, 0]
user=> (def y (lazy-array-seq x))
#'user/y
user=> (first y)
0
user=> (aset x 1 3)
3
user=> x
[0, 3, 0]
user=> (second y)
3
user=> (aset x 0 2)
2
user=> x
[2, 3, 0]
user=> (first y)
0
user=> y
(0 3 0)

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

2009-10-30 Thread Alex Osborne

Chick Corea wrote:
> Is everything in Clojure immutable?

Most things.  Clojure tries very hard to be thread-safe by default.

> how does one modify the value of 'x' ?
> 
> (let [x nil] (def x true))

One does not, at least not in a let-binding.  If you really want a 
lexical variable you can get one with a var:

(with-local-vars [x 3]
   (println (var-get x))
   (var-set x 5)
   (println (var-get x)))

It is thread-local to enforce thread-safety.  But as the others have 
said you should really be doing things functionally not imperatively. 
I've actually never used with-local-vars in a real program.

> So I want to ask, am I in the minority in thinking that read-only
> local bindings limit
> the programmer a lot.

You probably just have "gotten" functional programming yet.  If you use 
functions like map, reduce and for and recursion instead of traditional 
looping you basically find yourself not needing mutable local bindings.

> Do both of these statements intern a symbol?  Or does the 2nd set the
> currently
> interned symbol to a new value ?
> 
>   (def x 1)
>   (def x 2)

This changes the root binding of the var named by x to a new value, it 
does not create a new var.  The reason Clojure allows this is to make 
interactive development and debugging easier (so you can redefine a 
function at run-time).

user> (def x 1)
user> (def a #'x)
user> (def x 2)
user> (identical? a #'x)
true
user> a
#'user/x
user> (type a)
clojure.lang.Var


> Is the proper way to change the value bound to a symbol to use
> "set!" ?
> 
>   (set! x 3)

> If I understand correctly, the "(binding ...)" macro specifically
> applies to
> "Variables", one of Clojure's STM types, thus has dynamic, thead-local
> scope.  Is that right?  In which case, it may hide a lexical binding ?

Yes, I think you are basically correct.  Maybe this example will help:

(def foo 5) ; thread-global nearly immutable root binding

(binding [foo 7] ; creates a thread-local binding for x
   (set! foo 9) ; changes the thread-local binding

   (let [t (Thread. (fn []
 (println "foo in other thread:" x)
 (set! foo 25)))]
 (.start t)
 (.join t))

   (println "foo in main thread:" foo)
   (def foo 72) ; changing the root-bind (don't do this!)
   (println "foo in main thread after 2nd def:" foo))

(println "foo outside binding:" foo)

Output:
   x in other thread: 5

   Exception in thread "Thread-133" java.lang.RuntimeException: 
java.lang.IllegalStateException: Can't change/establish root binding of: 
foo with set

   x in main thread: 9
   x in main thread after 2nd def: 9
   foo outside binding: 72



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

2009-10-30 Thread Alex Osborne

Alex Osborne wrote:
> Chick Corea wrote:
>> If I understand correctly, the "(binding ...)" macro specifically
>> applies to
>> "Variables", one of Clojure's STM types, thus has dynamic, thead-local
>> scope.  Is that right?  In which case, it may hide a lexical binding ?
> 
> Yes, I think you are basically correct.  Maybe this example will help:

No it won't because I totally messed it up (copy-paste errors).  Sorry, 
I'm not completely awake yet.  What I meant was:

(def x 5) ; thread-global nearly immutable root binding

(binding [x 7] ; creates a thread-local binding for x
   (set! x 9) ; changes the thread-local binding

   (let [t (Thread. (fn []
 (println "x in other thread:" x)
 (set! x 25)))]
 (.start t)
 (.join t))

   (println "x in main thread:" x)
   (def x 72) ; change root-bindings (don't do this!)
   (println "x in main thread after 2nd def:" x))

(println "x outside binding:" x)

Output:

x in other thread: 5

Exception in thread "Thread-188" java.lang.RuntimeException: 
java.lang.IllegalStateException: Can't change/establish root binding of: 
x with set

x in main thread: 9
x in main thread after 2nd def: 9
x in outside binding: 72

--~--~-~--~~~---~--~~
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: Running out of memory when using loop/recur and destructuring

2009-10-30 Thread John Harrop
On Fri, Oct 30, 2009 at 3:15 PM, Paul Mooser  wrote:

> Is this behavior due to some artifact of destructuring I'm not aware
> of (or something else I'm missing), or is there a bug? If it sounds
> like a bug, can anyone else reproduce?
>
> Thanks!


I vaguely remember something like this coming up before, months ago. It may
have been a bug. That the behavior depends on the JVM and is nonbothersome
with a more recent JVM version suggests so.

--~--~-~--~~~---~--~~
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: can I make this faster (and leaner) ?

2009-10-30 Thread Alex Osborne

Chick Corea wrote:
> The corresponding Java code (w/ the hash-insert omitted in the clojure
> version)
> runs in 5.5sec and uses 290MB.
> 
> This code runs (which omits the hash-insert) runs in 17.8sec and uses
> 353MB.
> 
> I thought that I added all of the casts and "warn-on-reflections" that
> would help.
> Also, I thought using Java data structures directly would bypass some
> of Clojure's
> overhead.  What am I missing?

Not sure about the speed difference, but how are measuring memory usage? 
  Your memory figures might include a bunch of garbage that hasn't be 
GCed yetm, Clojure code tends to make lots more garbage than Java code 
due to the immutability.  You could try forcing a GC with (System/gc) 
and see if it changes your readings.  If it's generating exactly the 
same structure as the java code, Clojure should be using the same amount 
of memory (aside from the initial overhead).

--~--~-~--~~~---~--~~
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: Seq wrappers for arrays can morph

2009-10-30 Thread Josh Daghlian

During the Boston Lisp Users meeting last November (?) I asked Rich
about whether seq's on mutable java.util.Collections were really
immutable if the underlying object (the Collection) was mutable. He
emphatically said that no, the seq is still immutable, and that it
caches values as it sees them. I just checked, and for ArrayList it
still works like Rich said:

user> x
#
user> (def y (seq x))
#'user/
y
user> (first y)
"foo"
user> (.add x 0 "zzz")
nil
user> x
#
user> (first y)
"foo"
user>

I am surprised that the same doesn't happen with java arrays. Sounds
like a bug, or at least a change request I'd second.

After possibly improperly quoting rhickey, Josh slinks away...
--josh

On Oct 30, 12:40 pm, John Harrop  wrote:
> user=> (def x (int-array 3))
> #'user/x
> user=> x
> [0, 0, 0]
> user=> (def y (seq x))
> #'user/y
> user=> (first y)
> 0
> user=> (aset x 1 3)
> 3
> user=> x
> [0, 3, 0]
> user=> (second y)
> 3
> user=> (aset x 0 2)
> 2
> user=> x
> [2, 3, 0]
> user=> (first y)
> 2
>
> Here, (first y) returned first 0, then 2 without y being rebound in between.
> This is a bit disturbing; some seqs can't be counted on to remain constant.
> I'd expected (first y) once produced as a 0 to be carved in stone, while
> (second y) would be 3 because it would be lazily produced from the array
> only after the corresponding array cell was changed. For this, the seq
> implementation would be something analogous to
>
> (defn lazy-array-seq
>   ([arr]
>     (lazy-array-seq arr 0))
>   ([arr from-idx]
>     (lazy-array-seq arr 0 (count arr)))
>   ([arr from-idx end-idx]
>     (if-not (= from-idx end-idx)
>       (lazy-seq (aget arr from-idx) (lazy-array-seq arr (inc from-idx)
> end-idx)
>
> but apparently it's actually just a very thin wrapper around the array,
> something like a "Java Iterator with copy-on-next semantics".
>
> Perhaps it should be changed to behave something more like the
> lazy-array-seq function above?
>
> Probably the seq wrappers for java.util collections exhibit the same ... I
> hesitate to call it a "bug", but I'm not sure it's the best choice of
> semantics. My feeling is that once realized seq values should never change,
> however the seq was produced.
--~--~-~--~~~---~--~~
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: Seq wrappers for arrays can morph

2009-10-30 Thread Josh Daghlian

Although I suppose this isn't too surprising:

user> (second y)
--> ConcurrentModificationException

--josh

On Oct 30, 9:31 pm, Josh Daghlian  wrote:
> During the Boston Lisp Users meeting last November (?) I asked Rich
> about whether seq's on mutable java.util.Collections were really
> immutable if the underlying object (the Collection) was mutable. He
> emphatically said that no, the seq is still immutable, and that it
> caches values as it sees them. I just checked, and for ArrayList it
> still works like Rich said:
>
> user> x
> #
> user> (def y (seq x))
> #'user/
> y
> user> (first y)
> "foo"
> user> (.add x 0 "zzz")
> nil
> user> x
> #
> user> (first y)
> "foo"
> user>
>
> I am surprised that the same doesn't happen with java arrays. Sounds
> like a bug, or at least a change request I'd second.
>
> After possibly improperly quoting rhickey, Josh slinks away...
> --josh
>
> On Oct 30, 12:40 pm, John Harrop  wrote:
>
>
>
> > user=> (def x (int-array 3))
> > #'user/x
> > user=> x
> > [0, 0, 0]
> > user=> (def y (seq x))
> > #'user/y
> > user=> (first y)
> > 0
> > user=> (aset x 1 3)
> > 3
> > user=> x
> > [0, 3, 0]
> > user=> (second y)
> > 3
> > user=> (aset x 0 2)
> > 2
> > user=> x
> > [2, 3, 0]
> > user=> (first y)
> > 2
>
> > Here, (first y) returned first 0, then 2 without y being rebound in between.
> > This is a bit disturbing; some seqs can't be counted on to remain constant.
> > I'd expected (first y) once produced as a 0 to be carved in stone, while
> > (second y) would be 3 because it would be lazily produced from the array
> > only after the corresponding array cell was changed. For this, the seq
> > implementation would be something analogous to
>
> > (defn lazy-array-seq
> >   ([arr]
> >     (lazy-array-seq arr 0))
> >   ([arr from-idx]
> >     (lazy-array-seq arr 0 (count arr)))
> >   ([arr from-idx end-idx]
> >     (if-not (= from-idx end-idx)
> >       (lazy-seq (aget arr from-idx) (lazy-array-seq arr (inc from-idx)
> > end-idx)
>
> > but apparently it's actually just a very thin wrapper around the array,
> > something like a "Java Iterator with copy-on-next semantics".
>
> > Perhaps it should be changed to behave something more like the
> > lazy-array-seq function above?
>
> > Probably the seq wrappers for java.util collections exhibit the same ... I
> > hesitate to call it a "bug", but I'm not sure it's the best choice of
> > semantics. My feeling is that once realized seq values should never change,
> > however the seq was produced.
--~--~-~--~~~---~--~~
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: Seq wrappers for arrays can morph

2009-10-30 Thread John Harrop
On Fri, Oct 30, 2009 at 9:31 PM, Josh Daghlian  wrote:

> During the Boston Lisp Users meeting last November (?) I asked Rich
> about whether seq's on mutable java.util.Collections were really
> immutable if the underlying object (the Collection) was mutable. He
> emphatically said that no, the seq is still immutable, and that it
> caches values as it sees them. I just checked, and for ArrayList it
> still works like Rich said:
>
> user> x
> # >
> user> (def y (seq x))
> #'user/
> y
> user> (first y)
> "foo"
> user> (.add x 0 "zzz")
> nil
> user> x
> # >
> user> (first y)
> "foo"
> user>
>
> I am surprised that the same doesn't happen with java arrays. Sounds
> like a bug, or at least a change request I'd second.
>

Interesting.

I thought maybe inserting at the start might not affect the seq even if the
maybe-bug did affect ArrayList, because it might just stay aligned with the
existing elements. But no:

user=> (def x (ArrayList. ["foo" "bar" "baz" "quux"]))
#'user/x
user=> x
#
user=> (def y (seq x))
#'user/y
user=> (first y)
"foo"
user=> (.set x 0 "boo")
"foo"
user=> (first y)
"foo"
user=> (first x)
"boo"

So, (first y) doesn't morph even if you mutate the first cell of the
ArrayList in place.

--~--~-~--~~~---~--~~
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: Seq wrappers for arrays can morph

2009-10-30 Thread John Harrop
On Fri, Oct 30, 2009 at 9:36 PM, Josh Daghlian  wrote:

>
> Although I suppose this isn't too surprising:
>
> user> (second y)
> --> ConcurrentModificationException


Eeeuw. Guess it uses an Iterator to generate the elements for the lazy seq.
For ArrayList, walking it by index would avoid this. For LinkedList it would
make it O(n) as well though.

Mutable collections really don't play nice with Clojure seqs.

--~--~-~--~~~---~--~~
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: can I make this faster (and leaner) ?

2009-10-30 Thread DavidF

Try this:

(def *valid-chars* [ \a \b \c \d \e \f \g \h \i \j \k \l \m
 \n \o \p \q \r \s \t \u \v \w \x \u \z
 \0 \1 \2 \3 \4 \5 \6 \7 \8 \9 ] )

(defn generate-key [keylength]
(for [x (range keylength)] (nth *valid-chars* (rand-int (count *valid-
chars*)

(defn generate-random-keys [numkeys keylength]
(for [x (range numkeys)] (generate-key keylength)))

(time (into [] (generate-random-keys 10 1024)))  ;; 100k strings,
1024-chars each

I get "Elapsed time: 384.562 msecs".  (MacPro/2.66Ghz)

I didn't check the memory usage and it may not be the most idiomatic
clojure but it's a bit easier to read and understand.

On Oct 30, 3:56 pm, Chick Corea  wrote:
> How do I make this code faster?  And leaner, i.e., use less memory ?
>
> It's a simple function to generate NUMKEYS strings of length
> KEYLENGTH,
> store them in a Java array then store them in a HashMap
> hash-map.  [it doesn't store them in the hash-map yet; but it
> allocates it]
>
> (set! *warn-on-reflection* true)
>
> (def *valid-chars* [ \a \b \c \d \e \f \g \h \i \j \k \l \m
>                      \n \o \p \q \r \s \t \u \v \w \x \u \z
>                      \0 \1 \2 \3 \4 \5 \6 \7 \8 \9 ] )
>
> (defn generate-random-keys [ numkeys keylength ]
>   (time
>    (let [ rand (new java.util.Random)
>          key (make-array Character/TYPE keylength) ;;  :initial-
> element \a))
>          keys (make-array String numkeys)
>          data (new java.util.HashMap (int numkeys))
>          ]
>      (.println System/out (format "generating [%d] keys of length
> [%d]" numkeys keylength ))
>      (dotimes [i numkeys]
>        (dotimes [j keylength]
>          (aset key j (nth *valid-chars* (. rand nextInt (int 36)
>        (let [tmp (String. #^chars key) ]
>          ;;       (.println System/out (format "keys[%d] => [%s]" i
> tmp))
>          (aset keys i tmp)))
>      (.println System/out (format "inserting [%d] keys into HashMap"
> numkeys)
>
> (generate-random-keys 10 1024)  ;; 100k strings, 1024-chars each
>
> The corresponding Java code (w/ the hash-insert omitted in the clojure
> version)
> runs in 5.5sec and uses 290MB.
>
> This code runs (which omits the hash-insert) runs in 17.8sec and uses
> 353MB.
>
> I thought that I added all of the casts and "warn-on-reflections" that
> would help.
> Also, I thought using Java data structures directly would bypass some
> of Clojure's
> overhead.  What am I missing?
>
> Granted, the initial memory allocated is
>
>         * for POJ, ~12MB
>         * for Clojure, ~90MB
>
> That may or may  not be relevant.  It's hard to know w/ auto mem-mgmt.
>
> CHICKEE

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



Re: idiom questions

2009-10-30 Thread Adrian Cuthbertson

>  (let [x nil]
> ;; do something and modify 'x'
>   )
>
>how does one modify the value of 'x' ?

Hi Chick, there's nothing stopping you re-binding x within the let
construct, eg;

(defn myfn [x]
  (let [x (if (or (nil? x) (< x 0.2)) 0.0 x)
x (if  (>= x 0.8) 1.0 x)]
(do-something-with x)))

That technique helps a lot of the times you think you need mutable
variables. Nested lets are also useful.

-Rgds, Adrian.

--~--~-~--~~~---~--~~
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: bugs in cl-format.clj

2009-10-30 Thread Tom Faulhaber

Hey Carlos,

Thanks for noticing these. I'll take a look.

I think cl-format has the most tests of anything in Clojure and it
still has lousy test coverage of all the cases it's supposed to cover.

Tom

On Oct 28, 4:53 pm, carlitos  wrote:
> Hello,
>
> I've encountered a couple of issues with the cl-format function
> included in contrib.pprint
>
> (cl-format nil "~1,1$" -12.0) ;; => "12.0"  the sign is lost
>
> I think the problem is the following assignment in the dollar-float
> function
>         add-sign (and (:at params) (not (neg? arg)))     ;; wrong
> (the sign is only printed when the colon modifier is present and only
> for positive numbers)
> that should read, if I understand correctly the logic,
>         add-sign (or (:at params) (neg? arg))
>
> The second issue is not so straightforward to solve:
>
> (cl-format true "~1,1$~%" 0.001) ;; => String index out of range: -1
>
> I've tracked down the bug into the function round-str (the variable
> round-pos will be negative and this case is not handled properly), but
> I don't understand the code well enough to propose a fix.
>
> Cheers,
>
> Carlos
--~--~-~--~~~---~--~~
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: bugs in cl-format.clj

2009-10-30 Thread Tom Faulhaber

I've added assembla ticket #40 for these issues:
https://www.assembla.com/spaces/clojure-contrib/tickets/40-bugs-in-cl-format
--~--~-~--~~~---~--~~
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: bugs in cl-format.clj

2009-10-30 Thread Richard Newman

> I think cl-format has the most tests of anything in Clojure and it
> still has lousy test coverage of all the cases it's supposed to cover.

Don't be too hard on yourself... FORMAT and LOOP are amongst the  
scarier corners of Common Lisp, and even battle-hardened CL  
implementations sometimes disagree!

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