Re: concise description of SLIME indentation rules?
On Fri, Mar 18, 2011 at 4:05 AM, Laurent PETIT laurent.pe...@gmail.com wrote: Sure, this is the target solution. More complex than what I've done, though. And the gathering of the metadata is not easy, also. Any Clojure IDE that can open a REPL should be able to get at the metadata. Just eagerly spawn a runtime with the project classpaths and everything, able to spawn REPL servers on command via a kind of meta-REPL-server. Use a hidden one to send metadata requests and for other purposes, and if the user opens a REPL, spawn another. The only real issue then arises if the user causes a System/exit (or a VM crash) or monkeys with threads in a manner that kills stuff. If the indent (and anything else that queries the hidden REPL under the hood) loses the connection just generate a tray alert and respawn the entire subordinate JVM. The user will find that doing those things reboots their REPL. (You may want to detect the case of deliberate System/exit and not automatically restart the user REPL in that case, only the under-the-hood one. That should be easy, as the child process will have actually exited and probably with errorlevel 0 in that case.) If we go the static analysis road, then some deps may not be distributed as source code and we may not be able to get the info. True. If we go the dynamic road, then the smart identation may behave differently depending on whether a REPL for the project is loaded or not, and if it is loaded, which parts of the application are in memory. Ah, you're worried about resolve? Just use (ns-resolve the-namespace symbol), then, with the-namespace derived from the file's path and name, e.g. if it's com/my_domain/my_project/core.clj the namespace is com.my-domain.my-project.core. -- 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: concise description of SLIME indentation rules?
2011/3/18 Ken Wesson kwess...@gmail.com On Fri, Mar 18, 2011 at 4:05 AM, Laurent PETIT laurent.pe...@gmail.com wrote: Sure, this is the target solution. More complex than what I've done, though. And the gathering of the metadata is not easy, also. Any Clojure IDE that can open a REPL should be able to get at the metadata. Just eagerly spawn a runtime with the project classpaths and everything, able to spawn REPL servers on command via a kind of meta-REPL-server. Use a hidden one to send metadata requests and for other purposes, and if the user opens a REPL, spawn another. The only real issue then arises if the user causes a System/exit (or a VM crash) or monkeys with threads in a manner that kills stuff. If the indent (and anything else that queries the hidden REPL under the hood) loses the connection just generate a tray alert and respawn the entire subordinate JVM. The user will find that doing those things reboots their REPL. (You may want to detect the case of deliberate System/exit and not automatically restart the user REPL in that case, only the under-the-hood one. That should be easy, as the child process will have actually exited and probably with errorlevel 0 in that case.) All you describe is good, and is options already envisioned. Nothing new under the sun, but nicely summarized. Tho sharing the same JVM between the user and ccw might not be really interesting in case of remote connections, specific ways of starting it, etc. If we go the static analysis road, then some deps may not be distributed as source code and we may not be able to get the info. True. If we go the dynamic road, then the smart identation may behave differently depending on whether a REPL for the project is loaded or not, and if it is loaded, which parts of the application are in memory. Ah, you're worried about resolve? Not at all. ns-resolve will not discover vars if the namespace hasn't been required, used or loaded first. Just use (ns-resolve the-namespace symbol), then, with the-namespace derived from the file's path and name, e.g. if it's com/my_domain/my_project/core.clj the namespace is com.my-domain.my-project.core. -- 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: concise description of SLIME indentation rules?
On Fri, Mar 18, 2011 at 7:24 AM, Laurent PETIT laurent.pe...@gmail.com wrote: Ah, you're worried about resolve? Not at all. ns-resolve will not discover vars if the namespace hasn't been required, used or loaded first. It's the user's own damn fault if some things won't indent the way they should because the source file contains a bug. It won't indent properly if they've mismatched a delimiter somewhere, either. -- 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: xml parsing
Thanks Alan for the reminder, I had totally forgotten that, and was culprit of thinking that the only way of doing depth-first was postorder. My apologizes, Stu. Cheers, -- Laurent 2011/3/16 Alan a...@malloys.org It's one variety of depth-first. Pre-order, post-order, and in-order are all viable ways of doing depth-first searches (though in-order makes less sense for non-binary trees). Assume for the rest of this post the following tree: 1 --2 --3 4 --5 6 --7 - Breadth-first traversal: 1237465 - Depth-first, preorder traversal: 1234567 - Depth-first, postorder traversal: 2546371 I think it's possible to split breadth-first into pre- and post-order as well, but it's been a long time since I took that class and trying to do it is making my head hurt. I think the important point is that there's more than one way to do depth-first traversal, and preorder looks like what Stuart is talking about. On Mar 16, 2:50 pm, Laurent PETIT laurent.pe...@gmail.com wrote: 2011/3/16 Stuart Sierra the.stuart.sie...@gmail.com I think xml-seq is returning a sequence representing a depth-first traversal of the XML document. So the first item in the sequence is the entire document, followed by the first element under the root, and so on. That's not the definition of depth-first, is it ? -Stuart Sierra clojure.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 -- 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: New Release of the Clojure Debugging Toolkit
my apologies George - upon closer inspection, I don't have a tools.jar file - must have been in a daze before...I updated openjdk-6-jdk and it's update included the tools.jar file (not sure why it wasn't there to begin with though)...now I'm happily humming along with CDTthanks for all your hard work on this! -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Can't recur from within a catch expression.
Hello Clojure users, This is a dumb question but I'd like to write something equivalent to this in Clojure: public String loop_with_exception(int retries) { for (int n = retries; n 0; n--) { try { return some_io_operation(); } catch (IOException e) { continue; } } return null; } So I tried writing it like this: (ns sandbox.core (:import [java.io.IOException])) (defn some-io-operation Some read I/O operation that could throw an IOException. [] (println WOULD do a read operation)) (defn loop-with-exception [retries] (loop [n retries] (try (when (pos? n) (some-io-operation)) (catch IOException e (recur (dec n)) But I get this error: cd c:/EMACSHOME/CLOJURE-PROJECTS/sandbox/src/sandbox/ 1 compiler notes: Unknown location: error: java.lang.UnsupportedOperationException: Cannot recur from catch/finally core.clj:13:9: error: java.lang.UnsupportedOperationException: Cannot recur from catch/finally (core.clj:13) Compilation failed. What's the recommended way to handle exceptions and retry inside a loop/recur? Thank you. -Fiel Cab.ral -- 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
ClojureCLR startup times...
Hey All! Just downloaded the CLR version of clojure and are experiencing a pretty awkward boot up time of the ClojureMain.exe (~10 sec) I've tried to ngen the exe and it's dependencies but without any luck. Compiling and Running from within VS2010 it takes approximately the same... Any ideas? Cheers, Kristoffer -- 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
almost abusing the system... defmutabletype
Hello everybody, I wanted to define a bunch of types which were mutable to be used in defining a cyclic data structure. So, I wrote the following macro .. . (defmacro defmutabletype [type-name members] (let [proto-name (symbol (str I (name type-name))) member-setter-names (map #(symbol (str (name %) !)) members) member-setter-prototypes (map (fn [setter-name] `(~setter-name [this# newval#])) member-setter-names) member-setter-fns (map (fn [setter-name member-name] `(~setter-name [this# newval#] (set! ~member-name newval#))) member-setter-names members) member-getter-prototypes (map (fn [member-name] `(~member-name [this#])) members) member-getter-fns (map (fn [member-name] `(~member-name [this#] ~member-name)) members) annotated-members (vec (map (fn [name] (with-meta name (assoc (meta name) :volatile-mutable true))) members))] `(do (defprotocol ~proto-name ~@member-getter-prototypes ~@member-setter-prototypes) (deftype ~type-name ~annotated-members ~proto-name ~@member-getter-fns ~@member-setter-fns (defmutabletype point [x y]) is equivalent to (do (defprotocol Ipoint (x [this]) (x! [this v]) (y [this]) (y! [this v]) (deftype point [^{:volatile-mutable true} x ^{:volatile-mutable true} y] Ipoint (x [this] x) (x! [this v] (set! x v)) (y [this] y) (y! [this v] (set! y v although not idiomatic clojure .. thought some of you may find it useful. Sunil. -- 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: almost abusing the system... defmutabletype
https://gist.github.com/876029 On Fri, Mar 18, 2011 at 6:33 PM, Sunil S Nandihalli sunil.nandiha...@gmail.com wrote: Hello everybody, I wanted to define a bunch of types which were mutable to be used in defining a cyclic data structure. So, I wrote the following macro .. . (defmacro defmutabletype [type-name members] (let [proto-name (symbol (str I (name type-name))) member-setter-names (map #(symbol (str (name %) !)) members) member-setter-prototypes (map (fn [setter-name] `(~setter-name [this# newval#])) member-setter-names) member-setter-fns (map (fn [setter-name member-name] `(~setter-name [this# newval#] (set! ~member-name newval#))) member-setter-names members) member-getter-prototypes (map (fn [member-name] `(~member-name [this#])) members) member-getter-fns (map (fn [member-name] `(~member-name [this#] ~member-name)) members) annotated-members (vec (map (fn [name] (with-meta name (assoc (meta name) :volatile-mutable true))) members))] `(do (defprotocol ~proto-name ~@member-getter-prototypes ~@member-setter-prototypes) (deftype ~type-name ~annotated-members ~proto-name ~@member-getter-fns ~@member-setter-fns (defmutabletype point [x y]) is equivalent to (do (defprotocol Ipoint (x [this]) (x! [this v]) (y [this]) (y! [this v]) (deftype point [^{:volatile-mutable true} x ^{:volatile-mutable true} y] Ipoint (x [this] x) (x! [this v] (set! x v)) (y [this] y) (y! [this v] (set! y v although not idiomatic clojure .. thought some of you may find it useful. Sunil. -- 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: ClojureCLR startup times...
Just downloaded the CLR version of clojure and are experiencing a pretty awkward boot up time of the ClojureMain.exe (~10 sec) I've tried to ngen the exe and it's dependencies but without any luck. Compiling and Running from within VS2010 it takes approximately the same... Sadly, I think this is normal. Clojure on the JVM starts up in about 4-5 sec on my machine. I've found that Clojure-CLR is much slower. As an example here's what's happening: 1) the Clojure-CLR assembly loads 2) Clojure-CLR is run through some initial JIT compilation 3) Clojure-CLR loads core.clj which is about 200KB of Clojure code 4) core.clj is translated to CLR classes, functions, etc. 5) core.clj is run through the JIT 6) other supporting .clj files are read, translated, and run through the JIT 7) now you are at the REPL I think the only real way to improve these startup times is to do what Python does, and have cached assemblies so that core.clj doesn't have to be re-compiled each and every time. But as far as I know no one is even looking into this. Timothy -- 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: almost abusing the system... defmutabletype
On Fri Mar 18 18:33 2011, Sunil S Nandihalli wrote: I wanted to define a bunch of types which were mutable to be used in defining a cyclic data structure. So, I wrote the following macro... … code sample … although not idiomatic clojure .. thought some of you may find it useful. Sunil. Is there some reason you chose to use mutable members instead of one of Clojure's concurrency primitives? From your example, it doesn't seem like interop is the reason, given that your mutators will be called something similar to ‘y__BANG__’. Also, given that you are defining both a protocol and a type where the protocol doesn't seem like it really serves as an abstraction, perhaps you would be better served with defrecord or just a plain old map/struct. Here's an example using maps: (defn new-node [] {:prev (atom nil) :next (atom nil)}) (defn attach-next! [node next-node] (reset! (:next node) next-node)) (defn next-node [node] @(:next node)) This approach allows you to do some things you can't do with volatile mutables, such as an atomic compare and set operation: (defn attach-next! [node next-node] (compare-and-set! (:next node) nil next-node)) This function will only attach a next node if there is no next node already. Sincerely, Daniel Solano Gómez pgpkZ2DiLxE8A.pgp Description: PGP signature
Re: almost abusing the system... defmutabletype
Hi Gomez, Thanks for your response. The goal was to implement the half_edge datastructure .. it is described herehttp://www.flipcode.com/archives/The_Half-Edge_Data_Structure.shtml . I felt using atoms was not going to serve any extra purpose .. What would you be doing in this situation... So googling actually led me to a previous post http://www.mail-archive.com/clojure@googlegroups.com/msg25256.htmlwhich discussed about the implementation of half-edge datastructure in clojure .. It gave a reference to thishttp://www.cs.ucl.ac.uk/teaching/3C11/graph.pdf publication.. But I just thought it might just be simpler to do a mutable thing while still taking advantage of clojures stuff outside of this data-structure. Sunil. On Fri, Mar 18, 2011 at 7:08 PM, Daniel Solano Gomez cloj...@sattvik.comwrote: On Fri Mar 18 18:33 2011, Sunil S Nandihalli wrote: I wanted to define a bunch of types which were mutable to be used in defining a cyclic data structure. So, I wrote the following macro... … code sample … although not idiomatic clojure .. thought some of you may find it useful. Sunil. Is there some reason you chose to use mutable members instead of one of Clojure's concurrency primitives? From your example, it doesn't seem like interop is the reason, given that your mutators will be called something similar to ‘y__BANG__’. Also, given that you are defining both a protocol and a type where the protocol doesn't seem like it really serves as an abstraction, perhaps you would be better served with defrecord or just a plain old map/struct. Here's an example using maps: (defn new-node [] {:prev (atom nil) :next (atom nil)}) (defn attach-next! [node next-node] (reset! (:next node) next-node)) (defn next-node [node] @(:next node)) This approach allows you to do some things you can't do with volatile mutables, such as an atomic compare and set operation: (defn attach-next! [node next-node] (compare-and-set! (:next node) nil next-node)) This function will only attach a next node if there is no next node already. Sincerely, Daniel Solano Gómez -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Can't recur from within a catch expression.
Notice it says can't recur from within a catch. So throw the try/catch into it's own function and return nil if the try fails. Then throw a if statement to continue the loop only if the function return nil. Timothy On Thu, Mar 17, 2011 at 8:49 PM, Fiel Cabral e4696wyoa63emq6w3250kiw60i4...@gmail.com wrote: Hello Clojure users, This is a dumb question but I'd like to write something equivalent to this in Clojure: public String loop_with_exception(int retries) { for (int n = retries; n 0; n--) { try { return some_io_operation(); } catch (IOException e) { continue; } } return null; } So I tried writing it like this: (ns sandbox.core (:import [java.io.IOException])) (defn some-io-operation Some read I/O operation that could throw an IOException. [] (println WOULD do a read operation)) (defn loop-with-exception [retries] (loop [n retries] (try (when (pos? n) (some-io-operation)) (catch IOException e (recur (dec n)) But I get this error: cd c:/EMACSHOME/CLOJURE-PROJECTS/sandbox/src/sandbox/ 1 compiler notes: Unknown location: error: java.lang.UnsupportedOperationException: Cannot recur from catch/finally core.clj:13:9: error: java.lang.UnsupportedOperationException: Cannot recur from catch/finally (core.clj:13) Compilation failed. What's the recommended way to handle exceptions and retry inside a loop/recur? Thank you. -Fiel Cab.ral -- 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 -- “One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.” (Robert Firth) -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Can't recur from within a catch expression.
On Thu Mar 17 21:49 2011, Fiel Cabral wrote: Hello Clojure users, This is a dumb question but I'd like to write something equivalent to this in Clojure: public String loop_with_exception(int retries) { for (int n = retries; n 0; n--) { try { return some_io_operation(); } catch (IOException e) { continue; } } return null; } So I tried writing it like this: (ns sandbox.core (:import [java.io.IOException])) (defn some-io-operation Some read I/O operation that could throw an IOException. [] (println WOULD do a read operation)) (defn loop-with-exception [retries] (loop [n retries] (try (when (pos? n) (some-io-operation)) (catch IOException e (recur (dec n)) What's the recommended way to handle exceptions and retry inside a loop/recur? Well, I'm not sure what's the best recommendation, but playing around with it a bit, it seems like moving the exception-handling into the IO method works best. Doing this, you can have different return values depending on whether the operation succeeded. Here's what I came up with: (defn some-io-operation Some read I/O operation that may fail. Returns nil on failure, ::success on success. [] (try (when (zero? (rand-int 3)) (throw (IOException.))) ; return non-nil and non-false on success ::success (catch IOException e ; return nil on failure nil))) (defn loop-with-exception [retries] (first (remove nil? (repeatedly retries some-io-operation I am not absolutely certain the loop-with-exception function is the best implementation, but I believe it works correctly. Sincerely, Daniel Solano Gómez pgpzctxncuNQV.pgp Description: PGP signature
Re: Can't recur from within a catch expression.
On Fri, Mar 18, 2011 at 7:19 AM, Fiel Cabral e4696wyoa63emq6w3250kiw60i4...@gmail.com wrote: public String loop_with_exception(int retries) { for (int n = retries; n 0; n--) { try { return some_io_operation(); } catch (IOException e) { continue; } } return null; } This is how I would solve the problem - https://gist.github.com/876136 Regards, BG -- Baishampayan Ghose b.ghose at gmail.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: Can't recur from within a catch expression.
An alternate way of doing it: (defmacro maybe [ body] `(try [(do ~@body) nil] (catch Exception e# [nil e#]))) (defn repeat-exec ([f] (let [run (fn g[] (cons (f) (lazy-seq (g] (run))) ([n f] (take n (repeat-exec f (defmacro try-times [n body] `(let [f# #(first (maybe ~@body)) c# (repeat-exec (dec ~n) f#) r# (some identity c#)] (or r# (do ~@body Putting it into action: (defn f The sample function we want to try several times [] (println x ) (str y z ) (throw (NullPointerException. boom ))) (try-times 10 (f)) Hope this helps. Regards, Shantanu On Mar 18, 6:49 am, Fiel Cabral e4696wyoa63emq6w3250kiw60i4...@gmail.com wrote: Hello Clojure users, This is a dumb question but I'd like to write something equivalent to this in Clojure: public String loop_with_exception(int retries) { for (int n = retries; n 0; n--) { try { return some_io_operation(); } catch (IOException e) { continue; } } return null; } So I tried writing it like this: (ns sandbox.core (:import [java.io.IOException])) (defn some-io-operation Some read I/O operation that could throw an IOException. [] (println WOULD do a read operation)) (defn loop-with-exception [retries] (loop [n retries] (try (when (pos? n) (some-io-operation)) (catch IOException e (recur (dec n)) But I get this error: cd c:/EMACSHOME/CLOJURE-PROJECTS/sandbox/src/sandbox/ 1 compiler notes: Unknown location: error: java.lang.UnsupportedOperationException: Cannot recur from catch/finally core.clj:13:9: error: java.lang.UnsupportedOperationException: Cannot recur from catch/finally (core.clj:13) Compilation failed. What's the recommended way to handle exceptions and retry inside a loop/recur? Thank you. -Fiel Cab.ral -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Can't recur from within a catch expression.
This of course won't work when the body of code returns nil. Regards, Shantanu On Mar 18, 8:08 pm, Shantanu Kumar kumar.shant...@gmail.com wrote: An alternate way of doing it: (defmacro maybe [ body] `(try [(do ~@body) nil] (catch Exception e# [nil e#]))) (defn repeat-exec ([f] (let [run (fn g[] (cons (f) (lazy-seq (g] (run))) ([n f] (take n (repeat-exec f (defmacro try-times [n body] `(let [f# #(first (maybe ~@body)) c# (repeat-exec (dec ~n) f#) r# (some identity c#)] (or r# (do ~@body Putting it into action: (defn f The sample function we want to try several times [] (println x ) (str y z ) (throw (NullPointerException. boom ))) (try-times 10 (f)) Hope this helps. Regards, Shantanu On Mar 18, 6:49 am, Fiel Cabral e4696wyoa63emq6w3250kiw60i4...@gmail.com wrote: Hello Clojure users, This is a dumb question but I'd like to write something equivalent to this in Clojure: public String loop_with_exception(int retries) { for (int n = retries; n 0; n--) { try { return some_io_operation(); } catch (IOException e) { continue; } } return null; } So I tried writing it like this: (ns sandbox.core (:import [java.io.IOException])) (defn some-io-operation Some read I/O operation that could throw an IOException. [] (println WOULD do a read operation)) (defn loop-with-exception [retries] (loop [n retries] (try (when (pos? n) (some-io-operation)) (catch IOException e (recur (dec n)) But I get this error: cd c:/EMACSHOME/CLOJURE-PROJECTS/sandbox/src/sandbox/ 1 compiler notes: Unknown location: error: java.lang.UnsupportedOperationException: Cannot recur from catch/finally core.clj:13:9: error: java.lang.UnsupportedOperationException: Cannot recur from catch/finally (core.clj:13) Compilation failed. What's the recommended way to handle exceptions and retry inside a loop/recur? Thank you. -Fiel Cab.ral -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Can't recur from within a catch expression.
Here's a version with a similar simulated exception but caught in the main loop, with a bit of tracing code to show the retries. Clojure 1.2.0 user= (import java.io.IOException) java.io.IOException user= user= (defn some-io-operation [] (let [n (rand-int 30)] (if ( n 10) (throw (IOException.)) n))) #'user/some-io-operation user= user= (defn loop-with-exception [retries] (when (pos? retries) (let [v (try (print .) (some-io-operation) (catch IOException e e))] (if (instance? IOException v) (recur (dec retries)) v #'user/loop-with-exception user= (loop-with-exception 10) ..8 On Mar 18, 7:08 am, Daniel Solano Gomez cloj...@sattvik.com wrote: On Thu Mar 17 21:49 2011, Fiel Cabral wrote: Hello Clojure users, This is a dumb question but I'd like to write something equivalent to this in Clojure: public String loop_with_exception(int retries) { for (int n = retries; n 0; n--) { try { return some_io_operation(); } catch (IOException e) { continue; } } return null; } So I tried writing it like this: (ns sandbox.core (:import [java.io.IOException])) (defn some-io-operation Some read I/O operation that could throw an IOException. [] (println WOULD do a read operation)) (defn loop-with-exception [retries] (loop [n retries] (try (when (pos? n) (some-io-operation)) (catch IOException e (recur (dec n)) What's the recommended way to handle exceptions and retry inside a loop/recur? Well, I'm not sure what's the best recommendation, but playing around with it a bit, it seems like moving the exception-handling into the IO method works best. Doing this, you can have different return values depending on whether the operation succeeded. Here's what I came up with: (defn some-io-operation Some read I/O operation that may fail. Returns nil on failure, ::success on success. [] (try (when (zero? (rand-int 3)) (throw (IOException.))) ; return non-nil and non-false on success ::success (catch IOException e ; return nil on failure nil))) (defn loop-with-exception [retries] (first (remove nil? (repeatedly retries some-io-operation I am not absolutely certain the loop-with-exception function is the best implementation, but I believe it works correctly. Sincerely, Daniel Solano Gómez application_pgp-signature_part 1KViewDownload- Hide quoted text - - Show quoted text - -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Can't recur from within a catch expression.
On Fri, Mar 18, 2011 at 8:44 PM, Shantanu Kumar kumar.shant...@gmail.com wrote: This of course won't work when the body of code returns nil. Mine would :) Regards, BG -- Baishampayan Ghose b.ghose at gmail.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: Can't recur from within a catch expression.
If you need a library for automatic retry (which it sounds like from your example), I'd check out this sweet library by Joe Gallo: https://github.com/joegallo/robert-bruce (it uses trampolining retries). - Lee -- 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: ClojureCLR startup times...
core.clj and company are AOT-compiled. The dll's are loaded during startup, not the clojure source. When I run a profiler on it during startup, it reports almost all time being taken by the JITter. I'm open to suggestions on improving this. In particular, more detailed profiling information would be of interest. -David On Mar 18, 8:16 am, Timothy Baldridge tbaldri...@gmail.com wrote: Just downloaded the CLR version of clojure and are experiencing a pretty awkward boot up time of the ClojureMain.exe (~10 sec) I've tried to ngen the exe and it's dependencies but without any luck. Compiling and Running from within VS2010 it takes approximately the same... Sadly, I think this is normal. Clojure on the JVM starts up in about 4-5 sec on my machine. I've found that Clojure-CLR is much slower. As an example here's what's happening: 1) the Clojure-CLR assembly loads 2) Clojure-CLR is run through some initial JIT compilation 3) Clojure-CLR loads core.clj which is about 200KB of Clojure code 4) core.clj is translated to CLR classes, functions, etc. 5) core.clj is run through the JIT 6) other supporting .clj files are read, translated, and run through the JIT 7) now you are at the REPL I think the only real way to improve these startup times is to do what Python does, and have cached assemblies so that core.clj doesn't have to be re-compiled each and every time. But as far as I know no one is even looking into this. Timothy -- 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: ClojureCLR startup times...
I'm open to suggestions on improving this. In particular, more detailed profiling information would be of interest. Hrm...we have some fairly detailed profilers through my work, so I'll have to fire it up over lunch and see what I get. timothy -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Can't recur from within a catch expression.
On Mar 18, 8:08 pm, Shantanu Kumar kumar.shant...@gmail.com wrote: An alternate way of doing it: (defmacro maybe [ body] `(try [(do ~@body) nil] (catch Exception e# [nil e#]))) (defn repeat-exec ([f] (let [run (fn g[] (cons (f) (lazy-seq (g] (run))) ([n f] (take n (repeat-exec f (defmacro try-times [n body] `(let [f# #(first (maybe ~@body)) c# (repeat-exec (dec ~n) f#) r# (some identity c#)] (or r# (do ~@body The `try-times` macro above is buggy (doesn't work when body of code returns logical false). Fixed version is below: (defmacro try-times [n body] {:pre [(posnum? n)]} `(let [c# (repeat-exec (dec ~n) #(maybe ~@body)) r# (some #(if (last %) nil %) c#)] (first (or r# [(do ~@body)] Regards, Shantanu -- 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: ClojureCLR startup times...
Hrm...we have some fairly detailed profilers through my work, so I'll have to fire it up over lunch and see what I get. Yeah, I'm starting to think dmiller is correct. I went over the performance traces several times, and it really seems to all revolve around LoadAssembly, and the time it takes to load, and JIT that assembly. This is something that is a bad side-effect of a method level JIT. I think a tracing JIT would be a much better fit with Clojure, but that's an argument for a different day. Timothy -- “One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.” (Robert Firth) -- 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: slime-eval-at-register
On Mar 17, 9:39 pm, Ken Wesson kwess...@gmail.com wrote: On Thu, Mar 17, 2011 at 7:32 PM, Martin Blais bl...@furius.ca wrote: Emacs-using Clojurians may enjoy the following tidbit of Slime I just dreamed of: (defun slime-eval-at-register (reg) Take the cursor to a register's location and eval the expression there. Useful for testing stuff without having to 'go there' first. (interactive cEval at register: ) (save-excursion (jump-to-register reg) (slime-eval-last-expression))) ;; Note: slime-interactive-eval is also available on C-c :, ;; so we override it for something that looks like C-x C-e. (define-key slime-mode-map \C-c\C-e 'slime-eval-at-register) I'm curious. How does this work? I'll take a stab at it: 1. (interactive ...) is a macro that expands to nothing. 2. (define-key ...) parses the source of the named function, and if it sees (interactive ...), binds the key to a closure that presents the prompt string after interactive, gets a response from the user, and then calls that function with that response as the parameter. 3. (save-excursion ...) is a macro that wraps its body in code to push and pop the cursor location, so the insertion point returns to where it was immediately. Am I close? (interactive) makes the function into a command. I don't know how it works under the covers. I imagine the emacs-lisp evaluator has a special case for it (maybe it attaches meta-data to the function object). Emacs knows to pass context to a command based on a format string provided to interactive, e.g. (interactive r) will pass the current region to the function when it gets invoked interactively, i.e. triggered from a key sequence via a keymap. In other words, commands are just functions which can be invoked from key events and optionally get extra context given to them. (define-key) creates a binding in Slime mode that invokes the command. (save-excursion) you got it. This is just a new Emacs command that expands the functionality of registers beyond storing text, positions and window configuration. You can set a position register as usual C-x r SPC reg. If you invoke the command with C-c C-e reg, it will move the cursor at the saved register position, slime-eval at that point, and return the cursor where it was. When I'm mucking around with Clojure from Emacs, I always have a few test expressions that I evaluate repeatedly. With this trick I can eval them quickly from anywhere without having to move my cursor. I prefer working like this over using deftest and running a full suite of tests every time (I'm sure you can customize that though); I just invoke a single function, change some code, invoke again, etc. until it works as I expect it to. I'm probably weird, I don't know. Does anyone else find this useful? (I had this idea yesterday and I thought it was useful so I shared it. Probably OT, should have sent to Emacs list. Sorry.) -- 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: ClojureCLR startup times...
No signed assemblies. What got you below the reported JIT activity into the real problem? On Mar 18, 11:37 am, Scott Thoman scott.tho...@gmail.com wrote: I've had a similar issue unrelated to clojure. In my case we had some signed assemblies and the CLR was taking a long time, in certain environments, to go out and check for certificate revocations when verifying signatures. IIRC, the initial profiling I did made it look like the time was spent JIT-ing though it wasn't. I've not played at all with clojure-CLR at this point so I don't know if my experience helps or is related to this issue. -stt -- 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: ClojureCLR startup times...
From what I'm reading about some IronPython tests, it sounds like the .NET x64 JIT is much slower in startup times. Are we all running 64 bit? Timothy No signed assemblies. What got you below the reported JIT activity into the real problem? -- 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
defrecord/defprotocol documentation suggestions
I would suggest adding something on the lines of these - it is confusing to newcomers, and it really isnt complete documentation to not include. The crux of the matter is how clojure defines methods when a record has inline protocols, etc. Maybe i missed this in the documentations. 1. extend-protocol will allow the record to refer to the same protocol if the protocol is redefined (and even if the protocol is simply reloaded, but the definition doesnt change, it will still screw up records whicih define the protocol inline), inline wont http://groups.google.com/group/clojure/browse_thread/thread/607902114ab55ecd 2. record definitions with inline protocol definitions get redifined on a :reload-all (i think), even if their definitions dont actually change. This means previous records of the same type dont refer to the new same type - this is only of a concern when developing at the repl. This is a problem for type hinting and dispatching on classes in multimethods, and for testing types. 3. if defrecord has inline protocol definitions, you can access these methods with the dot syntax - otherwise you cant. ;;example - dot (defprotocol PSocket (close [this])) (defrecord socket [] PSocket (close [this] true)) (.close (socket.)) and (close socket) works (defrecord socket2 []) (extend-protocol PSocket socket2 (close [this] false)) (.close (socket2.)) ;;no matching field found error (close (socket2.)) ;;= returns false And for defprotocol 1. (defprotocol Test) Test = returns a map while in another ns, when you import the ns which has Test, Test will return the actual class. This is a problem for multimethods in the same ns - of course, you can just do myns.Test -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Can't recur from within a catch expression.
On Fri, Mar 18, 2011 at 1:13 PM, Shantanu Kumar kumar.shant...@gmail.com wrote: The `try-times` macro above is buggy (doesn't work when body of code returns logical false). Fixed version is below: (defmacro try-times [n body] {:pre [(posnum? n)]} `(let [c# (repeat-exec (dec ~n) #(maybe ~@body)) r# (some #(if (last %) nil %) c#)] (first (or r# [(do ~@body)] You might want to unwrap everything after: (defmacro try-times [n body] {:pre [(posnum? n)]} `(let [c# (repeat-exec (dec ~n) #(maybe ~@body)) r# (some #(if (last %) nil %) c#) [r# e#] (first (or r# [(do ~@body)]))] (if e# (throw e#) r#))) This will re-throw the last exception on failure. On success it will evaluate to the return value of the successful execution of the body. So this try-times trying to acquire a network socket may throw a socket unavailable exception or return the socket, rather than returning a vector of a maybe-socket and a maybe-exception. The other thing you will probably want is a delay: (defmacro try-times [delay n body] {:pre [(posnum? n)]} `(let [c# (repeat-exec (dec ~n) #(do (maybe ~@body) Thread/sleep ~delay)) r# (some #(if (last %) nil %) c#) [r# e#] (first (or r# [(do ~@body)]))] (if e# (throw e#) r#))) Now you can (try-times 3 10 (acquire-socket 80)) to spend five minutes trying to grab a socket at half-minute intervals, throw on failure, and return the socket on success. That would be quite handy for low level networking, where you usually want to wait a bit between retries for congestion to ease or other conditions to change that were impeding success. -- 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: Strange Loop 2011 - St. Louis - Sept 18-20
We do intend to record some or all of the talks. Alex On Mar 16, 10:08 pm, Ambrose Bonnaire-Sergeant abonnaireserge...@gmail.com wrote: I'm on the wrong side of the world, will these be recorded? Looks exciting On Wed, Mar 16, 2011 at 4:49 AM, Nick Zbinden nick...@gmail.com wrote: Man that look awesome not a single thing I would wanne miss. On Mar 15, 9:16 pm, Alex Miller a...@puredanger.com wrote: I just put up a blog entry with some updated info on Strange Loop 2011:http://thestrangeloop.com/blog/11/03/15/strange-loop-2011-update Lots more to come, but perhaps of interest to this group, *Rich Hickey* is the first confirmed keynote speaker (more in the works)... Workshops: - Hilary Mason - machine learning - Matthew McCullough - Git - Jeff Brown - Grails - Nathan Marz - Cascalog - members of Clojure/core - Clojure Sessions: - Jeremy Ashkenas - Coffeescript - Daniel Spiewak - functional data structures (Scala) - Peter Veentjer - Multiverse STM - John Hugg - VoltDB, OLTP - Zach Tellman - Lamina, Aleph (Clojure) - Nate Young - functional parser combinators (Haskell, Clojure) - Jim Duey - monads (Clojure) - Rúnar Bjarnason - Scalaz - Slava Pestov - Factor - Eric Brigham - Bitcoin (cryptocurrency) - Nathan Marz - building scalable realtime big data systems - Benjamin Manes, Charles Fry - concurrent caching with MapMaker -- 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] fs - file system utilities for Clojure
Hi Miki, The dirname testcase fails on Window. Does it make sense that even if it's running on window it should still pass? In another word, don't you think that it should not convert the separator implicitly? (deftest dirname-test (is (= (dirname /a/b/c) /a/b))) user= (fs/dirname /a/b/c) \\a\\b Thanks, Si Yu -- 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] fs - file system utilities for Clojure
Greetings, The dirname testcase fails on Window. Does it make sense that even if it's running on window it should still pass? In another word, don't you think that it should not convert the separator implicitly? I agree with you, and I'll try to fix the test. However I don't have access to a windows machine to make sure this work. You can track this bug at https://bitbucket.org/tebeka/fs/issue/4/dirname-test-fails-on-windows All the best, -- Miki -- 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
*ns* unexpectedly changing on me, via SwingUtilities/invokeLater
I want to examine the namespace within a swing thread, as I want to see if a particular var is defined from another part of the program before operating on it. However, inside the do-swing macro (or just using SwingUtilities/invokeLater directly) *ns* will always refer to clojure.core, not the namespace of the file I'm working on. eg: (ns some-namespace) (javax.swing.SwingUtilities/invokeLater (fn [] (println *ns*))) will print: #Namespace clojure.core But if I'm defensive and copy *ns*: (ns other-namespace) (def this-namespace *ns*) (javax.swing.SwingUtilities/invokeLater (fn [] (println this- namespace))) will print: #Namespace other-namespace as expected. Interestingly, all DEF instructions are interned into the expected namespaces: other-namespace (javax.swing.SwingUtilities/invokeLater (fn [] (def somevar 1))) nil other-namespace somevar 1 other-namespace clojure.core/somevar ; Evaluation aborted. other-namespace other-namespace/somevar 1 Is this expected behaviour? -- 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
Operations inside associative structures
I'm looking to do some operations upon the data in an associative structure. What do you think about this method of hijacking the definition of assoc-in? Is there some better way to do what I'm doing here? user (defn op-in [op m [k ks] v] (if ks (assoc m k (op-in op (get m k) ks v)) (assoc m k (op (get m k) v #'user/op-in user (def union-in (partial op-in clojure.set/union)) #'user/union-in user (union-in {:a [#{1 2}]} [:a 0] #{1 7 8}) {:a [#{1 2 7 8}]} user (def conj-in (partial op-in conj)) #'user/conj-in user (conj-in {:a [#{1 2}]} [:a 0] 7) {:a [#{1 2 7}]} -- 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: Operations inside associative structures
On Fri, Mar 18, 2011 at 9:04 PM, Nick npatric...@gmail.com wrote: I'm looking to do some operations upon the data in an associative structure. What do you think about this method of hijacking the definition of assoc-in? Is there some better way to do what I'm doing here? user (defn op-in [op m [k ks] v] (if ks (assoc m k (op-in op (get m k) ks v)) (assoc m k (op (get m k) v #'user/op-in user (def union-in (partial op-in clojure.set/union)) #'user/union-in user (union-in {:a [#{1 2}]} [:a 0] #{1 7 8}) {:a [#{1 2 7 8}]} user (def conj-in (partial op-in conj)) #'user/conj-in user (conj-in {:a [#{1 2}]} [:a 0] 7) {:a [#{1 2 7}]} Something wrong with using update-in here? -- 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 Editor
On 3/13/2011 1:09 PM, WoodHacker wrote: If you are looking for a very good editor for Clojure try Bluefish. It's been around for ever, is very stable, and does everything you would want an editor to do. And it now works with Clojure. Unless I am mistaken it seems to be missing a nice-to-have feature for a programmer's editor, namely scriptability. -- O.L. -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Can't recur from within a catch expression.
On Mar 19, 2:27 am, Ken Wesson kwess...@gmail.com wrote: On Fri, Mar 18, 2011 at 1:13 PM, Shantanu Kumar kumar.shant...@gmail.com wrote: The `try-times` macro above is buggy (doesn't work when body of code returns logical false). Fixed version is below: (defmacro try-times [n body] {:pre [(posnum? n)]} `(let [c# (repeat-exec (dec ~n) #(maybe ~@body)) r# (some #(if (last %) nil %) c#)] (first (or r# [(do ~@body)] You might want to unwrap everything after: (defmacro try-times [n body] {:pre [(posnum? n)]} `(let [c# (repeat-exec (dec ~n) #(maybe ~@body)) r# (some #(if (last %) nil %) c#) [r# e#] (first (or r# [(do ~@body)]))] (if e# (throw e#) r#))) This will re-throw the last exception on failure. On success it will evaluate to the return value of the successful execution of the body. So this try-times trying to acquire a network socket may throw a socket unavailable exception or return the socket, rather than returning a vector of a maybe-socket and a maybe-exception. My version already does that if I understand correctly. Ken Wesson's variation on `delay` is neat. I came up with another way to retry on exception: (defmacro try-while Return the result after executing code body; on exception keep re- trying as long as pred returns true. The predicate function accepts thrown exception as argument. Unless pred throws an exception, no exception will escape the code body itself. [pred body] {:pre [`(fn? ~pred)]} `(first (some #(let [e# (last %)] (if e# (if (~pred e#) false (throw e#)) %)) (repeat-exec #(maybe ~@body) Regards, Shantanu -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Can't recur from within a catch expression.
It occurs to me you probably only want the delay between successive retries, so really you want to put the delay in the retrying code, or maybe use interpose in some manner (and make the interposed delay function look like a failure to the surrounding loop). -- 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: Comparing clojure speed to java speed
Is there a reason I can't get this clojure program to compare with the java one? The following code: for(int q = 0;q 5;q++){ Point2D.Float a = new Point2D.Float(1, 2), b = new Point2D.Float(3, 4); long start = System.currentTimeMillis(); for(int d = 0;d (int)1e9;d++){ a.getX(); } long stop = System.currentTimeMillis(); System.out.println(stop - start); } prints this: 7 6 0 0 0 And this code: (set! *warn-on-reflection* true) (import 'java.awt.geom.Point2D$Float) (dotimes [_ 5] (let [q (Point2D$Float. 1 2)] (time (dotimes [_ (int 1e9)] (.getX q) prints this: Elapsed time: 2220.368412 msecs Elapsed time: 2214.941962 msecs Elapsed time: 2168.259558 msecs Elapsed time: 2162.655501 msecs Elapsed time: 2172.560098 msecs On Mar 11, 7:05 am, Chouser chou...@gmail.com wrote: On Thu, Mar 10, 2011 at 11:26 PM, Jarl Haggerty jarlhagge...@gmail.com wrote: Hmm, I should have thought of that. NewClojure: (ns hello.test (import org.jbox2d.common.Vec2) (:gen-class)) (defn -main [ args] (dotimes [q 5] (let [#^Vec2 a (Vec2. 1 2) #^Vec2 b (Vec2. 3 4)] (time (loop [x (int 0)] (when ( x (int 1e9)) (.addLocal a b) (recur (unchecked-inc x I think if you turn on reflection warnings and remove those type hints, you'll find those hints aren't useful. In which case I'd recommend leaving them out. --Chouser -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Comparing clojure speed to java speed
On Sat, Mar 19, 2011 at 12:26 AM, Jarl Haggerty jarlhagge...@gmail.comwrote: Is there a reason I can't get this clojure program to compare with the java one? The following code: for(int q = 0;q 5;q++){ Point2D.Float a = new Point2D.Float(1, 2), b = new Point2D.Float(3, 4); long start = System.currentTimeMillis(); for(int d = 0;d (int)1e9;d++){ a.getX(); } long stop = System.currentTimeMillis(); System.out.println(stop - start); } prints this: 7 6 0 0 0 That's 1 billion operations getting JITed away. Not really useful for profiling real programs, eh? :) David -- 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: *print-dup* and subvec
Thanks Stuart. Is that a bug that will be fixed in 1.3? I was under the impression that we can rely on print-dup for serialization. Am I wrong about that? Carson -- 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