Re: Multiple indexing for vectors/maps

2008-10-19 Thread Meikel Brandmeyer

Hello,

Am 19.10.2008 um 00:12 schrieb kwatford:


I don't think adding this should conflict with any existing code,
though I did notice that "get" currently accepts and apparently
ignores one extra parameter.

No. It does not ignore the extra parameter! This is a default value,
which is returned in case the index is not contained in the vector.

user=> (def x [:a :b])
#=(var user/x)
user=> (get x 0 :foo)
:a
user=> (get x 1 :foo)
:b
user=> (get x 2 :foo)
:foo

See also the help of get:

user=> (doc get)
-
clojure/get
([map key] [map key not-found])
  Returns the value mapped to key, not-found or nil if key not present.

This works also for maps and sets, not only vectors.

Sincerely
Meikel



smime.p7s
Description: S/MIME cryptographic signature


Re: offtopic - where are you come from? (poll)

2008-10-19 Thread hoeck

Dresden, Germany

On Oct 17, 11:27 am, "Rastislav Kassak" <[EMAIL PROTECTED]> wrote:
> Hello Clojurians,
>
> I think after 1st year of Clojure life it's good to check how far has
> Clojure spread all over the world.
>
> So wherever are you come from, be proud and say it.
>
> I'm from Slovakia. :)
>
> RK
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



CTPOP emulation in JVM

2008-10-19 Thread Krukow

I was reading though Phil Bagwells paper "Ideal Hash Trees", when I
encountered this paragraph:

Note that the performance of the algorithm is seriously impacted
by the poor execution speed of the CTPOP emulation in Java, a problem
the Java
designers may wish to address.


I am assuming he means the JVM and not Java. How does this affect
Clojure? do you know if it has been fixed? Does it mean that there is
a potential performance boost for Clojure  waiting in the future?


/krukow
--~--~-~--~~~---~--~~
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: CTPOP emulation in JVM

2008-10-19 Thread Rich Hickey



On Oct 19, 5:27 am, Krukow <[EMAIL PROTECTED]> wrote:
> I was reading though Phil Bagwells paper "Ideal Hash Trees", when I
> encountered this paragraph:
>
> Note that the performance of the algorithm is seriously impacted
> by the poor execution speed of the CTPOP emulation in Java, a problem
> the Java
> designers may wish to address.
>
> I am assuming he means the JVM and not Java. How does this affect
> Clojure? do you know if it has been fixed? Does it mean that there is
> a potential performance boost for Clojure  waiting in the future?
>

A lot has happened with JVM performance since the paper was written.
While not as fast as mapping to a dedicated instruction (often not
available), the manual code is plenty fast now.

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



Lazy step ...

2008-10-19 Thread Achim Passen

... is not some new form of dance music from the UK, but what appears  
to be a recurring - dare i say - pattern in the definition of lazy  
sequences: the combination of lazy-consing and a step function. There  
are plenty of these in boot.clj and in contrib's lazy-seq.clj.

Here's an example (poor Fibonacci again): 
http://en.wikibooks.org/wiki/Clojure_Programming#Lazy_Fibonacci
Why is any of the first three definitions preferable to a  
straightforward one like that?:

(def fibs (lazy-cat [0 1] (map + fibs (rest fibs

The last example i do understand: you can't define lexically scoped  
recursive lazy seqs like this (short of letrec), which you need if a  
function is supposed to construct a lazy seq internally. But i'm  
afraid there might be further subtleties i fail to see.

Can somebody explain this to a lazy-eval-newbie?

Thanks a lot!

Kind regards,
achim

-- 
http://rauschabstand.twoday.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
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
-~--~~~~--~~--~--~---



Another newbie app example

2008-10-19 Thread Asbjørn Bjørnstad

Hi,
I wrote a very simple swing app and wrote a little about it. Mostly
aimed at newbies with some lisp knowledge. (map/let is not explained
for example)

http://blog.jalat.com/2008/10/game-clojure-version.html

Even such a small program ended up using quite a bit of clojure stuff:
lazy sequences, proxy classes/java interop, doto, references etc.
--
 -asbjxrn
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Fibonacci function performance compare between clojure and scala

2008-10-19 Thread [EMAIL PROTECTED]

Clojure's

(defn fib [n]
   (if  (or (zero? n) (= n 1))
   1
  (+  (fib (dec n) )  (fib (- n 2)

(time (fib 36))

"Elapsed Time:  10475.325226 msecs"
24157817

Scala's

def fib(n:Int):Int=if (n==0||n==1)1 else fib(n-1)+fib(n-2)
def time(cal: =>Int)={
 val beginTime=System.currentTimeMillis
 cal
 val endTime=System.currentTimeMillis
 println(endTime-beginTime)
}
fib(36)
res70:Int=24157817
time(fib(36))
263

Both not tail recursive,both running on Repl (scala's interpreter),but
the difference between two is huge
10475~263
My box : Intel core2 cpu 1.86G,2G mem
Clojure and scala both latest build from svn

any ideas?

--~--~-~--~~~---~--~~
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: Fibonacci function performance compare between clojure and scala

2008-10-19 Thread Rastislav Kassak

Just use type hint in Clojure version and you'll see quite a
difference in performance.

Your scala version is completely optimized / crippled to integers
(maybe even unboxed), so there is no dynamic runtime overhead.

IMHO, this kind of microbenchmarks are just good for finding general
weak points of particular language / implementation and have nothing
in common with real world performance.
As you said in this case - it's not tail recursive at least.

On 10/19/08, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>  Clojure's
>
>  (defn fib [n]
>(if  (or (zero? n) (= n 1))
>1
>   (+  (fib (dec n) )  (fib (- n 2)
>
>  (time (fib 36))
>
>  "Elapsed Time:  10475.325226 msecs"
>  24157817
>
>  Scala's
>
>  def fib(n:Int):Int=if (n==0||n==1)1 else fib(n-1)+fib(n-2)
>  def time(cal: =>Int)={
>   val beginTime=System.currentTimeMillis
>   cal
>   val endTime=System.currentTimeMillis
>   println(endTime-beginTime)
>  }
>  fib(36)
>  res70:Int=24157817
>  time(fib(36))
>  263
>
>  Both not tail recursive,both running on Repl (scala's interpreter),but
>  the difference between two is huge
>  10475~263
>  My box : Intel core2 cpu 1.86G,2G mem
>  Clojure and scala both latest build from svn
>
>  any ideas?
>
>  >
>

--~--~-~--~~~---~--~~
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: Fibonacci function performance compare between clojure and scala

2008-10-19 Thread Parth Malwankar



On Oct 19, 7:49 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> Clojure's
>
> (defn fib [n]
>(if  (or (zero? n) (= n 1))
>1
>   (+  (fib (dec n) )  (fib (- n 2)
>
> (time (fib 36))
>
> "Elapsed Time:  10475.325226 msecs"
> 24157817
>
> Scala's
>
> def fib(n:Int):Int=if (n==0||n==1)1 else fib(n-1)+fib(n-2)
> def time(cal: =>Int)={
>  val beginTime=System.currentTimeMillis
>  cal
>  val endTime=System.currentTimeMillis
>  println(endTime-beginTime)}
>
> fib(36)
> res70:Int=24157817
> time(fib(36))
> 263
>

I am not a scala expert but I suspect scala Int maps directly to
java ints.

In clojure, the number are boxed and checked by default IIRC.
This would roughly correspond to BigInt in scala.

For clojure, you could coerce ints to native java types using
(int ..).
Also, unchecked-dec could be used.
Doing this should make the code as fast as java or scala.
There was some discussion along these lines here:
http://groups.google.com/group/clojure/browse_thread/thread/4274ce8bd44664ef#

That said, for most practical uses the default behavior
should be fast enough. Its not considered idiomatic to
use coersion and unchecked operation unless absolutely
necessary.

Parth


> Both not tail recursive,both running on Repl (scala's interpreter),but
> the difference between two is huge
> 10475~263
> My box : Intel core2 cpu 1.86G,2G mem
> Clojure and scala both latest build from svn
>
> any ideas?
--~--~-~--~~~---~--~~
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: Lazy step ...

2008-10-19 Thread Stephen C. Gilardi

On Oct 19, 2008, at 8:49 AM, Achim Passen wrote:

>   (def fibs (lazy-cat [0 1] (map + fibs (rest fibs

That looks strictly better than the other examples to me. If you're  
willing and if you send a Contributer Agreement to Rich, I'd like to  
adopt it in clojure.contrib.lazy-seqs. In the meantime, the wiki is  
editable, I encourage you to put yours in it.

I'm trying to come up with a similarly simple powers-of-2... Ah,  
there's one:

(def powers-of-2 (lazy-cons 1 (map #(bit-shift-left % 1) powers-of-2)))

or the equivalent

(def powers-of-2 (lazy-cons 1 (map #(+ % %) powers-of-2)))

Very cool, thanks for pointing it out!

--Steve


--~--~-~--~~~---~--~~
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: Fibonacci function performance compare between clojure and scala

2008-10-19 Thread [EMAIL PROTECTED]

Here is coersion version for Clojure

(defn fib [n]
  (let [n  (int n)]
   (if  (or (zero? n) (= n 1))
   1
  (+  (fib (dec n) )  (fib (- n 2))

(time (fib 36))
"Elapsed time 8848.865149"

not much better   and how to type hint for a int  type?





--~--~-~--~~~---~--~~
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: Fibonacci function performance compare between clojure and scala

2008-10-19 Thread [EMAIL PROTECTED]

Scala is sure to use java primitive int type underline, i.e value
type  and boxed to java Integer when necessarily

But  why not  Clojure auto make this ?




gerry


On Oct 19, 11:31 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> Here is coersion version for Clojure
>
> (defn fib [n]
>   (let [n  (int n)]
>    (if  (or (zero? n) (= n 1))
>        1
>       (+  (fib (dec n) )  (fib (- n 2))
>
> (time (fib 36))
> "Elapsed time 8848.865149"
>
> not much better   and how to type hint for a int  type?
--~--~-~--~~~---~--~~
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: Lazy step ...

2008-10-19 Thread Achim Passen

Hi Steve,

thanks for your answer!

Am 19.10.2008 um 17:13 schrieb Stephen C. Gilardi:

> That looks strictly better than the other examples to me. If you're  
> willing and if you send a Contributer Agreement to Rich, I'd like to  
> adopt it in clojure.contrib.lazy-seqs. In the meantime, the wiki is  
> editable, I encourage you to put yours in it.
>
> I'm trying to come up with a similarly simple powers-of-2... Ah,  
> there's one:
>
>   (def powers-of-2 (lazy-cons 1 (map #(bit-shift-left % 1) powers- 
> of-2)))
>
> or the equivalent
>
>   (def powers-of-2 (lazy-cons 1 (map #(+ % %) powers-of-2)))
>
> Very cool, thanks for pointing it out!

To be honest, i can't take much credit for it, about every Haskell  
tutorial starts with that definition. ;)
But it's often stated that Clojure's model of lazy evaluation differs  
from Haskell, so i thought there was a reason for using step functions.

If you'd really like to include it, please do (i already sent the CA).  
But - if i understand this correctly - there is this problem with root- 
bound lazy seqs: they won't ever get garbage collected. This might  
oder might not be what you want. Example 4 in the wiki adresses this  
problem, thus might be a better fit for a library, because the  
decision whether to root-bind oder lexically restrict the sequence is  
up to the user.

Kind regards,
achim


-- 
http://rauschabstand.twoday.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
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: Fibonacci function performance compare between clojure and scala

2008-10-19 Thread Lauri Oherd

There is also a faster way to calculate fibonacci numbers in Clojure
(code taken from from
http://en.wikibooks.org/wiki/Clojure_Programming#Lazy_Fibonacci):

(defn fib-seq []
  ((fn rfib [a b]
   (lazy-cons a (rfib b (+ a b
0 1))

user=> (time (take 38 (fib-seq)))
"Elapsed time: 0.032965 msecs"
(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
10946 17711 28657 46368 75025 121393 196418 317811 514229 832040
1346269 2178309 3524578 5702887 9227465 14930352 24157817)

Lauri


2008/10/19 [EMAIL PROTECTED] <[EMAIL PROTECTED]>:
>
> Clojure's
>
> (defn fib [n]
>   (if  (or (zero? n) (= n 1))
>   1
>  (+  (fib (dec n) )  (fib (- n 2)
>
> (time (fib 36))
>
> "Elapsed Time:  10475.325226 msecs"
> 24157817
>
> Scala's
>
> def fib(n:Int):Int=if (n==0||n==1)1 else fib(n-1)+fib(n-2)
> def time(cal: =>Int)={
>  val beginTime=System.currentTimeMillis
>  cal
>  val endTime=System.currentTimeMillis
>  println(endTime-beginTime)
> }
> fib(36)
> res70:Int=24157817
> time(fib(36))
> 263
>
> Both not tail recursive,both running on Repl (scala's interpreter),but
> the difference between two is huge
> 10475~263
> My box : Intel core2 cpu 1.86G,2G mem
> Clojure and scala both latest build from svn
>
> any ideas?
>
> >
>

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



Submitting patches

2008-10-19 Thread Matt Revelle

Rich,

I submitted a patch that adds support for exposing protected fields
inherited indirectly through the super class in gen-class.  No problem
if it's unwanted, but would be good to know either way.  Suppose the
original message should've had "patch" in the subject line; it's
"exposing ancestral protected fields with gen-class."

Take care,
Matt
--~--~-~--~~~---~--~~
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: Fibonacci function performance compare between clojure and scala

2008-10-19 Thread [EMAIL PROTECTED]

This lazy cached calculate is wonderful ,but i think the benefit from
it  mostly due to cache .





On Oct 19, 11:56 pm, "Lauri Oherd" <[EMAIL PROTECTED]> wrote:
> There is also a faster way to calculate fibonacci numbers in Clojure
> (code taken from 
> fromhttp://en.wikibooks.org/wiki/Clojure_Programming#Lazy_Fibonacci):
>
> (defn fib-seq []
>   ((fn rfib [a b]
>(lazy-cons a (rfib b (+ a b
> 0 1))
>
> user=> (time (take 38 (fib-seq)))
> "Elapsed time: 0.032965 msecs"
> (0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
> 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040
> 1346269 2178309 3524578 5702887 9227465 14930352 24157817)
>
> Lauri
>
> 2008/10/19 [EMAIL PROTECTED] <[EMAIL PROTECTED]>:
>
>
>
> > Clojure's
>
> > (defn fib [n]
> >   (if  (or (zero? n) (= n 1))
> >   1
> >  (+  (fib (dec n) )  (fib (- n 2)
>
> > (time (fib 36))
>
> > "Elapsed Time:  10475.325226 msecs"
> > 24157817
>
> > Scala's
>
> > def fib(n:Int):Int=if (n==0||n==1)1 else fib(n-1)+fib(n-2)
> > def time(cal: =>Int)={
> >  val beginTime=System.currentTimeMillis
> >  cal
> >  val endTime=System.currentTimeMillis
> >  println(endTime-beginTime)
> > }
> > fib(36)
> > res70:Int=24157817
> > time(fib(36))
> > 263
>
> > Both not tail recursive,both running on Repl (scala's interpreter),but
> > the difference between two is huge
> > 10475~263
> > My box : Intel core2 cpu 1.86G,2G mem
> > Clojure and scala both latest build from svn
>
> > any ideas?
--~--~-~--~~~---~--~~
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: offtopic - where are you come from? (poll)

2008-10-19 Thread J . Pablo Fernández

I think it might be more important where people are, where they live,
than where they come from. I live in Zürich, Switzerland.

On Oct 17, 11:27 am, "Rastislav Kassak" <[EMAIL PROTECTED]> wrote:
> Hello Clojurians,
>
> I think after 1st year of Clojure life it's good to check how far has
> Clojure spread all over the world.
>
> So wherever are you come from, be proud and say it.
>
> I'm from Slovakia. :)
>
> RK

--~--~-~--~~~---~--~~
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: offtopic - where are you come from? (poll)

2008-10-19 Thread Johan Berntsson

Tokyo, Japan

On Oct 17, 2:27 am, "Rastislav Kassak" <[EMAIL PROTECTED]> wrote:
> Hello Clojurians,
>
> I think after 1st year of Clojure life it's good to check how far has
> Clojure spread all over the world.
>
> So wherever are you come from, be proud and say it.
>
> I'm from Slovakia. :)
>
> RK

--~--~-~--~~~---~--~~
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: Submitting patches

2008-10-19 Thread Rich Hickey

Thanks for the patch! Hang tight, during the work on AOT compilation  
I've been revisiting whether or not I can bundle the functionality of  
genclass right into AOT compilation - I think I can, yielding a much  
streamlined process.

At that point, I'll have some syntax for adorning defns with metadata  
that will do what was once the work of genclass, and I'll be able to  
incorporate in that the enhancements to genclass that have been  
requested.

I hope to have some drops of AOT soon after I return from Lisp50 at  
OOPSLA. Until then, I'd encourage everyone who has ideas about how the  
metadata annotations might look to chime in.

Rich

On Oct 19, 2008, at 12:19 PM, Matt Revelle wrote:

>
> Rich,
>
> I submitted a patch that adds support for exposing protected fields
> inherited indirectly through the super class in gen-class.  No problem
> if it's unwanted, but would be good to know either way.  Suppose the
> original message should've had "patch" in the subject line; it's
> "exposing ancestral protected fields with gen-class."
>
> Take care,
> Matt
> >


--~--~-~--~~~---~--~~
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: Fibonacci function performance compare between clojure and scala

2008-10-19 Thread Meikel Brandmeyer

Hello,

Am 19.10.2008 um 17:56 schrieb Lauri Oherd:


There is also a faster way to calculate fibonacci numbers in Clojure
(code taken from from
http://en.wikibooks.org/wiki/Clojure_Programming#Lazy_Fibonacci):

(defn fib-seq []
((fn rfib [a b]
  (lazy-cons a (rfib b (+ a b
   0 1))

user=> (time (take 38 (fib-seq)))
"Elapsed time: 0.032965 msecs"
(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
10946 17711 28657 46368 75025 121393 196418 317811 514229 832040
1346269 2178309 3524578 5702887 9227465 14930352 24157817)


This timing is wrong. Since take returns a lazy sequence
you only get the timing for the call to take. The sequence
is forced later on when the Repl prints it.

The correct way is to force the evaluation with doall.

  user=> (time (take 38 (fib-seq)))
  "Elapsed time: 0.055 msecs"
  ...
  user=> (time (doall (take 38 (fib-seq
  "Elapsed time: 0.3 msecs"
  ...

Sincerely
Meikel



smime.p7s
Description: S/MIME cryptographic signature


Bug? Strange set equality (r1075)

2008-10-19 Thread Achim Passen

Hi,

user> (= #{1 4} #{2 3})
true

it's not, is it?

Kind regards,
achim




-- 
http://rauschabstand.twoday.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
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: Bug? Strange set equality (r1075)

2008-10-19 Thread Chouser
On Sun, Oct 19, 2008 at 3:02 PM, Achim Passen <[EMAIL PROTECTED]> wrote:
>
>user> (= #{1 4} #{2 3})
>true
>
> it's not, is it?

I hope not!

Patch attached.

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

diff --git a/src/jvm/clojure/lang/APersistentSet.java b/src/jvm/clojure/lang/APersistentSet.java
index d8345f3..241d091 100644
--- a/src/jvm/clojure/lang/APersistentSet.java
+++ b/src/jvm/clojure/lang/APersistentSet.java
@@ -59,7 +59,7 @@ public boolean equals(Object obj){
 
 	for(Object aM : m)
 		{
-		if(!m.contains(aM))
+		if(!contains(aM))
 			return false;
 		}
 //	for(ISeq s = seq(); s != null; s = s.rest())


Re: Bug? Strange set equality (r1075)

2008-10-19 Thread Rich Hickey



On Oct 19, 3:02 pm, Achim Passen <[EMAIL PROTECTED]> wrote:
> Hi,
>
> user> (= #{1 4} #{2 3})

Fixed - thanks for the report.

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: Bug? Strange set equality (r1075)

2008-10-19 Thread Stephen C. Gilardi

On Oct 19, 2008, at 3:02 PM, Achim Passen wrote:

> Hi,
>
>   user> (= #{1 4} #{2 3})
>   true
>
> it's not, is it?

It's not. Line 62 of APersistentSet.java should be:

if (!contains(aM))

rather than

if (!m.contains(aM))

As a more general thought, we Clojure users would benefit from a test  
suite for the functions (and macros) shipped with the Clojure  
distribution. This seems like an ideal thing for members of the  
community to work together on--a large effort that can be done in  
parallel. Any work we do on it is to the good and it can grow over  
time to be more complete.

Is there already a wiki page for it? Anybody want to sign up for  
writing tests for a range of lines in boot.clj? Stuart's  
clojure.contrib.test-is lib should provide a good framework to get  
started with.

--Steve


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



Suggestions on handling state for a game...

2008-10-19 Thread Adam Jones

I'm starting up work again on my Clojure-based game. Up until this
point I've been doing things the "dirty" way by re-defing a bunch of
global variables and/or storing the values in the Java objects I need
to use to get access to the game framework.

This approach is pretty unwieldy, and means I miss out on a lot of the
cool things Clojure has to offer. So, I need to figure out how to
handle state from within Clojure. The two ideas that I think will work
are:

1. Store the game world as a var, and have the game structured as a
function from the game world to the game world. Since I need to pass
control back to Java to handle a lot of the details this would require
me to store the game world in Java between updates.

2. Store the game world as a ref and use the facilities of that to
modify the game state on updates. This seems cleaner in that it can
all be handled without resorting to Java for intermediary storage. I
am concerned that using the STM system would add too much performance
overhead.

I guess what this all comes down to is, between vars and refs, which
one is the better choice for a program that I don't think will really
need multithreading?
--~--~-~--~~~---~--~~
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: Bug? Strange set equality (r1075)

2008-10-19 Thread J. McConnell

> As a more general thought, we Clojure users would benefit from a test suite
> for the functions (and macros) shipped with the Clojure distribution. This
> seems like an ideal thing for members of the community to work together
> on--a large effort that can be done in parallel. Any work we do on it is to
> the good and it can grow over time to be more complete.
>
> Is there already a wiki page for it? Anybody want to sign up for writing
> tests for a range of lines in boot.clj? Stuart's clojure.contrib.test-is lib
> should provide a good framework to get started with.

I've been thinking the same thing for awhile now and I'd love to help
contribute to an effort like this. Thanks for getting the idea out
there.

- J.

--~--~-~--~~~---~--~~
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: Beginners (and idiomatic Clojure)

2008-10-19 Thread Paul Barry

Krukow,

I agree, it would help to have a resource for learning Clojure.  For
now, my best advice is to pick a real project to start working and
then specific questions in the IRC room, #clojure on irc.freenode.net.

Within a few months, we'll have the beta book:
http://www.pragprog.com/titles/shcloj/programming-clojure

On Oct 18, 9:56 am, Krukow <[EMAIL PROTECTED]> wrote:
> This post is intended to start a discussion about how to help
> beginners (like myself) going in the right direction learning *to
> actually use* Clojure in real-world programs.
>
> The presentation videos on the net are excellent -- they are what
> convinced my that Clojure is really worth learning, and learning well,
> too. The homepage is great, it really explains many concepts and
> important ideas in a concise yet readable manner.
>
> Excellent work: The 'theory' part of the documentation is great. (As
> opposed to so many other languages).
>
> However, to be really accessible to newcomers,  it would be great with
> more information on the 'practice'. E.g., a number medium-scale 'real'
> open-source example programs. Even better if the design rationale and
> architecture of such application were available as well...
>
> Also a wiki on idiomatic Clojure would be really valuable.
>
> I am very willing to contribute to all of these to the best of my
> ability; even if my contribution would be trying, failing and
> sharing ;-)
>
> Rich, any chance you could help out with the 'idiomatic clojure' part?
> Obviously, your experiences would be most valuable. Apart from Clojure
> itself, do you have some programs you would be willing to share?
>
> Cheers,
> /krukow
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Testing Clojure (was Re: Bug? Strange set equality (r1075))

2008-10-19 Thread Stephen C. Gilardi


On Oct 19, 2008, at 5:11 PM, J. McConnell wrote:

> I've been thinking the same thing for awhile now and I'd love to help
> contribute to an effort like this. Thanks for getting the idea out
> there.

You're welcome. It seems like clojure.contrib could be a more  
convenient place to keep this than the wiki.

Direct or indirect contributions to clojure.contrib require that the  
contributed code be written by the contributor and that the  
contributor have a contributor agreement on file with Rich. Would that  
be acceptable to people interested in participating? I appreciate the  
care Rich showed and long view he took in coming up with the  
Contributor Agreement process. I think it would be a good idea to  
leverage that process for this effort as well.

Discussion of alternative proposals for a good way to do this and  
place to keep it are welcome.

I made a start on this today. I started with the Reader page at  
clojure.org and started making tests. I'm thinking of a structure like  
this:

Run tests with:

(require 'clojure.contrib.test-clojure)

The definition of clojure.contrib.test-clojure requires subordinate  
test namespaces like

'clojure.contrib.test-clojure.Reader
'clojure.contrib.test-clojure.Evaluation
'clojure.contrib.test-clojure.Special-Forms
...

with names that correspond to pages on the Clojure web site. After  
requiring the individual test namespaces, test-clojure runs  
"clojure.contrib.test-is/run-tests" on each one.

Here's a sample from clojure.contrib.test-clojure.

(ns clojure.contrib.test-clojure.Reader
  (:use clojure.contrib.test-is))

(deftest t-Symbols
  (is (= 'abc (symbol "abc")))
  (is (= '*+!-_? (symbol "*+!-_?")))
  (is (= 'abc:def:ghi (symbol "abc:def:ghi")))
  (is (= 'abc/def (symbol "abc" "def")))
  (is (= 'abc.def/ghi (symbol "abc.def" "ghi")))
  (is (= 'abc/def.ghi (symbol "abc" "def.ghi")))
  (is (= 'abc:def/ghi:jkl.mno (symbol "abc:def" "ghi:jkl.mno")))
  (is (instance? clojure.lang.Symbol 'alphabet))
  )

; additional tests to flesh out
(deftest t-Numbers)
(deftest t-Characters)
(deftest t-nil)
(deftest t-Booleans)
(deftest t-Keywords)
(deftest t-Lists)
(deftest t-Vectors)
(deftest t-Maps)
(deftest t-Sets)
(deftest t-Quote)
(deftest t-Character)
(deftest t-Comment)
(deftest t-Meta)
(deftest t-Deref)
(deftest t-Regex)
(deftest t-Metadata)
(deftest t-read)

and a run:

user=> (require 'clojure.contrib.test-clojure)
Testing #

Ran 18 tests with 10 assertions.
0 failures, 0 exceptions.
nil
user=>

(Currently the number of tests exceeds the number of assertions by so  
much because of the placeholders.)

Tesing Clojure is a big project and will take a lot of work over time.  
There many pieces and many interactions among them to test. The hope  
is that having it available will allow Rich to make changes with an  
even higher degree of confidence that they didn't have unintended  
consequences and to support efforts like Chouser's ClojureScript to  
bring Clojure to new platforms

Discussion and suggestions are welcome.

--Steve


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



Swank-clojure: slime-eval-print-last-expression failing.

2008-10-19 Thread Luke Hope

Hello,

I am a common lisp programmer and I use Slime extensively. I am
looking into using Clojure for an upcoming project (I have experience
with ABCL, but it is too slow) and I have clojure and swank-clojure
installed.  Unfortunately slime-eval-print-last-expression (C-j in
*slime-scratch*) is failing (evaluation aborted), I think because it
uses asynchronous evaluation.  Is this a known issue or a regression?
Is it fixable?

I really want to be able to adapt my workflow for working with
clojure, and I use C-j all the time.

All code except for Emacs itself is current as of 2 hours ago.  The
behaviour was duplicated by a member of #clojure.

Hope you can help,

-Luke

--~--~-~--~~~---~--~~
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: Testing Clojure (was Re: Bug? Strange set equality (r1075))

2008-10-19 Thread Paul Barry

I like this idea and I would be willing to contribute.

On Oct 19, 6:43 pm, "Stephen C. Gilardi" <[EMAIL PROTECTED]> wrote:
> On Oct 19, 2008, at 5:11 PM, J. McConnell wrote:
>
> > I've been thinking the same thing for awhile now and I'd love to help
> > contribute to an effort like this. Thanks for getting the idea out
> > there.
>
> You're welcome. It seems like clojure.contrib could be a more  
> convenient place to keep this than the wiki.
>
> Direct or indirect contributions to clojure.contrib require that the  
> contributed code be written by the contributor and that the  
> contributor have a contributor agreement on file with Rich. Would that  
> be acceptable to people interested in participating? I appreciate the  
> care Rich showed and long view he took in coming up with the  
> Contributor Agreement process. I think it would be a good idea to  
> leverage that process for this effort as well.
>
> Discussion of alternative proposals for a good way to do this and  
> place to keep it are welcome.
>
> I made a start on this today. I started with the Reader page at  
> clojure.org and started making tests. I'm thinking of a structure like  
> this:
>
> Run tests with:
>
>         (require 'clojure.contrib.test-clojure)
>
> The definition of clojure.contrib.test-clojure requires subordinate  
> test namespaces like
>
>         'clojure.contrib.test-clojure.Reader
>         'clojure.contrib.test-clojure.Evaluation
>         'clojure.contrib.test-clojure.Special-Forms
>         ...
>
> with names that correspond to pages on the Clojure web site. After  
> requiring the individual test namespaces, test-clojure runs  
> "clojure.contrib.test-is/run-tests" on each one.
>
> Here's a sample from clojure.contrib.test-clojure.
>
>         (ns clojure.contrib.test-clojure.Reader
>           (:use clojure.contrib.test-is))
>
>         (deftest t-Symbols
>           (is (= 'abc (symbol "abc")))
>           (is (= '*+!-_? (symbol "*+!-_?")))
>           (is (= 'abc:def:ghi (symbol "abc:def:ghi")))
>           (is (= 'abc/def (symbol "abc" "def")))
>           (is (= 'abc.def/ghi (symbol "abc.def" "ghi")))
>           (is (= 'abc/def.ghi (symbol "abc" "def.ghi")))
>           (is (= 'abc:def/ghi:jkl.mno (symbol "abc:def" "ghi:jkl.mno")))
>           (is (instance? clojure.lang.Symbol 'alphabet))
>           )
>
>         ; additional tests to flesh out
>         (deftest t-Numbers)
>         (deftest t-Characters)
>         (deftest t-nil)
>         (deftest t-Booleans)
>         (deftest t-Keywords)
>         (deftest t-Lists)
>         (deftest t-Vectors)
>         (deftest t-Maps)
>         (deftest t-Sets)
>         (deftest t-Quote)
>         (deftest t-Character)
>         (deftest t-Comment)
>         (deftest t-Meta)
>         (deftest t-Deref)
>         (deftest t-Regex)
>         (deftest t-Metadata)
>         (deftest t-read)
>
> and a run:
>
>         user=> (require 'clojure.contrib.test-clojure)
>         Testing #
>
>         Ran 18 tests with 10 assertions.
>         0 failures, 0 exceptions.
>         nil
>         user=>
>
> (Currently the number of tests exceeds the number of assertions by so  
> much because of the placeholders.)
>
> Tesing Clojure is a big project and will take a lot of work over time.  
> There many pieces and many interactions among them to test. The hope  
> is that having it available will allow Rich to make changes with an  
> even higher degree of confidence that they didn't have unintended  
> consequences and to support efforts like Chouser's ClojureScript to  
> bring Clojure to new platforms
>
> Discussion and suggestions are welcome.
>
> --Steve
--~--~-~--~~~---~--~~
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: Testing Clojure (was Re: Bug? Strange set equality (r1075))

2008-10-19 Thread Michael Beauregard

Ditto. I've been thinking about this for a few weeks and would be
happy to help out where I can.

On Sun, Oct 19, 2008 at 6:22 PM, Paul Barry <[EMAIL PROTECTED]> wrote:
>
> I like this idea and I would be willing to contribute.
>
> On Oct 19, 6:43 pm, "Stephen C. Gilardi" <[EMAIL PROTECTED]> wrote:
>> On Oct 19, 2008, at 5:11 PM, J. McConnell wrote:
>>
>> > I've been thinking the same thing for awhile now and I'd love to help
>> > contribute to an effort like this. Thanks for getting the idea out
>> > there.
>>
>> You're welcome. It seems like clojure.contrib could be a more
>> convenient place to keep this than the wiki.
>>
>> Direct or indirect contributions to clojure.contrib require that the
>> contributed code be written by the contributor and that the
>> contributor have a contributor agreement on file with Rich. Would that
>> be acceptable to people interested in participating? I appreciate the
>> care Rich showed and long view he took in coming up with the
>> Contributor Agreement process. I think it would be a good idea to
>> leverage that process for this effort as well.
>>
>> Discussion of alternative proposals for a good way to do this and
>> place to keep it are welcome.
>>
>> I made a start on this today. I started with the Reader page at
>> clojure.org and started making tests. I'm thinking of a structure like
>> this:
>>
>> Run tests with:
>>
>> (require 'clojure.contrib.test-clojure)
>>
>> The definition of clojure.contrib.test-clojure requires subordinate
>> test namespaces like
>>
>> 'clojure.contrib.test-clojure.Reader
>> 'clojure.contrib.test-clojure.Evaluation
>> 'clojure.contrib.test-clojure.Special-Forms
>> ...
>>
>> with names that correspond to pages on the Clojure web site. After
>> requiring the individual test namespaces, test-clojure runs
>> "clojure.contrib.test-is/run-tests" on each one.
>>
>> Here's a sample from clojure.contrib.test-clojure.
>>
>> (ns clojure.contrib.test-clojure.Reader
>>   (:use clojure.contrib.test-is))
>>
>> (deftest t-Symbols
>>   (is (= 'abc (symbol "abc")))
>>   (is (= '*+!-_? (symbol "*+!-_?")))
>>   (is (= 'abc:def:ghi (symbol "abc:def:ghi")))
>>   (is (= 'abc/def (symbol "abc" "def")))
>>   (is (= 'abc.def/ghi (symbol "abc.def" "ghi")))
>>   (is (= 'abc/def.ghi (symbol "abc" "def.ghi")))
>>   (is (= 'abc:def/ghi:jkl.mno (symbol "abc:def" "ghi:jkl.mno")))
>>   (is (instance? clojure.lang.Symbol 'alphabet))
>>   )
>>
>> ; additional tests to flesh out
>> (deftest t-Numbers)
>> (deftest t-Characters)
>> (deftest t-nil)
>> (deftest t-Booleans)
>> (deftest t-Keywords)
>> (deftest t-Lists)
>> (deftest t-Vectors)
>> (deftest t-Maps)
>> (deftest t-Sets)
>> (deftest t-Quote)
>> (deftest t-Character)
>> (deftest t-Comment)
>> (deftest t-Meta)
>> (deftest t-Deref)
>> (deftest t-Regex)
>> (deftest t-Metadata)
>> (deftest t-read)
>>
>> and a run:
>>
>> user=> (require 'clojure.contrib.test-clojure)
>> Testing #
>>
>> Ran 18 tests with 10 assertions.
>> 0 failures, 0 exceptions.
>> nil
>> user=>
>>
>> (Currently the number of tests exceeds the number of assertions by so
>> much because of the placeholders.)
>>
>> Tesing Clojure is a big project and will take a lot of work over time.
>> There many pieces and many interactions among them to test. The hope
>> is that having it available will allow Rich to make changes with an
>> even higher degree of confidence that they didn't have unintended
>> consequences and to support efforts like Chouser's ClojureScript to
>> bring Clojure to new platforms
>>
>> Discussion and suggestions are welcome.
>>
>> --Steve
> >
>

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



Newbie question on the use of functional hash maps

2008-10-19 Thread Tom Emerson

Hi all,

I have a somewhat embarassing newbie question on the use of hash maps
in a functional environment.

Consider a little utility that counts the number of unique words in a
file. A hash map mapping strings to integers is the obvious data
structure for this, but the fact that (assoc) returns a new map each
time it is called is tripping me up: since I can't define a 'global'
hash map to accumulate the counts, do you pass one around in a
function? or do you structure the code a different way? This is a
difference from what I would do in Common Lisp, where I would just
have a global that is used for the collection.

Thanks in advance for your wisdom.

-tree

-- 
Tom Emerson
[EMAIL PROTECTED]
http://www.dreamersrealm.net/~tree

--~--~-~--~~~---~--~~
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: Suggestions on handling state for a game...

2008-10-19 Thread wwmorgan

In the game I'm writing, state is held in a single var *state*.
Actions are reified and represented by maps. So I have a single
function
(defmulti execute :Action)
that operates on *state* and returns a new state object (state is
immutable). So to execute a sequence of actions would look like this:
(defn execute-sequence
  [actions]
(reduce
  #(binding [*state* %1] (execute %2))
  *state*
  actions))

The advantage of this approach is that running multiple games at the
same time is absolutely trivial, because bindings are thread-local.

I doubt there would be significant overhead as a result of operating
on refs. Since there won't be any contention, every transactions is
guaranteed to work the first time you try it. But the usual rule of
performance optimization applies, i.e., measure.



On Oct 19, 4:38 pm, Adam Jones <[EMAIL PROTECTED]> wrote:
> I'm starting up work again on my Clojure-based game. Up until this
> point I've been doing things the "dirty" way by re-defing a bunch of
> global variables and/or storing the values in the Java objects I need
> to use to get access to the game framework.
>
> This approach is pretty unwieldy, and means I miss out on a lot of the
> cool things Clojure has to offer. So, I need to figure out how to
> handle state from within Clojure. The two ideas that I think will work
> are:
>
> 1. Store the game world as a var, and have the game structured as a
> function from the game world to the game world. Since I need to pass
> control back to Java to handle a lot of the details this would require
> me to store the game world in Java between updates.
>
> 2. Store the game world as a ref and use the facilities of that to
> modify the game state on updates. This seems cleaner in that it can
> all be handled without resorting to Java for intermediary storage. I
> am concerned that using the STM system would add too much performance
> overhead.
>
> I guess what this all comes down to is, between vars and refs, which
> one is the better choice for a program that I don't think will really
> need multithreading?
--~--~-~--~~~---~--~~
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: Newbie question on the use of functional hash maps

2008-10-19 Thread wwmorgan

If you're interested only in counting the number of unique words, then
you don't even need a map. You can get by with a set, like this:

(defn unique-words-in-file
  [file]
(count (set (split-on-whitespace (slurp file)

slurp reads file into a String object in memory. The hypothetical
split-on-whitespace takes a String and returns a collection of word
objects. set takes a collection and produces a set of the elements in
that collection. count counts the number of elements in the set.

If, on the other hand, you wanted a map from each word in the file to
the number of times that it appears, you might do it like this:

(defn word-counts
  [file]
(reduce
  (fn [map word] (assoc map word (inc (get map word 0
  {}
  (split-on-whitespace (slurp file

The reduce starts with the empty map {}, and then for each word in the
file, produces a new map by invoking the anonymous function supplied
as the first argument to reduce.

You could also get the same result with a list comprehension, using
for:

(defn word-counts
  [file]
(apply merge-with +
  (for [word (split-on-whitespace (slurp file))]
{word 1})))

Here we emit a map for each word in the file, mapping that word to 1.
Then we merge all the maps together, using + when two maps contain the
same key.  This function has a small bug: it throws an exception if
the file contains no words. To fix it, you would insert an additional
argument to apply, the empty map {}.

On Oct 19, 9:16 pm, "Tom Emerson" <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I have a somewhat embarassing newbie question on the use of hash maps
> in a functional environment.
>
> Consider a little utility that counts the number of unique words in a
> file. A hash map mapping strings to integers is the obvious data
> structure for this, but the fact that (assoc) returns a new map each
> time it is called is tripping me up: since I can't define a 'global'
> hash map to accumulate the counts, do you pass one around in a
> function? or do you structure the code a different way? This is a
> difference from what I would do in Common Lisp, where I would just
> have a global that is used for the collection.
>
> Thanks in advance for your wisdom.
>
> -tree
>
> --
> Tom Emerson
> [EMAIL PROTECTED]://www.dreamersrealm.net/~tree
--~--~-~--~~~---~--~~
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: offtopic - where are you come from? (poll)

2008-10-19 Thread [EMAIL PROTECTED]

Austin, Texas, USA.

On Oct 17, 9:18 am, "Eric Rochester" <[EMAIL PROTECTED]> wrote:
> Atlanta, Georgia, US
>
> On Fri, Oct 17, 2008 at 5:27 AM, Rastislav Kassak <[EMAIL PROTECTED]>wrote:
>
>
>
> > Hello Clojurians,
>
> > I think after 1st year of Clojure life it's good to check how far has
> > Clojure spread all over the world.
>
> > So wherever are you come from, be proud and say it.
>
> > I'm from Slovakia. :)
>
> > RK

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

2008-10-19 Thread Alex Miller

Rich, I'm the tech lead for the transparency team at Terracotta and
this is not exactly correct.  For example, while you can read
clustered state outside of a clustered lock, it's possible for the tc
memory manager to clear that state at any time, allowing you to see a
null instead of the real value.  Generally, if you're not reading
under at least a read lock, you can see nulls.

Now, all is not lost because an immutable data structure requires only
a read lock and read locks can be checked out to all the nodes in the
cluster and used locally as a write will never occur.  Read locks like
that will be served locally without crossing the network and will not
generate tc transactions.  So, this will be a really cheap lock to get
even in the cluster.

Maps (especially CHM) are also a fairly specialized case as we heavily
instrument and modify the internal behavior.  Map impls like HashMap,
Hashtable, and CHM are all "partial" in tc, meaning that the keys must
always be in all nodes but values are faulted lazily as needed.

Currently, volatiles are not really treated any differently than other
non-volatile fields - we require that they be accessed under a
clustered lock.  The whole purpose of volatile isn't really possible
with Terracotta so we can't really do anything special.  We do have
some internal support for automatically wrapping volatiles in
clustered locks but haven't surfaced that out to our config file
yet.

You're correct on the CAS - in general the stuff that really makes
single VM concurrency fly (like fine-grained locks with lightweight
impls like CAS) are awful for clustering.  For things like sequences,
it's generally better to use checked out blocks of ids.  We've got an
implementation of this if you want it (it's pretty straightforward).
If that's not sufficient, I'm sure we could come up with some other
solution.

I have a keen interest in Clojure (awesome stuff Rich) and obviously
also Terracotta.  If only I had another 5 hrs/day, I'd spend it on
Clojure/TC integration.  :)  I (and others at Terracotta) are
certainly happy to answer questions and help where we can.  Probably
best to ask on our mailing lists as everyone in eng monitors the user
and dev lists:

http://www.terracotta.org/confluence/display/wiki/Mailing+Lists

Alex Miller


On Oct 18, 7:50 am, "Rich Hickey" <[EMAIL PROTECTED]> wrote:
> On Fri, Oct 17, 2008 at 8:01 PM, Luc Prefontaine
>
>
>
> <[EMAIL PROTECTED]> wrote:
> > I am not very far from tackling this issue. In our bus messaging system, we
> > are using Terracotta with some Java components
> > and it's a matter of weeks before we start to investigate how we can bridge
> > Clojure and Terracotta.
>
> > A customer asked us about some new functionality today and I see a need to
> > fill the Terracotta/Clojure gap
> > somehow.
>
> > I'll comeback toward the end of November with some proposal.
>
> > Any comments Rich about how you would see this integration and what Clojure
> > semantics you would like to share through Terracotta ?
> > I might enlarge the scope beyond what we need in our system even if not all
> > the pieces are delivered in the very short term.
>
> There are lots of potential synergies. I think that one key benefit of
> using Clojure is that the immutable data structures can be shared, yet
> read outside of locks. As you know, Terracotta requires shared objects
> to be accessed under a lock. However, once the object has been
> propagated, it need not be acceessed under a lock iff it is immutable.
> This was one of the first things I verified with Terracotta.
>
> So, for instance, you can do the normal Terracotta cache thing and put
> Clojure's persistent data structures in a ConcurrentHashMap shared
> root. Once you pull one out of the map, you can use it henceforth
> without locking - a huge benefit IMO. Plus, since the data structures
> share structure, updates are also efficient. A current hitch, which I
> am looking to enhance anyway, is that some of the data structures do
> lazy hash creation with volatile caches. In proc this is no problem,
> nor out of proc since the hash value is a pure function of the
> immutable structure value, but I think Terracotta may not be happy
> with the volatile members. I have already on my todo list moving to
> incrementally calculated hash values (had to wait for the unification
> of hash logic with java.util's, now done).
>
> Of course, this leaves you with ConcurrentHashMap's last-one-in-wins,
> no coordinated activity semantics. When that's not good enough, you'll
> want to try STM refs + transactions. Here too, I think a lot is
> possible. As I've said, I once had this working, but haven't tracked
> whether or not all mechanisms I am using are supported by Terracotta.
> Underneath the STM are all standard Java concurrency things -
> reentrant locks, wait/notify, java.util.concurrent.atomic stuff etc.
> To the extent these are supported, it should function correctly right
> away.
>
> That said, there's one aspect 

Re: Beginners (and idiomatic Clojure)

2008-10-19 Thread Krukow



On Oct 20, 12:30 am, Paul Barry <[EMAIL PROTECTED]> wrote:
> Krukow,
>
> I agree, it would help to have a resource for learning Clojure.  For
> now, my best advice is to pick a real project to start working and
> then specific questions in the IRC room, #clojure on irc.freenode.net.
>
> Within a few months, we'll have the beta 
> book:http://www.pragprog.com/titles/shcloj/programming-clojure

Wow, I wasn't expecting a book this early. Hope the quality will be as
high as the other material on the web -- will definitely pre-order it.
Thanks for the hint.


/krukow


--~--~-~--~~~---~--~~
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: offtopic - where are you come from? (poll)

2008-10-19 Thread Tatu Tarvainen



> So wherever are you come from, be proud and say it.

Oulu, Finland.

>
> I'm from Slovakia. :)
>
> RK
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---