Re: Proxying in clojure

2008-12-26 Thread Emeka
Chooser,

Please send me yours then.

Emeka

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Help with Slime

2008-12-26 Thread Emeka
>
> Kogu
>
> Below is the relevant part of my init.el.

Which file is init.el? Where can I find it? I use windows vista.

Emeka

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Proxying in clojure

2008-12-26 Thread Chouser

On Fri, Dec 26, 2008 at 4:40 AM, Emeka  wrote:
> Chooser,
"Chouser"

> Please send me yours then.

Already did:
http://groups.google.com/group/clojure/msg/4f00d2a3b5da8444

And Mr. Gilardi improved on it here:
http://groups.google.com/group/clojure/msg/90316675320091cf

--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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



How To Make Code More Functional?

2008-12-26 Thread aria42

Hi all,
  I'm just getting started with clojure from a functional background,
and while I like playing with clojure and accomplishing script like
tasks, I have no experience with anything larger than about 20 lines.
I wanted to try to take something with "alot of state" and put it into
clojure. I decided to code Tarjan's Algorithm for finding all the
strongly connected components of a graph (http://en.wikipedia.org/wiki/
Tarjan's_strongly_connected_components_algorithm). I've written this
code in Java and it's about a 100 lines. Sadly, my clojure version is
about a 100 lines too. I am more-or-less translating my java code
(which is more or less translated from Psuedo-Code), but I don't see a
good way to make this problem more functional, but this is probably
due to my imperative roots.

  I have the code posted as an attachment here or posted at
http://www.cs.berkeley.edu/~aria42/tarjan.clj.


  Let me know if there's more canonical / functional ways to do
something like this.

  Thanks, Aria
--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: How To Make Code More Functional?

2008-12-26 Thread aria42

Clojure for Tajan's Algorithm uploaded here
http://clojure.googlegroups.com/web/tarjan.clj?gsc=yOHJ-CEAAAB3Fq8nFW3O6gqQkWXH_xrOYRvSPFZyhAT412614U6EGkzfKN-m9S9niuHrq-IEXAE

- aria

On Dec 26, 6:30 am, aria42  wrote:
> Hi all,
>   I'm just getting started with clojure from a functional background,
> and while I like playing with clojure and accomplishing script like
> tasks, I have no experience with anything larger than about 20 lines.
> I wanted to try to take something with "alot of state" and put it into
> clojure. I decided to code Tarjan's Algorithm for finding all the
> strongly connected components of a graph (http://en.wikipedia.org/wiki/
> Tarjan's_strongly_connected_components_algorithm). I've written this
> code in Java and it's about a 100 lines. Sadly, my clojure version is
> about a 100 lines too. I am more-or-less translating my java code
> (which is more or less translated from Psuedo-Code), but I don't see a
> good way to make this problem more functional, but this is probably
> due to my imperative roots.
>
>   I have the code posted as an attachment here or posted 
> athttp://www.cs.berkeley.edu/~aria42/tarjan.clj.
>
>   Let me know if there's more canonical / functional ways to do
> something like this.
>
>   Thanks, Aria
--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Exercise: words frequency ranking

2008-12-26 Thread lpetit

Instead of #(- (val %)), one could also use the compose function :
(comp - val)

My 0,02 EURO,

--
Laurent

On Dec 25, 4:58 pm, Mibu  wrote:
> My version:
>
> (defn top-words [input-filename result-filename]
>   (spit result-filename
> (apply str
>(map #(format "%s : %d\n" (first %) (second %))
> (sort-by #(-(val %))
>  (reduce #(conj %1 { %2 (inc (%1 %2 0)) }) {}
>  (map #(.toLowerCase %)
>   (re-seq #"\w+"
>   (slurp 
> input-filename)
>
> Mibu
>
> On Dec 25, 2:16 pm, Piotr 'Qertoip' Włodarek 
> wrote:
>
> > Given the input text file, the program should write to disk a ranking
> > of words sorted by frequency, like:
>
> >  the : 52483
> >  and : 32558
> >   of : 23477
> >a : 22486
> >   to : 21993
>
> > My first implementation:
>
> > (defn topwords [in-filepath, out-filepath]
> >   (def words (.split (.toLowerCase (slurp in-filepath)) "\\s+"))
>
> >   (spit out-filepath
> > (apply  str
> > (concat
> >   (map (fn [pair] (format "%20s : %5d \r\n" (key pair)
> > (val pair)))
> >(sort-by #( -(val %) )
> > (reduce
> >   (fn [counted-words word]
> >   ( assoc counted-words
> >   word
> >   (inc (get counted-words
> > word 0)) ))
> >   {}
> >   words)))
> >   ["\r\n"]
>
> > Somehow I feel it's far from optimal. Could you please advise and
> > improve? What is the best, idiomatic implementation of this simple
> > problem?
>
> > regards,
> > Piotrek
--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Exercise: words frequency ranking

2008-12-26 Thread lpetit

What would you think of this form of coding ?
- The rationale is to separate functions that deal with system
"boundaries" from "core algorithmic functions".
So you should at least have two functions : one that does not deal
with input/output formats : will only deal with clojure/java
constructs.
- Don't expose "too early" functions that are just here to simplify
the algorithm : there's already the possibility to use defn- , but
there's also the possibility to embed functions in the principal
function by using let and inner functions
- And I also tried to write the "core algorithmic function" as
"functional" as I can.
Do you think the functional version is more ore less "obfuscated" ?

Here would be the "core function" (taking a string as an input, and
outputting the sorted sequence of ["word" 2] vectors) :

(defn topwords [str]
  "Takes a string as an input, and returns a sequence of vectors of
pairs [word nb-of-word-occurences]"
  (let [words (let [ls (System/getProperty "line.separator")]
#(.split % ls))
freqs (partial reduce #(merge-with + %1 {%2 1}) {})
sort (partial sort-by (comp - val))]
(-> str words freqs sort)))


HTH,
--
Laurent

On Dec 26, 4:37 pm, lpetit  wrote:
> Instead of #(- (val %)), one could also use the compose function :
> (comp - val)
>
> My 0,02 EURO,
>
> --
> Laurent
>
> On Dec 25, 4:58 pm, Mibu  wrote:
>
> > My version:
>
> > (defn top-words [input-filename result-filename]
> >   (spit result-filename
> >         (apply str
> >                (map #(format "%s : %d\n" (first %) (second %))
> >                     (sort-by #(-(val %))
> >                              (reduce #(conj %1 { %2 (inc (%1 %2 0)) }) {}
> >                                      (map #(.toLowerCase %)
> >                                           (re-seq #"\w+"
> >                                                   (slurp 
> > input-filename)
>
> > Mibu
>
> > On Dec 25, 2:16 pm, Piotr 'Qertoip' Włodarek 
> > wrote:
>
> > > Given the input text file, the program should write to disk a ranking
> > > of words sorted by frequency, like:
>
> > >                  the : 52483
> > >                  and : 32558
> > >                   of : 23477
> > >                    a : 22486
> > >                   to : 21993
>
> > > My first implementation:
>
> > > (defn topwords [in-filepath, out-filepath]
> > >   (def words (.split (.toLowerCase (slurp in-filepath)) "\\s+"))
>
> > >   (spit out-filepath
> > >         (apply  str
> > >                 (concat
> > >                   (map (fn [pair] (format "%20s : %5d \r\n" (key pair)
> > > (val pair)))
> > >                        (sort-by #( -(val %) )
> > >                                 (reduce
> > >                                   (fn [counted-words word]
> > >                                       ( assoc counted-words
> > >                                               word
> > >                                               (inc (get counted-words
> > > word 0)) ))
> > >                                   {}
> > >                                   words)))
> > >                   ["\r\n"]
>
> > > Somehow I feel it's far from optimal. Could you please advise and
> > > improve? What is the best, idiomatic implementation of this simple
> > > problem?
>
> > > regards,
> > > Piotrek
--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Exercise: words frequency ranking

2008-12-26 Thread Piotr 'Qertoip' Włodarek

On Dec 25, 4:58 pm, Mibu  wrote:
> My version:
>
> (defn top-words [input-filename result-filename]
>   (spit result-filename
>         (apply str
>                (map #(format "%s : %d\n" (first %) (second %))
>                     (sort-by #(-(val %))
>                              (reduce #(conj %1 { %2 (inc (%1 %2 0)) }) {}
>                                      (map #(.toLowerCase %)
>                                           (re-seq #"\w+"
>                                                   (slurp 
> input-filename)
>
> Mibu

Once you move .toLowerCase right after slurp, it gets 3 times faster.


regards,
Piotrek
--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Proxying in clojure

2008-12-26 Thread MattyDub

This might be off-topic, but when I launched "snake" from SLIME (via
load-file and then "run-snake"), the app didn't receive any UI
Events.  I thought at first it might be because the snake Frame didn't
have focus, but even when I gave it focus, it still didn't receive any
Events.  When I ran it from the clojure REPL (outside of SLIME), it
worked appropriately.  Is this expected behavior from SLIME?
-Matt

On Dec 26, 4:34 am, Chouser  wrote:
> On Fri, Dec 26, 2008 at 4:40 AM, Emeka  wrote:
> > Chooser,
>
> "Chouser"
>
> > Please send me yours then.
>
> Already did:http://groups.google.com/group/clojure/msg/4f00d2a3b5da8444
>
> And Mr. Gilardi improved on it 
> here:http://groups.google.com/group/clojure/msg/90316675320091cf
>
> --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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



sync vs. dosync

2008-12-26 Thread Mark Volkmann

What is the difference between the sync and dosync functions? Their
documentation strings are identical.

-- 
R. Mark Volkmann
Object Computing, Inc.

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



println output

2008-12-26 Thread Mark Volkmann

Why does

(for [x (range 3)] (println x))

output

(0
nil 1
nil 2
nil)

when run in the REPL instead of

0
1
2

and nothing at all when run from a script?

-- 
R. Mark Volkmann
Object Computing, Inc.

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: sync vs. dosync

2008-12-26 Thread Michael Wood

On Fri, Dec 26, 2008 at 9:44 PM, Mark Volkmann
 wrote:
>
> What is the difference between the sync and dosync functions? Their
> documentation strings are identical.

sync has an extra flags argument.  At the moment they are the same,
but presumably sync will do different things depending on the flags in
future.

Here is how they are currently defined:

(defmacro sync
  "transaction-flags => TBD, pass nil for now

  Runs the exprs (in an implicit do) in a transaction that encompasses
  exprs and any nested calls.  Starts a transaction if none is already
  running on this thread. Any uncaught exception will abort the
  transaction and flow out of sync. The exprs may be run more than
  once, but any effects on Refs will be atomic."
  [flags-ignored-for-now & body]
  `(. clojure.lang.LockingTransaction
  (runInTransaction (fn [] ~...@body

(defmacro dosync
  "Runs the exprs (in an implicit do) in a transaction that encompasses
  exprs and any nested calls.  Starts a transaction if none is already
  running on this thread. Any uncaught exception will abort the
  transaction and flow out of dosync. The exprs may be run more than
  once, but any effects on Refs will be atomic."
  [& exprs]
  `(sync nil ~...@exprs))

-- 
Michael Wood 

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: println output

2008-12-26 Thread mehrheit

On Fri, 26 Dec 2008 14:43:23 -0600
"Mark Volkmann"  wrote:

>
>Why does
>
>(for [x (range 3)] (println x))
>
>output
>
>(0
>nil 1
>nil 2
>nil)
>
>when run in the REPL instead of
>
>0
>1
>2
>
>and nothing at all when run from a script?
>

The seq of nils is the return value of `(for ...)'. It is printed
because the REPL shows return values and interpersed with println's
output, because `for' is lazy and the printlns are executed while the
seq is printed.

When run from a script (as a top level form, I assume), the return
value of the form is not used, therefore the tail-functions of the lazy
sequence aren't evaluated and nothing at all is printed. This can be
remedied by surrounding the `(for ...)' form with a `doall' or `dorun'
form, which both evaluate the whole sequence (they differ in their
return values).

In this case `doseq' should be used instead of `for'.

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: println output

2008-12-26 Thread Michael Wood

On Fri, Dec 26, 2008 at 10:43 PM, Mark Volkmann
 wrote:
>
> Why does
>
> (for [x (range 3)] (println x))
>
> output
>
> (0
> nil 1
> nil 2
> nil)
>
> when run in the REPL instead of
>
> 0
> 1
> 2

This is because println returns nil every time it's run.

user=> (println "test")
test
nil
user=>

Also:

user=> (for [x (range 3)] nil)
(nil nil nil)
user=>

So the result of the for is a seq containing three nils.  The output
from println is interspersed with the printing of the seq.

> and nothing at all when run from a script?

It doesn't print anything when run from a script because range returns
a lazy sequence.  Here's a recent post with a similar question:

http://groups.google.com/group/clojure/browse_thread/thread/53a32d222c3b40e3#

Chouser said the following:

> > This does not work - prints nothing - why?:
> > (defn pretty-print-row [row]
> >  (map print row))
>
> Because 'map' is lazy, and won't evaluate 'print' on any of the items
> sequence unless necessary.  For functions with side-effects (like
> 'print') you'll generally need 'doseq', loop/recur, or 'dorun'.

-- 
Michael Wood 

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Accessing "this" in gen-class constructor

2008-12-26 Thread CuppoJava

Hi,
I've hit a stumbling block using Clojure's gen-class facility for
constructors.
Is there anyway to access "this" inside Clojure's constructor/init
function?

ie. The following type of code is quite common in Java. How would you
do the same in Clojure?

public class MyDerivedClass extends SuperClass{  //<-- Derived Class
  public MyDerivedClass (){
super("derived class");   //<-- Call superclass constructor
setDescription("this is a derived class");  //<-- Call inherited
method
  }
}

Thanks
  -Patrick
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Proxying in clojure

2008-12-26 Thread Abhishek Reddy

Works for me from SLIME.  Check your *inferior-lisp* buffer for exceptions.

On 12/27/08, MattyDub  wrote:
>
> This might be off-topic, but when I launched "snake" from SLIME (via
> load-file and then "run-snake"), the app didn't receive any UI
> Events.  I thought at first it might be because the snake Frame didn't
> have focus, but even when I gave it focus, it still didn't receive any
> Events.  When I ran it from the clojure REPL (outside of SLIME), it
> worked appropriately.  Is this expected behavior from SLIME?
> -Matt
>
> On Dec 26, 4:34 am, Chouser  wrote:
>> On Fri, Dec 26, 2008 at 4:40 AM, Emeka  wrote:
>> > Chooser,
>>
>> "Chouser"
>>
>> > Please send me yours then.
>>
>> Already did:http://groups.google.com/group/clojure/msg/4f00d2a3b5da8444
>>
>> And Mr. Gilardi improved on it
>> here:http://groups.google.com/group/clojure/msg/90316675320091cf
>>
>> --Chouser
> >
>


-- 
Abhishek Reddy
http://abhishek.geek.nz

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: How To Make Code More Functional?

2008-12-26 Thread CuppoJava

I'm just getting started myself, and I found Stuart Halloway's blog of
great use. Perhaps it'll be of some help to you also.
  -Patrick
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



distinct broken?

2008-12-26 Thread tristan

Hi All,

I've been trying to learn clojure lately by solving the project euler
problems with it, and I think the distinct function is broken (or it
doesn't quite work as I assume it would).

here is the code i'm running


(defn pow [nbr pwr]
  (if (< pwr 2)
nbr
(* nbr (pow nbr (dec pwr)

(count (sort (distinct (apply concat (map (fn [i] (map (fn [j] (pow i
j)) (range 2 101))) (range 2 101))

for which the result shows 9188, but should be 9183.

I wrote my own distinct function which gives the correct result but
runs a LOT slower

(defn in? [lst n]
  (if (nil? lst)
false
(if (= (first lst) n)
  true
  (in? (rest lst) n

(defn unique [lst]
  (loop [l lst n (list)]
(if (nil? l)
  (sort n)
  (if (in? n (first l))
(recur (rest l) n)
(recur (rest l) (cons (first l) n))

i'm using revision 1185.
is this a bug or am i doing something wrong?

thanks
-Tristan

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



macro help

2008-12-26 Thread what-a-guy

I'm attempting what should be a simple transformation using a macro
called dlg in the following code:
(defn fld [parent lay id text field]
  '...)

;; dlg macro.  For this input:
;;
;; (dlg "test"
;;  (field fld-1 "Field number one" (JTextField.))
;;  (field fld-2 "Field number two" (JTextField.)))
;;
;; we want this output:
;;
;; (fn [parent layout]
;;   (fld parent layout 'fld-1 "Field number one" (JTextField.))
;;   (fld parent layout 'fld-2 "Field number two" (JTextField.))
;;   parent)

(defmacro dlg [dlgid# & fields#]
  `(fn  [parent# layout#]
 ~@(map
(fn [[f# id# text# type#]]
  `(fld parent# layout# '~id# ~text# ~type#))
fields#)))

(def inp '(dlg "test"
   (field fld-1 "Field number one" (JTextField.))
   (field fld-2 "Field number two" (JTextField.

(macroexpand inp)

;; =>
;; (fn* ([parent__6 layout__7]
;;  (user/fld parent__4 layout__5 (quote fld-1) "Field number
one" (JTextField.))
;;  (user/fld parent__4 layout__5 (quote fld-2) "Field number
two" (JTextField.

(eval inp)

;; =>
;; java.lang.Exception: Unable to resolve symbol: parent__4 in this
context (NO_SOURCE_FILE:24)

I'm wondering why the macro produces 2 different "parent" symbols--I'd
like both references to be the same parent.  Any suggestions?

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



macro help

2008-12-26 Thread what-a-guy

I'm attempting what should be a simple macro transformation (dlg
below):

(defn fld [parent lay id text field]
  '...)

;; dlg macro.  For this input:
;;
;; (dlg "test"
;;  (field fld-1 "Field number one" (JTextField.))
;;  (field fld-2 "Field number two" (JTextField.)))
;;
;; we want this output:
;;
;; (fn [parent layout]
;;   (fld parent layout 'fld-1 "Field number one" (JTextField.))
;;   (fld parent layout 'fld-2 "Field number two" (JTextField.))
;;   parent)

(defmacro dlg [dlgid# & fields#]
  `(fn  [parent# layout#]
 ~@(map
(fn [[f# id# text# type#]]
  `(fld parent# layout# '~id# ~text# ~type#))
fields#)))

(def inp '(dlg "test"
   (field fld-1 "Field number one" (JTextField.))
   (field fld-2 "Field number two" (JTextField.

(macroexpand inp)

;; =>
;; (fn* ([parent__6 layout__7]
;;  (user/fld parent__4 layout__5 (quote fld-1) "Field number
one" (JTextField.))
;;  (user/fld parent__4 layout__5 (quote fld-2) "Field number
two" (JTextField.

(eval inp)

;; =>
;; java.lang.Exception: Unable to resolve symbol: parent__4 in this
context (NO_SOURCE_FILE:24)

The dlg macro seems to be generating 2 different symbols for 'parent,
when I'd like both to refer to the
same symbol.  Any suggestions?

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



IntelliJ Plugin

2008-12-26 Thread Justin Johnson
Hi,

Is this the appropriate mailing list to talk about the Clojure IntelliJ
plugin?  The Google Code site didn't list any other mailing list.

http://code.google.com/p/clojure-intellij-plugin/

I went through the process of building and installing the plugin on Windows
XP with IntelliJ IDEA 8.0.1 and thought it might be helpful if I document
what I did on the wiki.  I also have a small suggestion that the build.xml
file use environment variables instead of hard coded paths to java.home and
idea.home.

Thanks,
Justin

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



macro help

2008-12-26 Thread what-a-guy

I'm getting stuck on what should be a simple macro (dlg below).  It
produces 2 different symbols (for 'parent#) when I was expecting both
to refer to the same symbol.  Here's the code:

(defn fld [parent lay id text field]
  '...)

;; dlg macro.  For this input:
;;
;; (dlg "test"
;;  (field fld-1 "Field number one" (JTextField.))
;;  (field fld-2 "Field number two" (JTextField.)))
;;
;; we want this output:
;;
;; (fn [parent layout]
;;   (fld parent layout 'fld-1 "Field number one" (JTextField.))
;;   (fld parent layout 'fld-2 "Field number two" (JTextField.))
;;   parent)

(defmacro dlg [dlgid# & fields#]
  `(fn  [parent# layout#]
 ~@(map
(fn [[f# id# text# type#]]
  `(fld parent# layout# '~id# ~text# ~type#))
fields#)))

(def inp '(dlg "test"
   (field fld-1 "Field number one" (JTextField.))
   (field fld-2 "Field number two" (JTextField.

(macroexpand inp)

;; =>
;; (fn* ([parent__6 layout__7]
;;  (user/fld parent__4 layout__5 (quote fld-1) "Field number
one" (JTextField.))
;;  (user/fld parent__4 layout__5 (quote fld-2) "Field number
two" (JTextField.

(eval inp)

;; =>
;; java.lang.Exception: Unable to resolve symbol: parent__4 in this
context (NO_SOURCE_FILE:24)

Any suggestions?

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



macro help

2008-12-26 Thread what-a-guy

I'm attempting what should be a simple transformation using a macro
called dlg in the following code:
(defn fld [parent lay id text field]
  '...)

;; dlg macro.  For this input:
;;
;; (dlg "test"
;;  (field fld-1 "Field number one" (JTextField.))
;;  (field fld-2 "Field number two" (JTextField.)))
;;
;; we want this output:
;;
;; (fn [parent layout]
;;   (fld parent layout 'fld-1 "Field number one" (JTextField.))
;;   (fld parent layout 'fld-2 "Field number two" (JTextField.))
;;   parent)

(defmacro dlg [dlgid# & fields#]
  `(fn  [parent# layout#]
 ~@(map
(fn [[f# id# text# type#]]
  `(fld parent# layout# '~id# ~text# ~type#))
fields#)))

(def inp '(dlg "test"
   (field fld-1 "Field number one" (JTextField.))
   (field fld-2 "Field number two" (JTextField.

(macroexpand inp)

;; =>
;; (fn* ([parent__6 layout__7]
;;  (user/fld parent__4 layout__5 (quote fld-1) "Field number
one" (JTextField.))
;;  (user/fld parent__4 layout__5 (quote fld-2) "Field number
two" (JTextField.

(eval inp)

;; =>
;; java.lang.Exception: Unable to resolve symbol: parent__4 in this
context (NO_SOURCE_FILE:24)

I'm wondering why the macro produces 2 different "parent" symbols--I'd
like both references to be the same parent.  Any suggestions?

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Random Number Generation Issues

2008-12-26 Thread Mark H.

On Dec 23, 3:10 pm, Jason  wrote:
> For the time-being, you could try something like:
>
> user> (def *random* (java.util.Random.))
> #'user/*random*
>
> user> (defn my-rand-int [max-val]
>         (let [bit-length (.bitLength (BigInteger. (str max-val)))]
>           (loop []
>             (let [x (BigInteger. bit-length *random*)]
>               (if (< x max-val)
>                   x
>                 (recur))
> #'user/my-rand-int

BigInteger has a constructor that takes a byte[] array, so you could
just give it an array of randomly generated bytes.  I'll work on some
code to do that.

mfh
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: How to encapsulate local state in closures

2008-12-26 Thread Adrian Cuthbertson


On Dec 22, 2:34 pm, "Mark Engelberg"  wrote:
> On Mon, Dec 22, 2008 at 4:23 AM, Parth Malwankar
>
>  wrote:
> > If I get it right, atoms are quite useful to maintain state
> > in the context of a single thread with memoization and
> > counter (within a thread) being two examples.
>
> No, RH said that atoms were definitely intended for multiple threads,
> not just single threads.  But their use is highly specific.  With
> memoization, it doesn't matter if things get retried, as long as
> things don't get "lost".  atombasically guarantees that the ref and
> the set occur atomically (via swap), so you don't have to worry about
> two threads losing something from the cache as follows:
> Current cache {:a 1 :b 2}
> One thread tries to add :c 3, and another tries to add :d 4.
> Without atomic swap, one thread could try to update the cache to {:a 1
> :b 2 :c 3} and the other to {:a 1 :b 2 :d 4} (because they are both
> basing their updates on what they see).  Whichever one wins, one of
> the values will be "lost" from the cache.)
> So atoms make this one guarantee, allowing safe multithread
> memoization, but at great risk for other types of applications,
> because most "seemingly-obvious" uses for atoms would probably be
> hosed by the possible retry.
>
> I fear a lot of people are going to end up misusing atoms.  I assume
> they were necessary to make memoization perform better than under the
> ref-with-commute approach.

It's important to distinguish between updating atoms within
transactions and outside transactions. In the former case, one has to
ensure the update function can be retried without ill-effects.
However, outside a transaction, atoms are just mutable values, that
can safely be shared between threads, provided that their updating
does not need to be coordinated with other updates (to other atoms,
refs or agents).

Here's an example; say we have a multi-threaded service where we wish
to share a single counter to use as say a serial id. The following
code initializes serid to 0 and provides a function incserid to
increment it.

(def serid (atom 0))
(defn incserid [] (swap! serid inc))
(defn docount [n cntr] (dotimes [ind n] (cntr)))

Now, the following uses a thread factory to instantiate (nthreads)
number of threads, each which will execute the above incrementor
(ncount) number of times...

(import '(java.util.concurrent Executors))
(def th-factory (Executors/newSingleThreadExecutor))
(defn do-thrds [nthreads ncount] (dotimes [t nthreads] (.submit th-
factory (partial docount ncount incserid

So, if we then initiate 100 threads to each increment the serid 1
times...

(do-thrds 100 1)
@serid
=>100

We see that the threads have happily shared the atom and updated it
correctly with no ill-effects. Note the lack of requirement for
dosync.

So in summary, atoms are great (and should be used in preference to
refs) where the shared state needs to be mutable, shared and
independent (not requiring coordinated update with other objects).

Regards, 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
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 encapsulate local state in closures

2008-12-26 Thread Mark Engelberg

On Fri, Dec 26, 2008 at 8:35 PM, Adrian Cuthbertson
 wrote:
> It's important to distinguish between updating atoms within
> transactions and outside transactions. In the former case, one has to
> ensure the update function can be retried without ill-effects.
> However, outside a transaction, atoms are just mutable values, that
> can safely be shared between threads, provided that their updating
> does not need to be coordinated with other updates (to other atoms,
> refs or agents).

Yes, but when you write your atom-based code, you have no way to know
whether you or others who want to reuse it will want to use it as part
of a transaction.  atom-based code is not generally safe for
transactions, which is why I suggested it should be avoided.  In your
example, if incserid is used in a transaction, it is possible that
certain serids will be skipped.  This may be acceptable, in which
case, go ahead and use an atom, but often programs rely on subtle
assumptions (like the serids will be issued consecutively), and your
program can become brittle if there's a chance your code won't follow
these assumptions.  Probably better off not to take the chance.  Stick
with something like refs which will yield more predictable behavior,
and thus be easier to test.  Memoization is a very special exception,
because it really doesn't matter if something gets cached more than
once.

The whole point of Clojure seems to make as much code as possible safe
for its software transactional memory.  Thus the persistent data
structures, and other design details.  Although interoperating with
Java also produces risk within transaction, generally speaking, if you
stay within the "Clojure core", you're safe for transactions.  Except
atoms.

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Exercise: words frequency ranking

2008-12-26 Thread Mibu

I wrote what I think is the idiomatic version. Idiomatically, you
delay execution of functions over lazy sequences, so if you get a
sequence of a million words and you only take the first 100, you don't
lowercase (or whatever else) the entire sequence. Also a smart
compiler on a multi-core machine could do a much better job dispensing
the computation with a program written this way. I doubt today's JITs
are this smart, but I'm sure someday soon they will be. Of course,
this doesn't apply to this specific example with slurp, because how it
works as the inner most function. But again, I was being idiomatic. If
you need a blazing fast implementation for this specific task, I
suspect perl would be a better choice.

Mibu


On Dec 26, 7:11 pm, Piotr 'Qertoip' Włodarek 
wrote:
> On Dec 25, 4:58 pm, Mibu  wrote:
>
> > My version:
>
> > (defn top-words [input-filename result-filename]
> >   (spit result-filename
> >         (apply str
> >                (map #(format "%s : %d\n" (first %) (second %))
> >                     (sort-by #(-(val %))
> >                              (reduce #(conj %1 { %2 (inc (%1 %2 0)) }) {}
> >                                      (map #(.toLowerCase %)
> >                                           (re-seq #"\w+"
> >                                                   (slurp 
> > input-filename)
>
> > Mibu
>
> Once you move .toLowerCase right after slurp, it gets 3 times faster.
>
> regards,
> Piotrek
--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: Accessing "this" in gen-class constructor

2008-12-26 Thread CuppoJava

I found that I can use another factory method to workaround this
limitation. I can first instantialize the object to get a reference,
and then initialize all it's settings. This works only if I don't
expect this class to be derived from. Any subclass would expect the
class to be fully initialized in the constructor, and not have to call
another initialize_settings() function.
--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



sorted-map-by value

2008-12-26 Thread Mibu

Is there a way to sort a sorted-map-by by value without a letrec?

--~--~-~--~~~---~--~~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---