Clojure finally on SPOJ!

2010-07-25 Thread sphere research
Hi,

now, you can solve problems/submit problems in Clojure on SPOJ,

good luck,

regards,
SPOJ Team

ps.

We are very happy to announce that users' accounts have finally
appeared on Ideone :)
If you liked Ideone as it has been so far, you will like the new one
even more. The most important new features available for Ideone users
are:

* management panel where users can view and group their pastes,
* personalized access to Ideone API
* and public folders which allow to publish and share a group of
pastes at one go.

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Why can not alter or ref-set ref after commute it?

2010-07-25 Thread dennis
Alter or ref-set a ref after commute would throw a
IllegalStateException:Can't set after commute

for example:

user= (def counter (ref 0))
#'user/counter
(dosync (commute counter inc) (ref-set counter 3))
java.lang.IllegalStateException: Can't set after commute
(NO_SOURCE_FILE:0)

I want to know why this should not happen?is it a explanation here? I
can't understand what is the difference with  commuting ref  after ref-
set or alter.Thanks a lot.

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Improvents on agent,user-custom thread pool.

2010-07-25 Thread dennis

Agent use two thread pools to execute actions,send use a fixed thread
pool (2+cpus threads),and send-off use a cached thread pool.These
pools are global in clojure system.

I think the Agent should allow users to customize the thread pool,
if no custom,  then use the global thread pool.
   Why do I need a custom thread pool?
   First, the default thread pool is global, send use the thread pool
is a fixed size cpus +2, is likely to become the system bottleneck
sometime. Although you can use the send-off, use the cache thread
pool, but in a real world application, I can not use the cache thread
pool, which will introduce the risk of OutOfMemoryError, normally I
like to use a fixed-size thread pool.

  Second, the actions which global thread pool execute are from a
variety of agents, the actions are not homogeneous, and can not
maximize the efficient use of the thread pool, we hope that you can
specify different agent to isolate a particular thread pool to
maximize the use of thread pool .

   I think Agent could add two new functions:

(set-executor! agent (java.util.concurrent.Executors/
newFixedThreadPool 2))
(shutdown-agent agent)

set-executor! is to set the agent's custom thread pool,and shutdown-
agent to shutdown the agent's custom thread pool.



-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Remove-first function

2010-07-25 Thread nickikt
@Randy Hudson
Really like that solution.

@Mark Engelberg
Thanks for the explanation


On Jul 25, 4:33 am, ataggart alex.tagg...@gmail.com wrote:
 To add one small addendum to Mark's excellent comment, if you use lazy-
 seq then you don't need to worry about the nil from when

 On Jul 24, 12:01 pm, Mark Engelberg mark.engelb...@gmail.com wrote:



  On Sat, Jul 24, 2010 at 11:45 AM, Mark Engelberg

  mark.engelb...@gmail.com wrote:
   The simplest translation is to wrap a lazy-seq around the last line to
   avoid the stack overflows.

  Just to clarify, there are at least three reasonable places to place
  the call to lazy-seq.  You can put lazy-seq around the full body of
  the function.  You can place it around the cons.  You can place it
  around the recursive call to scheme-remove-first.  Each choice results
  in slightly different laziness behavior, i.e., when various elements
  are computed, but the overall semantics of the sequence remains the
  same and stack overflows will be avoided.  Placing the lazy-seq around
  the recursive function call will cause scheme-remove-first to compute
  the first element right away, and delay the rest.  Placing the
  lazy-seq around the full body will prevent any computation until it is
  asked for by a consumer.  Placing the lazy-seq around the cons results
  in in immediate behavior for the nil and
  removable-item-at-front-of-list case, and delayed behavior otherwise.
  All are acceptable choices, but preferences vary.  Probably placing
  lazy-seq around the full body is the most common style you'll see in
  Clojure, although I tend to place it where the laziness is actually
  required (like around the recursive call, or around the cons).

  You've probably noticed from the other samples posted that many
  Clojurians prefer to use (seq lst) instead of (not (empty? lst)), and
  organize their code around the not-empty case.

  So (if (empty? lst) empty-case not-empty-case) becomes (if (seq lst)
  not-empty-case empty-case)

  When the empty case also results in nil, you can replace the if
  structure with a one-armed when, because when automatically returns
  nil in the other case.

  So (if (seq lst) not-empty-case nil) becomes (when (seq lst) 
  not-empty-case).

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Remove-first function

2010-07-25 Thread Gary Fredericks
Well obviously if you can get something to be tail-recursive you won't have
the stack overflows, and the thing in your code that prevents tail recursion
is having to cons the result of the recursive call. So let's try this:

(defn remove-first
  [syb lst]
  (let [[before after]
  (loop [b [] a lst]
(if (empty? lst)
  [b a]
  (if (= syb (first a))
[b (rest a)]
(recur (cons (first a) b) (rest a)]
   (concat (reverse before) after)))

user= (remove-first 4 '(1 5 3 4 2 6 674 4 2))
(1 5 3 2 6 674 4 2)

I'm interested if somebody comes up with something more efficient, i.e. that
doesn't require the reversal.

Gary

On Sat, Jul 24, 2010 at 11:41 AM, nickikt nick...@gmail.com wrote:

 Hallo all,

 I'm working trough Essentials of Programming Languages. I'm trying to
 right a function like this one:

 (defn scheme-remove-first [syb lst]
  (if (empty? lst)
'()
(if (= (first lst) syb)
  (rest lst)
  (cons (first lst) (scheme-remove-first syb (rest lst))

 in a idiomatic clojure way (this is just a scheme to clojure 1:1
 version). I don't like that this function produces stack overflows.

 I tried some stuff but I it (almost) semantically correct working but
 I didn't like my code. Can anyone come up with a good version?

 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.comclojure%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en




-- 
Gary Fredericks
(660)-623-1095
fredericksg...@gmail.com
www.gfredericks.com

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: ANN: clj-dropbox, Dropbox client library in Clojure

2010-07-25 Thread Mikael Sundberg
Looks realy nice
i Tried it, and i cant get the example oath dance to work. i get:
Bad Response: 403
{error: Token is not an authorized request token.}
  [Thrown class java.lang.RuntimeException]

i used  or nil as a callback-url. i assume thats where the problem is.

if i do the dance by hand and copy the result the client works great!


/Micke

2010/7/23 Marc Spitzer mspit...@gmail.com:
 I need to look at this.  Thanks for the toy.

 marc

 On Thu, Jul 22, 2010 at 8:23 PM, aria42 ari...@gmail.com wrote:
 Hi all,

 I've released my clojure library for accessing the Dropbox API. Its
 called clj-dropbox and it can be found at:

 http://github.com/aria42/clj-dropbox

 Hope some people get use out of it. Feel free to email or leave
 comments and/or suggestions

 Best,
 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
 Note that posts from new members are moderated - please be patient with your 
 first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en



 --
 Freedom is nothing but a chance to be better.
 --Albert Camus

  The problem with socialism is that eventually you run out
 of other people's money.
 --Margaret Thatcher

 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with your 
 first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Efficiency of reduce function for lists.

2010-07-25 Thread samnardoni
I have a simple string (or list of characters to be precise) in a form
of: 123456789.

I want to parse this string and end up with: 123569.

The  is essentially the same as a backspace.

I managed to implement this fairly simply using the reduce function -
source: http://gist.github.com/489019.

Although it can be implemented easily using the reduce function, I was
concerned with the performance (especially with large inputs). As the
reduce function invokes the provided function with the previous
result, this list that is getting built is being passed around a huge
amount.

I'm not too sure as to what will be going on under the hood in this
sample. Whether the previous result of the function application is
really being passed around or not clear to me.

For the program, I know that when processing a character, I do not
need the previous result. All I need is the current character, and I
can return a function that acts on the result. I'm not sure if this is
simple to implement in a functional way.

So, I have 2 questions:

1) Is this a correct use of the reduce function? (I know there is a
clue in the word reduce, but it's also called inject and else in
other languages.)

2) Is there a more efficient way of solving this?

I appreciate any replies (for such a trivial topic). Answers to this
will hopefully enlighten me to using functional languages more
effectively, or possibly to a more suitable high-order 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
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: java gui is flashing

2010-07-25 Thread Ryan Sattler
I've been working on a game with Clojure/Swing lately and the simplest
way to avoid flashing is to draw to a bufferedImage first, then draw
that bufferedImage all at once. This means that parts of the screen
that don't change won't be briefly overwritten by the background
color, avoiding flashing. Here's some code:

(defn render [game window]
  (let [#^BufferedImage image (.createImage window window-width window-
height)
#^Graphics gfx (.createGraphics image)
#^Graphics2D gfx2 (.getGraphics #^JFrame window)]

  (render-background gfx)
  ;draw more stuff on gfx here, eg, your draw-boid would go here

  (.drawImage gfx2 image 0 0 window))) ;finally draws the buffered
image to the screen all at once

--
Ryan Sattler

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: java gui is flashing

2010-07-25 Thread Ryan Twitchell
HI,

You'd be best served overriding the JPanel's paint or paintComponent
method (I'm fuzzy on the difference), and then calling repaint
periodically (at your desired frame rate).  IIRC, repaint is safe to
call from any thread, and it will cause the paint methods to be called
in the swing thread.  Also, try to ensure your swing setup code runs
in the swing thread if possible (see Brian's reply).

See also :
Background on Java 2D: http://java.sun.com/docs/books/tutorial/2d/TOC.html
contrib has a do-swing macro for evaling in the swing thread:
http://richhickey.github.com/clojure-contrib/swing-utils-api.html

Ryan


On Jul 24, 9:11 am, Mate Toth totm...@gmail.com wrote:
 Hi,

 my problem is that during execution my presentation java applet is
 flashing. There are many components which paint to a JPanel using it's
 Graphics (.getGraphics). I think maybe the problem is that I don't
 have any paint everything now routine..(I don't know which Java
 routine to use, how I have to synchronize it etc..) Thanks for your
 time and help.

 I've the following code for the gui part (fe stands for flocking-
 enviroment, I'm making a flocking library):

 The main loop:

 (defn main [starting-fe]
   (let [first-fe starting-fe
         panel (new JPanel)
         frame (new JFrame)]

     ;; configuring panel
     (. panel setPreferredSize (new Dimension width height))

     ;; configuring frame
     (. frame add panel)
     (. frame pack)
     (. frame setVisible true)

     (loop [fe first-fe]
       (draw-fe fe panel)
       (Thread/sleep 100)
       (recur ((:update-f fe) fe)

 Blanking the screen:

 (defn blank-panel [panel]
     (let [g (. panel getGraphics)]
       (. g setColor (. Color BLACK))
       (. g fillRect 0 0 width height

 At the end every boid drawed by this(called by the draw-fe function):

 (defn draw-boid [boid panel]
     (let [[x0 x1] (:pos boid)]
       (let [g (. panel getGraphics)]
         (. g setColor (:color boid))
         (. g fillRect (- x0 2) (- x1 2) 5 5)

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Clojure finally on SPOJ!

2010-07-25 Thread Cachou
Even the TEST Problem will TLE!!!

My code is here:

(ns spoj-test)

(defn read-int
  []
  (let [s (read-line)]
(Integer/parseInt s)))

(defn main
  [] (let [n (read-int)]
   (when (not (== 42 n))
 (println n)
 (recur


(main)

the sample input is OK.

On Jul 25, 3:51 pm, sphere research sphere.resea...@gmail.com wrote:
 Hi,

 now, you can solve problems/submit problems in Clojure on SPOJ,

 good luck,

 regards,
 SPOJ Team

 ps.

 We are very happy to announce that users' accounts have finally
 appeared on Ideone :)
 If you liked Ideone as it has been so far, you will like the new one
 even more. The most important new features available for Ideone users
 are:

     * management panel where users can view and group their pastes,
     * personalized access to Ideone API
     * and public folders which allow to publish and share a group of
 pastes at one go.

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: java gui is flashing

2010-07-25 Thread Isak Hansen
On Sat, Jul 24, 2010 at 3:11 PM, Mate Toth totm...@gmail.com wrote:
 Hi,

 my problem is that during execution my presentation java applet is
 flashing. There are many components which paint to a JPanel using it's
 Graphics (.getGraphics). I think maybe the problem is that I don't
 have any paint everything now routine..(I don't know which Java
 routine to use, how I have to synchronize it etc..) Thanks for your
 time and help.


Google for double buffering.


Isak




 I've the following code for the gui part (fe stands for flocking-
 enviroment, I'm making a flocking library):

 The main loop:

 (defn main [starting-fe]
  (let [first-fe starting-fe
        panel (new JPanel)
        frame (new JFrame)]

    ;; configuring panel
    (. panel setPreferredSize (new Dimension width height))

    ;; configuring frame
    (. frame add panel)
    (. frame pack)
    (. frame setVisible true)

    (loop [fe first-fe]
      (draw-fe fe panel)
      (Thread/sleep 100)
      (recur ((:update-f fe) fe)


 Blanking the screen:

 (defn blank-panel [panel]
    (let [g (. panel getGraphics)]
      (. g setColor (. Color BLACK))
      (. g fillRect 0 0 width height

 At the end every boid drawed by this(called by the draw-fe function):

 (defn draw-boid [boid panel]
    (let [[x0 x1] (:pos boid)]
      (let [g (. panel getGraphics)]
        (. g setColor (:color boid))
        (. g fillRect (- x0 2) (- x1 2) 5 5)

 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with your 
 first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: java gui is flashing

2010-07-25 Thread Nurullah Akkaya
On Sun, Jul 25, 2010 at 12:55 PM, Ryan Twitchell metatheo...@gmail.com wrote:
 You'd be best served overriding the JPanel's paint or paintComponent
 method (I'm fuzzy on the difference), and then calling repaint
 periodically (at your desired frame rate).  IIRC, repaint is safe to
 call from any thread, and it will cause the paint methods to be called
 in the swing thread.  Also, try to ensure your swing setup code runs
 in the swing thread if possible (see Brian's reply).


Unless you have a good reason, you should not override paint,

http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/painting/closer.html

--
Nurullah Akkaya
http://nakkaya.com

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Efficiency of reduce function for lists.

2010-07-25 Thread Randy Hudson
This is a wholly appropriate use of reduce; it's the obvious function
to use when you want to accumulate some calculation over a sequence.


As you say, you can just produce a function that acts on the result,
something like

(defn fstep [c] (if (= c \) pop #(conj % c))

However, the obvious way of accumulating the application of these
functions is ... reduce:

(reduce #(%2 %1) [] (map fstep input-chars))


You can gain some efficiency by using transient operations:

(defn tstep [v c] (if (= c \) (pop! v) (conj! v c)))   ;cf. step

(defn run [cs] (persistent! (reduce tstep (transient []) cs))  ; cf.
run-program

On my machine this reduced the runtime of your 6e6 test about 20%

On Jul 24, 6:16 pm, samnardoni samnard...@googlemail.com wrote:
 I have a simple string (or list of characters to be precise) in a form
 of: 123456789.

 I want to parse this string and end up with: 123569.

 The  is essentially the same as a backspace.

 I managed to implement this fairly simply using the reduce function -
 source:http://gist.github.com/489019.

 Although it can be implemented easily using the reduce function, I was
 concerned with the performance (especially with large inputs). As the
 reduce function invokes the provided function with the previous
 result, this list that is getting built is being passed around a huge
 amount.

 I'm not too sure as to what will be going on under the hood in this
 sample. Whether the previous result of the function application is
 really being passed around or not clear to me.

 For the program, I know that when processing a character, I do not
 need the previous result. All I need is the current character, and I
 can return a function that acts on the result. I'm not sure if this is
 simple to implement in a functional way.

 So, I have 2 questions:

 1) Is this a correct use of the reduce function? (I know there is a
 clue in the word reduce, but it's also called inject and else in
 other languages.)

 2) Is there a more efficient way of solving this?

 I appreciate any replies (for such a trivial topic). Answers to this
 will hopefully enlighten me to using functional languages more
 effectively, or possibly to a more suitable high-order 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
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: java gui is flashing

2010-07-25 Thread Mate Toth
Thx guys!
I used double-buffering like in Ryan's code and it worked like a
charm!

M

On Jul 25, 4:01 am, Ryan Sattler xgravi...@gmail.com wrote:
 I've been working on a game with Clojure/Swing lately and the simplest
 way to avoid flashing is to draw to a bufferedImage first, then draw
 that bufferedImage all at once. This means that parts of the screen
 that don't change won't be briefly overwritten by the background
 color, avoiding flashing. Here's some code:

 (defn render [game window]
   (let [#^BufferedImage image (.createImage window window-width window-
 height)
         #^Graphics gfx (.createGraphics image)
         #^Graphics2D gfx2 (.getGraphics #^JFrame window)]

       (render-background gfx)
       ;draw more stuff on gfx here, eg, your draw-boid would go here

       (.drawImage gfx2 image 0 0 window))) ;finally draws the buffered
 image to the screen all at once

 --
 Ryan Sattler

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Efficiency of reduce function for lists.

2010-07-25 Thread samnardoni
Randy, thanks for the reply. This has certainly cleared things up for
me.

I was only concerned about performance for an order of magnitude or
more; just checking I was on the right track, so to speak.

It seems I'll have to do a little research on transients now.

On Jul 25, 4:23 pm, Randy Hudson randy_hud...@mac.com wrote:
 This is a wholly appropriate use of reduce; it's the obvious function
 to use when you want to accumulate some calculation over a sequence.

 As you say, you can just produce a function that acts on the result,
 something like

 (defn fstep [c] (if (= c \) pop #(conj % c))

 However, the obvious way of accumulating the application of these
 functions is ... reduce:

 (reduce #(%2 %1) [] (map fstep input-chars))

 You can gain some efficiency by using transient operations:

 (defn tstep [v c] (if (= c \) (pop! v) (conj! v c)))   ;cf. step

 (defn run [cs] (persistent! (reduce tstep (transient []) cs))  ; cf.
 run-program

 On my machine this reduced the runtime of your 6e6 test about 20%

 On Jul 24, 6:16 pm, samnardoni samnard...@googlemail.com wrote:



  I have a simple string (or list of characters to be precise) in a form
  of: 123456789.

  I want to parse this string and end up with: 123569.

  The  is essentially the same as a backspace.

  I managed to implement this fairly simply using the reduce function -
  source:http://gist.github.com/489019.

  Although it can be implemented easily using the reduce function, I was
  concerned with the performance (especially with large inputs). As the
  reduce function invokes the provided function with the previous
  result, this list that is getting built is being passed around a huge
  amount.

  I'm not too sure as to what will be going on under the hood in this
  sample. Whether the previous result of the function application is
  really being passed around or not clear to me.

  For the program, I know that when processing a character, I do not
  need the previous result. All I need is the current character, and I
  can return a function that acts on the result. I'm not sure if this is
  simple to implement in a functional way.

  So, I have 2 questions:

  1) Is this a correct use of the reduce function? (I know there is a
  clue in the word reduce, but it's also called inject and else in
  other languages.)

  2) Is there a more efficient way of solving this?

  I appreciate any replies (for such a trivial topic). Answers to this
  will hopefully enlighten me to using functional languages more
  effectively, or possibly to a more suitable high-order 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
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: ANN: clj-dropbox, Dropbox client library in Clojure

2010-07-25 Thread aria42
Hmmm, can you privately send me some more details about what you did?
Did you go to the authorization
site and get an ok from dropbox before calling the request-callback;
that error typically means the user didn't
authorize your app? I know its a straightforward
question, but just checking. Dropbox knows the oauth dance is a little
brittle and they have a bypass
I'll include in the next update where you ship off user email and
password and they return the user token. It
completely defeats the point of OAuth, but they know the process is
buggy enough to include.

Thanks, Aria

On Jul 24, 3:20 pm, Mikael Sundberg mikael.sundber...@gmail.com
wrote:
 Looks realy nice
 i Tried it, and i cant get the example oath dance to work. i get:
 Bad Response: 403
 {error: Token is not an authorized request token.}
   [Thrown class java.lang.RuntimeException]

 i used  or nil as a callback-url. i assume thats where the problem is.

 if i do the dance by hand and copy the result the client works great!

 /Micke

 2010/7/23 Marc Spitzer mspit...@gmail.com:



  I need to look at this.  Thanks for the toy.

  marc

  On Thu, Jul 22, 2010 at 8:23 PM, aria42 ari...@gmail.com wrote:
  Hi all,

  I've released my clojure library for accessing the Dropbox API. Its
  called clj-dropbox and it can be found at:

 http://github.com/aria42/clj-dropbox

  Hope some people get use out of it. Feel free to email or leave
  comments and/or suggestions

  Best,
  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
  Note that posts from new members are moderated - please be patient with 
  your first post.
  To unsubscribe from this group, send email to
  clojure+unsubscr...@googlegroups.com
  For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en

  --
  Freedom is nothing but a chance to be better.
  --Albert Camus

   The problem with socialism is that eventually you run out
  of other people's money.
  --Margaret Thatcher

  --
  You received this message because you are subscribed to the Google
  Groups Clojure group.
  To post to this group, send email to clojure@googlegroups.com
  Note that posts from new members are moderated - please be patient with 
  your first post.
  To unsubscribe from this group, send email to
  clojure+unsubscr...@googlegroups.com
  For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: ANN: clj-dropbox, Dropbox client library in Clojure

2010-07-25 Thread aria42
How are you accessing the authorization url? If by hand you mean you
manually go to the url, this is what you're supposed to do. I don't
think
you can visit the authorization url via a browser agent unless the
agent is logged in to dropbox antecedently and you change the browser
agent
so it looks like a normal browser.

On Jul 24, 3:20 pm, Mikael Sundberg mikael.sundber...@gmail.com
wrote:
 Looks realy nice
 i Tried it, and i cant get the example oath dance to work. i get:
 Bad Response: 403
 {error: Token is not an authorized request token.}
   [Thrown class java.lang.RuntimeException]

 i used  or nil as a callback-url. i assume thats where the problem is.

 if i do the dance by hand and copy the result the client works great!

 /Micke

 2010/7/23 Marc Spitzer mspit...@gmail.com:



  I need to look at this.  Thanks for the toy.

  marc

  On Thu, Jul 22, 2010 at 8:23 PM, aria42 ari...@gmail.com wrote:
  Hi all,

  I've released my clojure library for accessing the Dropbox API. Its
  called clj-dropbox and it can be found at:

 http://github.com/aria42/clj-dropbox

  Hope some people get use out of it. Feel free to email or leave
  comments and/or suggestions

  Best,
  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
  Note that posts from new members are moderated - please be patient with 
  your first post.
  To unsubscribe from this group, send email to
  clojure+unsubscr...@googlegroups.com
  For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en

  --
  Freedom is nothing but a chance to be better.
  --Albert Camus

   The problem with socialism is that eventually you run out
  of other people's money.
  --Margaret Thatcher

  --
  You received this message because you are subscribed to the Google
  Groups Clojure group.
  To post to this group, send email to clojure@googlegroups.com
  Note that posts from new members are moderated - please be patient with 
  your first post.
  To unsubscribe from this group, send email to
  clojure+unsubscr...@googlegroups.com
  For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Remove-first function

2010-07-25 Thread Mark Engelberg
On Sat, Jul 24, 2010 at 9:07 AM, Gary Fredericks
fredericksg...@gmail.com wrote:
 (defn remove-first
   [syb lst]
   (let [[before after]
   (loop [b [] a lst]
     (if (empty? lst)
   [b a]
   (if (= syb (first a))
     [b (rest a)]
     (recur (cons (first a) b) (rest a)]
    (concat (reverse before) after)))

 user= (remove-first 4 '(1 5 3 4 2 6 674 4 2))
 (1 5 3 2 6 674 4 2)

 I'm interested if somebody comes up with something more efficient, i.e. that
 doesn't require the reversal.

If you change (cons (first a) b) to (conj b (first a), then no
reversal will be required.

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Small problem in CCW IDE

2010-07-25 Thread Arie van Wingerden
Hi,

when I Run as Clojure Repl the next little source file:

   (ns test.readln)
   (defn -main []
   (println Enter some text for the first time:)
   (println (read-line))
   (println Enter some text for the second time:)
   (println (read-line)))
   (-main)

I get the following output:
   Clojure 1.2.0-master-SNAPSHOT
   Enter some text for the first time:
   (in-ns 'test.readln)
   Enter some text for the second time:

So, it appears the (read-line) consumes the Clojure form (in-ns
'test.readln)
probably stacked by CCW (at least I didn't type that!)

Any ideas?

Arie

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Efficiency of reduce function for lists.

2010-07-25 Thread B Smith-Mannschott
On Sun, Jul 25, 2010 at 00:16, samnardoni samnard...@googlemail.com wrote:
 I have a simple string (or list of characters to be precise) in a form
 of: 123456789.

 I want to parse this string and end up with: 123569.

 The  is essentially the same as a backspace.

 I managed to implement this fairly simply using the reduce function -
 source: http://gist.github.com/489019.


Yes, that's a good use of reduce. If you really need performance,
however, you might consider dropping down more imperative in this
case. I was able to cut the runtime to about 10% by reformulating the
problem in terms of String and StringBuilder.  The necessary code,
though, is really ugly:


(ns backspace)

(def data-with-deletions
    (apply str
           (repeat 1
                   (str 12345789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456

00

(def data-without-deletions
    (apply str (repeat 100 0)))

(defn run-program [program]
 (letfn [(step [stack char]
           (if (= char \)
             (pop stack)
             (conj stack char)))]
   (reduce step [] program)))

(defn time-run-program [data]
 (count (time (run-program data

(defn run-program2 [^String program]
 (loop [i 0, j (.indexOf program ), b (StringBuilder.)]
   (if (= -1 j)
     (.toString (.append b (.substring program i)))
     (let [k (int (loop [k (inc j)]
                    (if (= (.charAt program k) \)
                      (recur (inc k))
                      k)))]
       (recur (int k)
              (.indexOf program  k)
              (.append b (.substring program i (- j (- k j)

(defn time-run-program2 [data]
 (count (time (run-program2 data

(defn timings []
 (println run-program on data without deletions)
 (dorun (repeatedly 10 #(time-run-program data-without-deletions)))
 (println run-program on data with deletions)
 (dorun (repeatedly 10 #(time-run-program data-with-deletions)))
 (println run-program2 on data without deletions)
 (dorun (repeatedly 10 #(time-run-program2 data-without-deletions)))
 (println run-program2 on data with deletions)
 (dorun (repeatedly 10 #(time-run-program2 data-with-deletions

;; ---
;; On a 1.6Ghz Atom (Dell Mini 9)
;; ---
;; run-program on data without deletions
;; Elapsed time: 1658.232133 msecs
;; Elapsed time: 883.943424 msecs
;; Elapsed time: 865.990504 msecs
;; Elapsed time: 840.566116 msecs
;; Elapsed time: 816.676015 msecs
;; Elapsed time: 815.181318 msecs
;; Elapsed time: 808.408751 msecs
;; Elapsed time: 811.207018 msecs
;; Elapsed time: 828.006483 msecs
;; Elapsed time: 805.052535 msecs
;; run-program on data with deletions
;; Elapsed time: 805.520812 msecs
;; Elapsed time: 763.458827 msecs
;; Elapsed time: 786.309237 msecs
;; Elapsed time: 784.190927 msecs
;; Elapsed time: 779.576116 msecs
;; Elapsed time: 776.683829 msecs
;; Elapsed time: 788.427932 msecs
;; Elapsed time: 766.683824 msecs
;; Elapsed time: 762.674231 msecs
;; Elapsed time: 759.6923 msecs
;; run-program2 on data without deletions
;; Elapsed time: 16.040587 msecs
;; Elapsed time: 17.901927 msecs
;; Elapsed time: 30.643421 msecs
;; Elapsed time: 32.301243 msecs
;; Elapsed time: 19.36524 msecs
;; Elapsed time: 16.757645 msecs
;; Elapsed time: 15.305436 msecs
;; Elapsed time: 23.4835 msecs
;; Elapsed time: 30.554508 msecs
;; Elapsed time: 25.414402 msecs
;; run-program2 on data with deletions
;; Elapsed time: 324.703781 msecs
;; Elapsed time: 73.732552 msecs
;; Elapsed time: 95.258957 msecs
;; Elapsed time: 92.265781 msecs
;; Elapsed time: 69.199159 msecs
;; Elapsed time: 77.407051 msecs
;; Elapsed time: 62.621647 msecs
;; Elapsed time: 78.062991 msecs
;; Elapsed time: 68.359239 msecs
;; Elapsed time: 58.825144 msecs

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Improvents on agent,user-custom thread pool.

2010-07-25 Thread Alex Miller
Hey Dennis,

I suggested some of the same ideas here (http://tech.puredanger.com/
2010/06/08/clojure-agent-thread-pools/) and Rich said that these
seemed like good suggestions post-1.2.  I think allowing you to modify
the agent thread pools after construction seems possibly dangerous
from a concurrency point of view, but maybe that would be ok.

Alex


On Jul 25, 3:47 am, dennis killme2...@gmail.com wrote:
 Agent use two thread pools to execute actions,send use a fixed thread
 pool (2+cpus threads),and send-off use a cached thread pool.These
 pools are global in clojure system.

     I think the Agent should allow users to customize the thread pool,
 if no custom,  then use the global thread pool.
    Why do I need a custom thread pool?
    First, the default thread pool is global, send use the thread pool
 is a fixed size cpus +2, is likely to become the system bottleneck
 sometime. Although you can use the send-off, use the cache thread
 pool, but in a real world application, I can not use the cache thread
 pool, which will introduce the risk of OutOfMemoryError, normally I
 like to use a fixed-size thread pool.

   Second, the actions which global thread pool execute are from a
 variety of agents, the actions are not homogeneous, and can not
 maximize the efficient use of the thread pool, we hope that you can
 specify different agent to isolate a particular thread pool to
 maximize the use of thread pool .

    I think Agent could add two new functions:

 (set-executor! agent (java.util.concurrent.Executors/
 newFixedThreadPool 2))
 (shutdown-agent agent)

 set-executor! is to set the agent's custom thread pool,and shutdown-
 agent to shutdown the agent's custom thread pool.

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Why can not alter or ref-set ref after commute it?

2010-07-25 Thread dennis
Thanks for your reply,Ulrich

I knew that comute would rerun before the commit,but my problem is
that if we allow ref-set ref after commuting,it seems there is no bad
thing would happen.What's the purpose of this limitation except alter
or ref-set have no lasting result?

On Jul 25, 7:01 pm, Moritz Ulrich ulrich.mor...@googlemail.com
wrote:
 Read the documentation of commute 
 carefully:http://richhickey.github.com/clojure/clojure.core-api.html#clojure.co...

 commute acts at the end of the current dosync-block, regardless of
 when commute was applied inside it. That's the reason why you can't
 ref-set it after a commute; the commute isn't done.



 On Sun, Jul 25, 2010 at 11:19 AM, dennis killme2...@gmail.com wrote:
  Alter or ref-set a ref after commute would throw a
  IllegalStateException:Can't set after commute

  for example:

  user= (def counter (ref 0))
  #'user/counter
  (dosync (commute counter inc) (ref-set counter 3))
  java.lang.IllegalStateException: Can't set after commute
  (NO_SOURCE_FILE:0)

  I want to know why this should not happen?is it a explanation here? I
  can't understand what is the difference with  commuting ref  after ref-
  set or alter.Thanks a lot.

  --
  You received this message because you are subscribed to the Google
  Groups Clojure group.
  To post to this group, send email to clojure@googlegroups.com
  Note that posts from new members are moderated - please be patient with 
  your first post.
  To unsubscribe from this group, send email to
  clojure+unsubscr...@googlegroups.com
  For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en

 --
 Moritz Ulrich
 Programmer, Student, Almost normal Guy

 http://www.google.com/profiles/ulrich.moritz

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en