struct coll
Hi, I am sorry if this has already discussed, but I didn't see it when I did a search. I keep running into instances where I would like to define a struct, using defstruct, and then have that be applied to the elements of a coll. A simple example might explain it better. (defstruct mystruct :A :B :C :D) (def somecoll ["abcd" 5 6.2 [1 2 3]]) So far the only way I have been able to get this to work is to do something that is rather awkward. (struct mystruct (first somecoll) (second somecoll) (nth somecoll 2) (last somecoll)) Is there a better way of doing this? For example, something like: (apply-struct-coll mystruct mycoll) -- 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: roll call of production use?
Hi, we replaced a command line interface for an internal, mission-critical application with a custom Clojure-REPL. It is used for administrative tasks as well as testing. Cheers, Stefan -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: struct coll
Hi, On Wed, Nov 25, 2009 at 9:01 AM, cej38 wrote: > Hi, > I am sorry if this has already discussed, but I didn't see it when I > did a search. > I keep running into instances where I would like to define a struct, > using defstruct, and then have that be applied to the elements of a > coll. A simple example might explain it better. > > (defstruct mystruct :A :B :C :D) > > (def somecoll ["abcd" 5 6.2 [1 2 3]]) > > So far the only way I have been able to get this to work is to do > something that is rather awkward. > > (struct mystruct (first somecoll) (second somecoll) (nth somecoll 2) > (last somecoll)) > > Is there a better way of doing this? For example, something like: > > (apply-struct-coll mystruct mycoll) > (apply struct mystruct mycoll) works well. Christophe -- 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: N00B Java Question
Wow! Many thanks for all the replies :-D If there are any speech recognition enthusiasts out there, this code is part of a Clojure example for Sphinx4. http://cmusphinx.svn.sourceforge.net/viewvc/cmusphinx/trunk/sphinx4/src/scripts/clojure/ClojureTranscriber.clj?view=markup Suggestions/improvements/additions welcome. Thanks again P On Nov 24, 8:51 pm, dannyo152 wrote: > Peter: > > I recommend pages 60-62 in Stuart's book for demonstrating how to do > this. The notes about (. Math PI) or Math/PI (equivalent notations) > are very much to point. > > To demonstrate: > > ;first the import > user=> (import '(java.util.logging Logger Level)) > nil > > ;create a logger > user=> (def our-logger (. Logger getLogger "")) > #'user/our-logger > > ;set the level > user=> (. our-logger setLevel Level/WARNING) > nil > > ;check > user=> (. our-logger getLevel) > # > > I only knew where to look because as a java-slinging N00B myself, I > had many questions about java interop. > > Dan > > On Nov 24, 1:39 pm, Peter Wolf wrote: > > > Hi all, > > > Here is a N00B question, but I can not find the answer by Googling, or > > reading Stuart's book. So, I assume that others will want to find this > > FAQ in the future. > > > I am calling legacy code, and I need to set the level on the Java Logger. > > > In Java it would look like this > > > import java.util.logging.Logger > > import java.util.logging.Level > > > Logger.getLogger("").setLevel(Level.WARNING) > > > What is the Clojure equivalent? In particular, what is the equivalent > > of Level.WARNING? > > > Level.WARNING is a static field of Level, and is an instance of Level. > > There is no getter method. I can't find anything about accessing static > > fields in the docs. > > > Thanks in advance > > Peter -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: ANN: Clojure API for AllegroGraph
Thanks Mike. No big hurry on my part - I'll look at the tutorial code. On Nov 24, 4:39 pm, Mike Hinchey wrote: > You're right, the tests have not been converted to 3.2, so they are not > running at this time. The best thing to look at is the tutorial.clj - most > of these work properly, but example6 doesn't return correct results. The > ones that don't work is where the clj code is incomplete, so I still have > some work to do. I'll get these fixed or documented in the next few days. > > -Mike > > On Tue, Nov 24, 2009 at 3:31 PM, patrickdlogan wrote: > > > Hi Mike - thanks for this. > > > I am fairly new to git, but from what I can tell, I have the agraph32 > > branch as current. The clojure code on the agraph32 branch still seems > > to be using the AG 4.0 API. For example... > > >http://github.com/franzinc/agraph-java-client/blob/agraph32/clojure/t... > > > Running this against the agraph32 branch java jar results in a class > > not found exception... > > > java.lang.ClassNotFoundException: > > com.franz.agraph.repository.AGCatalog (agtest.clj:0) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Question about future
Hi all, I'm new to Clojure and playing with small programs. Today I wrote a snippet to figure out how future works: (defn testf [] (let [f (future #(do (Thread/sleep 5000) %) 5) g 7] (+ g @f))) (println (testf)) I'm expecting the program to sleep 5 seconds and then print 12 and immediately terminate. However, the program seems to print out 12 immediately, and it takes a long time (more like 50 seconds instead of 5) to terminate. Am I missing something? Thanks. --Hong -- 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: performance issues with multi-dimensional array
Hi Konrad, In the original post, I put in the code, it was removed by the post editor. The way I wanted it implement, I have the main in java (gui and stuff). I create a 3D array in java, it's an int array (i.e. int arr[] [][] ) and call with that array to clojure. It can be done in java at least 3 orders of magnitude faster so I'm sure there is something I'm doing wrong. I'll appreciate any suggestion. Thanks, Amnon I've squeezed my example into two functions: (defn light [arr x y] (+ (aget arr x y 0) (aget arr x y 1) (aget arr x y 2)) ) (defn grey [arr] (dotimes [x (length arr)] (dotimes [y (alength (aget arr 0))] (let [lg (int (/ (light arr x y) 3))] (aset arr x y 0 lg) (aset arr x y 1 lg) (aset arr x y 2 lg) On Nov 24, 8:35 pm, Konrad Hinsen wrote: > On 24 Nov 2009, at 17:30, Amnon wrote: > > > I hope it's not the billion time you get the question. > > I wanted to use clojure for image processing. I have a 3 dimensional > > array I'm passing to clojure from java. > > I then loop on the array to manipulate it. > > Even the simplest task takes about half a minutes (I expected it to be > > over in less than a second) > > Could you give some more details? For example, what is the Java class > you use for the 3D arrays? How are you accessing it from Clojure? How > do you do the loops? How do you create the destination array, and how > do you fill it with the grey-scale values? I suspect that many of > these steps could be optimized, but it's hard to say without knowing > how your current code works. > > Konrad. -- 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
Recursions under lazy-seq - how does it work?
Just out of curiosity, How come that infinite recursions under lazy-seq won't crash the program, whilst regular infinite recursion would crash the program ? What's the trick ? For example why doesn't the following "repeatedly" never crash? (defn repeatedly [f] (lazy-seq (cons (f) (repeatedly f (last (repeatedly rand)) ;won't crash -- 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: struct coll
Hi, On Nov 25, 9:01 am, cej38 wrote: > Is there a better way of doing this? For example, something like: > > (apply-struct-coll mystruct mycoll) Close: (apply struct mystruct mycol) Sincerely Meikel -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: "Oh, yeah, transients are fast!"
Here are my results. The transient version performs slightly better on windows 32bit client JVM, and considerably better on linux 64bit server JVM (they are both 1.06.0_17) -- 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: N00B Java Question
user=> (import '(java.util.logging Logger Level)) nil user=> (def my-logger (Logger/getLogger "mylogger")) #'user/my-logger user=> (. my-logger setLevel Level/WARNING) nil user=> (. my-logger warning "this is a warning") Nov 25, 2009 5:38:25 AM sun.reflect.NativeMethodAccessorImpl invoke0 WARNING: this is a warning nil On Nov 24, 1:39 pm, Peter Wolf wrote: > Hi all, > > Here is a N00B question, but I can not find the answer by Googling, or > reading Stuart's book. So, I assume that others will want to find this > FAQ in the future. > > I am calling legacy code, and I need to set the level on the Java Logger. > > In Java it would look like this > > import java.util.logging.Logger > import java.util.logging.Level > > Logger.getLogger("").setLevel(Level.WARNING) > > What is the Clojure equivalent? In particular, what is the equivalent > of Level.WARNING? > > Level.WARNING is a static field of Level, and is an instance of Level. > There is no getter method. I can't find anything about accessing static > fields in the docs. > > Thanks in advance > Peter -- 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: Sneak Peek
Hi Jim, On Tue, Nov 24, 2009 at 11:21 PM, jim wrote: > Evening all, > > I've been working on a library for writing web applications for > compojure. I've got it written and (I think) working. First thing > tomorrow is to write a sample app and post it along with the library. > > But in the meantime, I thought I'd let you all read about it if you're > having trouble sleeping. :) > > http://intensivesystems.net/tutorials/web_sessions.html Neat. It looks like you're writing what's usually called a "continuation-based web framework". When I read your title and first couple paragraphs, I thought you were writing a session-management library (e.g. setting cookies, keeping server-side state, etc.). You might consider mentioning continuations somewhere in your article, if only to attract Googling continuation-based fans. You might already be familiar with other continuation-based frameworks. If not, consider looking at projects like Wee (for Ruby) and Seaside (for Smalltalk) to steal some ideas. :) Best, Graham > > One thing I love about FP in general and Clojure specifically is the > density of code that is possible. This library is only about 80 lines > of code. > > Jim > > -- > 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
classpath issue with code from book: programming clojure
I am going through Programming Clojure and I recently downloaded the code from the books official website. For other utils I can do, for example, (require 'clojure.contrib.str-utils) and it works. But how do I load code from the book? (require 'examples.introduction) throws the following exception: java.io.FileNotFoundException: Could not locate examples/ introduction__init.class or examples/introduction.clj on classpath: (NO_SOURCE_FILE:0) [Thrown class clojure.lang.Compiler$CompilerException] Here is the full backtrace: Backtrace: 0: clojure.lang.Compiler.eval(Compiler.java:4543) 1: clojure.core$eval__3990.invoke(core.clj:1728) 2: swank.commands.basic$eval_region__686.invoke(basic.clj:36) 3: swank.commands.basic$listener_eval__695.invoke(basic.clj:50) 4: clojure.lang.Var.invoke(Var.java:346) 5: user$eval__1200.invoke(NO_SOURCE_FILE) 6: clojure.lang.Compiler.eval(Compiler.java:4532) 7: clojure.core$eval__3990.invoke(core.clj:1728) 8: swank.core$eval_in_emacs_package__307.invoke(core.clj:55) 9: swank.core$eval_for_emacs__384.invoke(core.clj:123) 10: clojure.lang.Var.invoke(Var.java:354) 11: clojure.lang.AFn.applyToHelper(AFn.java:179) 12: clojure.lang.Var.applyTo(Var.java:463) 13: clojure.core$apply__3243.doInvoke(core.clj:390) 14: clojure.lang.RestFn.invoke(RestFn.java:428) 15: swank.core$eval_from_control__310.invoke(core.clj:62) 16: swank.core$eval_loop__313.invoke(core.clj:67) 17: swank.core$spawn_repl_thread__445$fn__476$fn__478.invoke(core.clj: 173) 18: clojure.lang.AFn.applyToHelper(AFn.java:171) 19: clojure.lang.AFn.applyTo(AFn.java:164) 20: clojure.core$apply__3243.doInvoke(core.clj:390) 21: clojure.lang.RestFn.invoke(RestFn.java:428) 22: swank.core$spawn_repl_thread__445$fn__476.doInvoke(core.clj:170) 23: clojure.lang.RestFn.invoke(RestFn.java:402) 24: clojure.lang.AFn.run(AFn.java:37) 25: java.lang.Thread.run(Unknown Source) I am using both Clojure Box and Enclojure in NetBeans on Windows XP. Is it a classpath issue? Where should I place the folder that contains code from the book? Please help me out with my variable enviroment settings as well. -- 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: Question about future
I'm not quite sure what you're seeing. You might want to use the time macro to help. Here's what I was able to do: user=> (time ((fn [] (let [f (future (#(do (Thread/sleep 5000) %) 5)) g 7] (+ g @f) "Elapsed time: 4975.917889 msecs" 12 Sean On Nov 25, 12:04 am, Hong Jiang wrote: > Hi all, > > I'm new to Clojure and playing with small programs. Today I wrote a > snippet to figure out how future works: > > (defn testf [] > (let [f (future #(do > (Thread/sleep 5000) > %) > 5) > g 7] > (+ g @f))) > > (println (testf)) > > I'm expecting the program to sleep 5 seconds and then print 12 and > immediately terminate. However, the program seems to print out 12 > immediately, and it takes a long time (more like 50 seconds instead of > 5) to terminate. Am I missing something? > > Thanks. > --Hong -- 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: Sneak Peek
Graham, That's exactly what it is. I used the continuation monad from clojure.contrib.monads. After I get the code out, I'll be writing a tutorial on how it works which will also explain the continuation monad. I found that monad to be the most difficult to get my head around, but it's hugely powerful. Using it, that library only took about 50 lines of code to write. Having to account for browser history took another 30. However, those 50 lines of code will turn your brain to mush if you don't have a handle on the continuation monad. :) Jim Graham Fawcett wrote: > Hi Jim, > > On Tue, Nov 24, 2009 at 11:21 PM, jim wrote: > > Evening all, > > > > I've been working on a library for writing web applications for > > compojure. I've got it written and (I think) working. First thing > > tomorrow is to write a sample app and post it along with the library. > > > > But in the meantime, I thought I'd let you all read about it if you're > > having trouble sleeping. :) > > > > http://intensivesystems.net/tutorials/web_sessions.html > > Neat. It looks like you're writing what's usually called a > "continuation-based web framework". When I read your title and first > couple paragraphs, I thought you were writing a session-management > library (e.g. setting cookies, keeping server-side state, etc.). You > might consider mentioning continuations somewhere in your article, if > only to attract Googling continuation-based fans. > > You might already be familiar with other continuation-based > frameworks. If not, consider looking at projects like Wee (for Ruby) > and Seaside (for Smalltalk) to steal some ideas. :) > > Best, > Graham > > > > > One thing I love about FP in general and Clojure specifically is the > > density of code that is possible. This library is only about 80 lines > > of code. > > > > Jim > > > > -- > > 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: Sneak Peek
On 25.11.2009, at 15:32, jim wrote: > That's exactly what it is. I used the continuation monad from > clojure.contrib.monads. After I get the code out, I'll be writing a > tutorial on how it works which will also explain the continuation > monad. I found that monad to be the most difficult to get my head > around, but it's hugely powerful. I can confirm that impression... And only wish you good luck writing that tutorial! Konrad. -- 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: Question about future
On Tue, Nov 24, 2009 at 09:04:38PM -0800, Hong Jiang wrote: >Hi all, > >I'm new to Clojure and playing with small programs. Today I wrote a >snippet to figure out how future works: > >(defn testf [] > (let [f (future #(do > (Thread/sleep 5000) > %) > 5) >g 7] >(+ g @f))) You don't ever evaluate the function containing the sleep, you just create it, and then immediately return 5. Future contains an explicit do, so you can just do the steps in the future: [f (future (Thread/sleep 5000) 5) ... Or, if you want to get the function call in, you'll need to call it: [f (future (#(do (Thread/sleep 5000) %) 5)) ...] 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
pre-1.1 User Survey results
On Nov 23, 4:55 pm, the.stuart.sie...@gmail.com wrote: > Clojure User Survey First wave of results (258 responses) How do you get Clojure? 19% Download release 69% Github 8% Maven or Ivy 4% IDE / Package manager What distribution of Clojure do you use primarily? 32% 1.0 release 12% I picked one development snapshot and stuck with it 9% I use the latest development snapshot 35% I follow Github "master" branch 9% I follow Github "new" branch What about chunked sequences? 56% I don't know what they are 12% They help me 1% They hurt me 27% I don't notice any difference -- 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: Recursions under lazy-seq - how does it work?
On Wed, Nov 25, 2009 at 12:10:36AM -0800, Gabi wrote: >How come that infinite recursions under lazy-seq won't crash the >program, whilst regular infinite recursion would crash the program ? >What's the trick ? > >For example why doesn't the following "repeatedly" never crash? > >(defn repeatedly > [f] (lazy-seq (cons (f) (repeatedly f > >(last (repeatedly rand)) ;won't crash The JVM doesn't support tail call elimination, so an infinite retursion causes a new stack frame for each call. The stack is fairly small and will quickly be exhausted. The lazy-seq, instead, wraps the contents up into a function closure and wraps it in a clojure.lang.LazySeq. This causes a few heap allocations. If you were to keep the head of this chain, you would also exhaust the heap (which would take a while longer). But since your example is only walking along next, each item generated contains no more references, and can be garbage collected. Lazy-seq basically turns a stack allocation into a heap allocation. This both allows for not allow of the sequence to be allocated, but allows the earlier parts of the sequence to be collected. Try: (let [r (repeatedly rand)] (last r) r) which will try to keep the whole sequence in memory, and eventually exhaust your heap. 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
swing: efficiently updating a listbox from a clojure list
I'm writing a crossword editor that provides a list of suggestions to fill in the current word. This is displayed in a listbox to the right of the grid, and every time the cursor is moved, I update it via (def update-wlist #(let [w (take 26 (words-with (current-word)))] (. words setListData (to-array w This is slowing my UI down very badly - there is a noticeable lag between hitting a key and having the cursor move. The bottleneck seems to be to-array, since replacing it with (def update-wlist #(let [w (take 26 (words-with (current-word)))] (to-array w))) is still slow, but dropping down to (def wlistdata (to-array (take 26 (words-with "..." (def update-wlist #(let [w (take 26 (words-with (current-word)))] (. words setListData wlistdata))) leaves everything running smoothly. Is there a more efficient way to do this? This is the definition of words-with: (def wordlist (split (slurp "csw.txt") #"\n")) (defn words-with [re] (filter #(re-matches (re-pattern re) %) wordlist)) so if there's some equivalent that directly outputs a java array, that would likely solve my problem. I'm open to all suggestions, though. martin -- 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: AOT'd namespaces lose their metadata
Yup, that's right - I filed ticket 130 to reflect this. If I get there first, I'll mark the ticket as well and we can compare notes. I'm super-excited about Leiningen, btw. I've been thinking about how to turn my autodoc stuff into a Leiningen plugin so that we can get easy doc for any project. On Nov 24, 8:48 pm, Phil Hagelberg wrote: > Tom Faulhaber writes: > > So, Phil, if you want to take it from there, it would be great. If you > > don't, I'll keep it on my list. > > Thanks for the notes; nice to see someone has done some detective work > already. I'll probably try to take a look at this once I get Leiningen > 1.0 out. It looks like ticket #130 in Assembla is about this problem, so > I'll be sure to add a note to there once I start working on it. > > -Phil -- 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: performance issues with multi-dimensional array
Read Christophe's post about multi-dim arrays. Reflection is getting in the way here. On Wed, Nov 25, 2009 at 2:55 AM, Amnon wrote: > Hi Konrad, > In the original post, I put in the code, it was removed by the post > editor. > The way I wanted it implement, I have the main in java (gui and > stuff). I create a 3D array in java, it's an int array (i.e. int arr[] > [][] ) and call with that array to clojure. > > It can be done in java at least 3 orders of magnitude faster so I'm > sure there is something I'm doing wrong. > I'll appreciate any suggestion. > Thanks, > Amnon > > I've squeezed my example into two functions: > > (defn light [arr x y] >(+ (aget arr x y 0) (aget arr x y 1) (aget arr x y 2)) >) > > (defn grey [arr] >(dotimes [x (length arr)] >(dotimes [y (alength (aget arr 0))] >(let [lg (int (/ (light arr x y) 3))] >(aset arr x y 0 lg) >(aset arr x y 1 lg) >(aset arr x y 2 lg) > > > > > > > On Nov 24, 8:35 pm, Konrad Hinsen wrote: > > On 24 Nov 2009, at 17:30, Amnon wrote: > > > > > I hope it's not the billion time you get the question. > > > I wanted to use clojure for image processing. I have a 3 dimensional > > > array I'm passing to clojure from java. > > > I then loop on the array to manipulate it. > > > Even the simplest task takes about half a minutes (I expected it to be > > > over in less than a second) > > > > Could you give some more details? For example, what is the Java class > > you use for the 3D arrays? How are you accessing it from Clojure? How > > do you do the loops? How do you create the destination array, and how > > do you fill it with the grey-scale values? I suspect that many of > > these steps could be optimized, but it's hard to say without knowing > > how your current code works. > > > > Konrad. > > -- > 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: swing: efficiently updating a listbox from a clojure list
Bear in mind that when going from the "slow" to the "fast", you not only removed the repeated calls to to-array, but also removed the overhead of words-with, since the lazy seq returned from the let doesn't get fully realized. Try changing your "fast" version to: (def update-wlist #(let [w (doall (take 26 (words-with (current- word] (. words setListData wlistdata))) My guess is it's not going to be fast anymore. On Nov 25, 8:35 am, Martin DeMello wrote: > I'm writing a crossword editor that provides a list of suggestions to > fill in the current word. This is displayed in a listbox to the right > of the grid, and every time the cursor is moved, I update it via > > (def update-wlist #(let [w (take 26 (words-with (current-word)))] > (. words setListData (to-array w > > This is slowing my UI down very badly - there is a noticeable lag > between hitting a key and having the cursor move. The bottleneck seems > to be to-array, since replacing it with > > (def update-wlist #(let [w (take 26 (words-with (current-word)))] > (to-array w))) > > is still slow, but dropping down to > > (def wlistdata (to-array (take 26 (words-with "..." > > (def update-wlist #(let [w (take 26 (words-with (current-word)))] > (. words setListData wlistdata))) > > leaves everything running smoothly. Is there a more efficient way to do this? > > This is the definition of words-with: > > (def wordlist (split (slurp "csw.txt") #"\n")) > > (defn words-with [re] > (filter #(re-matches (re-pattern re) %) wordlist)) > > so if there's some equivalent that directly outputs a java array, that > would likely solve my problem. I'm open to all suggestions, though. > > martin -- 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: swing: efficiently updating a listbox from a clojure list
ooh, i wondered how words-with was so fast - my first thought was that it was bound to be the bottleneck, but when i removed the to-array and things sped up i figured maybe the jvm was more efficient than i thought :) laziness takes a bit of getting used to! m. On Thu, Nov 26, 2009 at 12:26 AM, ataggart wrote: > Bear in mind that when going from the "slow" to the "fast", you not > only removed the repeated calls to to-array, but also removed the > overhead of words-with, since the lazy seq returned from the let > doesn't get fully realized. > > Try changing your "fast" version to: > > (def update-wlist #(let [w (doall (take 26 (words-with (current- > word] > (. words setListData wlistdata))) > > My guess is it's not going to be fast anymore. > > > On Nov 25, 8:35 am, Martin DeMello wrote: >> I'm writing a crossword editor that provides a list of suggestions to >> fill in the current word. This is displayed in a listbox to the right >> of the grid, and every time the cursor is moved, I update it via >> >> (def update-wlist #(let [w (take 26 (words-with (current-word)))] >> (. words setListData (to-array w >> >> This is slowing my UI down very badly - there is a noticeable lag >> between hitting a key and having the cursor move. The bottleneck seems >> to be to-array, since replacing it with >> >> (def update-wlist #(let [w (take 26 (words-with (current-word)))] >> (to-array w))) >> >> is still slow, but dropping down to >> >> (def wlistdata (to-array (take 26 (words-with "..." >> >> (def update-wlist #(let [w (take 26 (words-with (current-word)))] >> (. words setListData wlistdata))) >> >> leaves everything running smoothly. Is there a more efficient way to do this? >> >> This is the definition of words-with: >> >> (def wordlist (split (slurp "csw.txt") #"\n")) >> >> (defn words-with [re] >> (filter #(re-matches (re-pattern re) %) wordlist)) >> >> so if there's some equivalent that directly outputs a java array, that >> would likely solve my problem. I'm open to all suggestions, though. >> >> martin > > -- > 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: swing: efficiently updating a listbox from a clojure list
-- > is still slow, but dropping down to > > (def wlistdata (to-array (take 26 (words-with "..." > > (def update-wlist #(let [w (take 26 (words-with (current-word)))] >(. words setListData wlistdata))) > > leaves everything running smoothly. Is there a more efficient way to do this? > That doesn't actually prove that it is to-array that is the slow part. This is because all of the seq manipulation stuff that you are doing is 'lazy'. So when you run the 'let [w (take 26 (words-with (current-word)))' section, nothing is being consumed and the re-matches part doesn't run until you ask for an item from the list. (So we haven't proved conclusively that to-array is slow, it could be re-matches or re- pattern (which is what I suspect). I think a better way to do this is to not use a regex at all. Canonically I think this sort of thing is (would be?) implemented by constructing a 'sequence' of strings, (first filtered based on potential word length) and recursively filtering based on 'character at position x' in the string, as characters are entered... the resulting seqs could be memoized. Then, you could type a letter and quickly update the options based on the new character at that position (by simply filtering the stored sequence one more time). When you delete a letter, you run it and receive the memoized result from earlier. To display you would want to use 'into-array' and your 'setListData' function, because you seem to have a homogeneous collection of strings; while to-array makes you an array of objects, into-array will return an array of strings (It might help, I'm really not sure). -- 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: swing: efficiently updating a listbox from a clojure list
On Thu, Nov 26, 2009 at 12:31 AM, Jonathan Smith wrote: > > I think a better way to do this is to not use a regex at all. > Canonically I think this sort of thing is (would be?) implemented by > constructing a 'sequence' of strings, (first filtered based on > potential word length) and recursively filtering based on 'character > at position x' in the string, as characters are entered... the > resulting seqs could be memoized. Isn't 'character at position x' an expensive test too? I have a feeling tries will do better here - I actually started working on a trie-based implementation, then decided to see if it really was the linear regexp scan that was slowing things down, and when it looked like it wasn't, I dropped the idea. I'll complete that and see how it looks. > Then, you could type a letter and quickly update the options based on > the new character at that position (by simply filtering the stored > sequence one more time). When you delete a letter, you run it and > receive the memoized result from earlier. Nice idea. > To display you would want to use 'into-array' and your 'setListData' > function, because you seem to have a homogeneous collection of > strings; while to-array makes you an array of objects, into-array will > return an array of strings (It might help, I'm really not sure). Another good idea. I forgot strings weren't Objects in java. martin -- 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: swing: efficiently updating a listbox from a clojure list
On Nov 25, 11:09 am, Martin DeMello wrote: > On Thu, Nov 26, 2009 at 12:31 AM, Jonathan Smith > > To display you would want to use 'into-array' and your 'setListData' > > function, because you seem to have a homogeneous collection of > > strings; while to-array makes you an array of objects, into-array will > > return an array of strings (It might help, I'm really not sure). > > Another good idea. I forgot strings weren't Objects in java. String *are* Objects. Since the JList method takes an Object[], there's no need to worry about typing of the array, which is all into- array buys you. -- 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: swing: efficiently updating a listbox from a clojure list
On Nov 25, 2:09 pm, Martin DeMello wrote: > On Thu, Nov 26, 2009 at 12:31 AM, Jonathan Smith > > wrote: > > > I think a better way to do this is to not use a regex at all. > > Canonically I think this sort of thing is (would be?) implemented by > > constructing a 'sequence' of strings, (first filtered based on > > potential word length) and recursively filtering based on 'character > > at position x' in the string, as characters are entered... the > > resulting seqs could be memoized. > > Isn't 'character at position x' an expensive test too? I have a > feeling tries will do better here - I actually started working on a > trie-based implementation, then decided to see if it really was the > linear regexp scan that was slowing things down, and when it looked > like it wasn't, I dropped the idea. I'll complete that and see how it > looks. > > > Then, you could type a letter and quickly update the options based on > > the new character at that position (by simply filtering the stored > > sequence one more time). When you delete a letter, you run it and > > receive the memoized result from earlier. > > Nice idea. > > > To display you would want to use 'into-array' and your 'setListData' > > function, because you seem to have a homogeneous collection of > > strings; while to-array makes you an array of objects, into-array will > > return an array of strings (It might help, I'm really not sure). > > Another good idea. I forgot strings weren't Objects in java. > > martin Shouldn't charAt be approximately equivalent to an array dereference? I wouldn't expect it to be terribly expensive. But yeah, tries are perfect for this sort of thing.. (I had assumed we were going for the quick fix). If you already have the trie version 1/2 implemented, it would be best. re: to vs. into: I did an informal test and it looks like to-array is a bit faster invocation-wise than into-array. The reason I said it might be faster to use into-array is because if the array is typed, we then know the type of its contents later, so it might be possible to reduce some reflection if you use that same array elsewhere, and end up using its contents in string manipulation functions (or if swing does that sort of thing at some point). -- 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: Recursions under lazy-seq - how does it work?
Hi, On Nov 25, 9:10 am, Gabi wrote: > For example why doesn't the following "repeatedly" never crash? > > (defn repeatedly > [f] (lazy-seq (cons (f) (repeatedly f Because the nested call is deferred until the sequence is realised. Then the original function already returned. So it does not build up the stack until it crashes. > (last (repeatedly rand)) ;won't crash But it won't return either. Not sure which result is more favorable. ;) Sincerely Meikel -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Question about future
Hi, On Nov 25, 6:04 am, Hong Jiang wrote: > I'm new to Clojure and playing with small programs. Today I wrote a > snippet to figure out how future works: > > (defn testf [] > (let [f (future #(do > (Thread/sleep 5000) > %) > 5) > g 7] > (+ g @f))) > > (println (testf)) > > I'm expecting the program to sleep 5 seconds and then print 12 and > immediately terminate. However, the program seems to print out 12 > immediately, and it takes a long time (more like 50 seconds instead of > 5) to terminate. Am I missing something? future takes a body not a function. So in the new thread, you create a new anonymous function which is immediately thrown away and 5 is returned. This explains why 12 is printed immediately. The shutdown time might be connected to something like shutdown-agents. But I'm not sure on this one. To test the hypothesis you can do something like this: (defn testf [] (let [f (future #(do (Thread/sleep 5000) (+ % 6)) 5) g 7] (+ g @f))) This should print still 12 and not 18. Sincerely Meikel -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
insert-rows
Hi, I am working on reading a pipe delimited file into a SQL database. I can read the lines using (doseq [line (read-lines "myfile.csv")] and format the lines using (map #(format "\"%s\"" %) (re-split #"\|" line)) This results in list for each line. I have created a table that has one column for each value in the list. The values are like ("mark" "stang" "employee"). What I can figure out is how to "convert" the list into something that can be passed to insert-rows. The error I get is that "insert into mytable values ()" I have tried converting the row into a vector (vec (map #(format "\"%s\"" %) (re-split #"\|" line))) Which when I print it, it looks like ["mark" "stang" "employee"] but the insert-rows function isn't seeing it. I have tried doing a let for each line and passing the name, but no such luck. It seems that I could get it to work with insert-values, but that seems like a lot of overhead. I would think I should be able to call insert-rows with the results of reading the entire file. Thoughts? Thanks, Mark -- 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: Recursions under lazy-seq - how does it work?
Very interesting indeed. I am not sure I understand completely, but by intuition I presume that the recursive call actually creates a new heap allocated LazySeq (with the function definition inside) . So is there some help from the compiler for this? How does the recursive call suddenly transfers into a call to a LazySeq object ? On Nov 25, 6:29 pm, David Brown wrote: > On Wed, Nov 25, 2009 at 12:10:36AM -0800, Gabi wrote: > >How come that infinite recursions under lazy-seq won't crash the > >program, whilst regular infinite recursion would crash the program ? > >What's the trick ? > > >For example why doesn't the following "repeatedly" never crash? > > >(defn repeatedly > > [f] (lazy-seq (cons (f) (repeatedly f > > >(last (repeatedly rand)) ;won't crash > > The JVM doesn't support tail call elimination, so an infinite > retursion causes a new stack frame for each call. The stack is fairly > small and will quickly be exhausted. > > The lazy-seq, instead, wraps the contents up into a function closure > and wraps it in a clojure.lang.LazySeq. This causes a few heap > allocations. If you were to keep the head of this chain, you would > also exhaust the heap (which would take a while longer). But since > your example is only walking along next, each item generated contains > no more references, and can be garbage collected. > > Lazy-seq basically turns a stack allocation into a heap allocation. > This both allows for not allow of the sequence to be allocated, but > allows the earlier parts of the sequence to be collected. > > Try: > > (let [r (repeatedly rand)] > (last r) > r) > > which will try to keep the whole sequence in memory, and eventually > exhaust your heap. > > 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: Question about future
Thanks for your replies David and Sean. Yes, I made a mistake thinking that future takes a function and its arguments, so the function was never called in my program. On Nov 25, 8:21 am, David Brown wrote: > On Tue, Nov 24, 2009 at 09:04:38PM -0800, Hong Jiang wrote: > >Hi all, > > >I'm new to Clojure and playing with small programs. Today I wrote a > >snippet to figure out how future works: > > >(defn testf [] > > (let [f (future #(do > > (Thread/sleep 5000) > > %) > > 5) > > g 7] > > (+ g @f))) > > You don't ever evaluate the function containing the sleep, you just > create it, and then immediately return 5. > > Future contains an explicit do, so you can just do the steps in the > future: > > [f (future > (Thread/sleep 5000) > 5) > ... > > Or, if you want to get the function call in, you'll need to call it: > > [f (future (#(do (Thread/sleep 5000) %) 5)) ...] > > 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: Question about future
future also uses the same threadpool as agents, so once you call future the threadpool spins up, and just sort of sits around for a while before the jvm decides to exit, which is why the program would sit around for 50 seconds On Wed, Nov 25, 2009 at 10:30 AM, Hong Jiang wrote: > Thanks for your replies David and Sean. Yes, I made a mistake thinking > that future takes a function and its arguments, so the function was > never called in my program. > > On Nov 25, 8:21 am, David Brown wrote: >> On Tue, Nov 24, 2009 at 09:04:38PM -0800, Hong Jiang wrote: >> >Hi all, >> >> >I'm new to Clojure and playing with small programs. Today I wrote a >> >snippet to figure out how future works: >> >> >(defn testf [] >> > (let [f (future #(do >> > (Thread/sleep 5000) >> > %) >> > 5) >> > g 7] >> > (+ g @f))) >> >> You don't ever evaluate the function containing the sleep, you just >> create it, and then immediately return 5. >> >> Future contains an explicit do, so you can just do the steps in the >> future: >> >> [f (future >> (Thread/sleep 5000) >> 5) >> ... >> >> Or, if you want to get the function call in, you'll need to call it: >> >> [f (future (#(do (Thread/sleep 5000) %) 5)) ...] >> >> 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 -- And what is good, Phaedrus, And what is not good— Need we ask anyone to tell us these things? -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Recursions under lazy-seq - how does it work?
On Wed, Nov 25, 2009 at 09:40:44AM -0800, Gabi wrote: >Very interesting indeed. I am not sure I understand completely, but by >intuition I presume that the recursive call actually creates a new >heap allocated LazySeq (with the function definition inside) . So is >there some help from the compiler for this? How does the recursive >call suddenly transfers into a call to a LazySeq object ? Actually, the compiler doesn't really do anything. lazy-seq is a macro, a very short one at that. I suggest making sure you have cloure.contrib.repl-utils available, and just try: (source lazy-seq) and see for yourselves. The only real compiler magic is that it sets the metadata {:once true} on the function it creates which makes for a bit better code for functions that will only ever be called once. But yeah, (defmacro lazy-seq "..." [& body] (list 'new 'clojure.lang.LazySeq (list* '#^{:once true} fn* [] body))) That's it. The neat thing is that you or I can also write macros that do this kind of thing. It's one of the things that makes Lisp so powerful. 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: Recursions under lazy-seq - how does it work?
Ok. Now I get it. Cool stuff On Nov 25, 4:18 pm, Meikel Brandmeyer wrote: > Hi, > > On Nov 25, 9:10 am, Gabi wrote: > > > For example why doesn't the following "repeatedly" never crash? > > > (defn repeatedly > > [f] (lazy-seq (cons (f) (repeatedly f > > Because the nested call is deferred until the sequence is realised. > Then the original function already returned. So it does not build up > the stack until it crashes. > > > (last (repeatedly rand)) ;won't crash > > But it won't return either. Not sure which result is more > favorable. ;) > > Sincerely > Meikel -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: The specificity of bound-fn: capturing specific dynamic vars
Hi, Am 23.11.2009 um 21:46 schrieb Garth Sheldon-Coulson: bound-fn is very nice, but I've found that capturing *all* dynamic vars is often overkill. As I said in said thread: I don't think, that this is a big problem. The number Vars having actually a non-root binding should be rather small in the general case. So the saving is not tht large. (For the case file: I asked exactly this in the assembla ticket, but got no answer.) In fact, I would argue (subject to persuasion otherwise) that capturing all dynamic vars should be the very rare exception rather than the rule. In most cases, it seems to me that a developer should capture just the dynamic vars that he or she has defined in that particular app, allowing other dynamic vars to reset in new threads as they otherwise would. Another library might expect this resetting of its dynamic vars, or the user might expect it of Clojure's default bindings. Hmm... I see your point, but I'm not sure. I think, the environment should be consisted. If I don't use the other Vars, then there is no problem. If I use them, I'd expect them to not change under the code... Maybe I prefer the current solution. Analogously there could be a bound-lazy-seq-with (or whatever name) for defining lazy seqs bound over dynamic vars. bound-seq might be a nice complement to bound-fn. Rich? Sincerely Meikel smime.p7s Description: S/MIME cryptographic signature
Re: Recursions under lazy-seq - how does it work?
On Wed, Nov 25, 2009 at 12:40 PM, Gabi wrote: > Very interesting indeed. I am not sure I understand completely, but by > intuition I presume that the recursive call actually creates a new > heap allocated LazySeq (with the function definition inside) . Not quite; it creates a Java object one method of which calls your function to generate a) the next element and b) another such Java object, which in turn would be used to generate the *next* element, and so on. When you walk the sequence, you get a series of sequential method calls to these objects in the JVM, one after the next (not nested), so no stack overflow. Well, except that when you repeatedly transform lazy sequences or wrap lazy sequences in lazy sequences (most usually a problem with (reduce (mapcat ...)) and (reduce (filter ..., the nesting depth of calls can get high enough to be a problem as each lazy sequence call wraps a call to some transformed sequence, which wraps another, which wraps another... -- 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: Recursions under lazy-seq - how does it work?
Hi, Am 25.11.2009 um 22:43 schrieb David Brown: (defmacro lazy-seq "..." [& body] (list 'new 'clojure.lang.LazySeq (list* '#^{:once true} fn* [] body))) That's it. The neat thing is that you or I can also write macros that do this kind of thing. It's one of the things that makes Lisp so powerful. But please don't write them in this style. This is done only, because syntax-quote is not yet fully available at the point in core.clj. A good style macro would like this: (defmacro lazy-seq "..." [& body] `(LazySeq. ~(with-meta `fn* {:once true}) [] ~...@body)) (Assuming LazySeq was :imported...) Sincerely Meikel smime.p7s Description: S/MIME cryptographic signature
Re: Question about future
Thanks. Yeah, after adding a call to shutdown-agents, the process no longer hangs. On Nov 25, 1:15 pm, Kevin Downey wrote: > future also uses the same threadpool as agents, so once you call > future the threadpool spins up, and just sort of sits around for a > while before the jvm decides to exit, which is why the program would > sit around for 50 seconds > > > > On Wed, Nov 25, 2009 at 10:30 AM, Hong Jiang wrote: > > Thanks for your replies David and Sean. Yes, I made a mistake thinking > > that future takes a function and its arguments, so the function was > > never called in my program. > > > On Nov 25, 8:21 am, David Brown wrote: > >> On Tue, Nov 24, 2009 at 09:04:38PM -0800, Hong Jiang wrote: > >> >Hi all, > > >> >I'm new to Clojure and playing with small programs. Today I wrote a > >> >snippet to figure out how future works: > > >> >(defn testf [] > >> > (let [f (future #(do > >> > (Thread/sleep 5000) > >> > %) > >> > 5) > >> > g 7] > >> > (+ g @f))) > > >> You don't ever evaluate the function containing the sleep, you just > >> create it, and then immediately return 5. > > >> Future contains an explicit do, so you can just do the steps in the > >> future: > > >> [f (future > >> (Thread/sleep 5000) > >> 5) > >> ... > > >> Or, if you want to get the function call in, you'll need to call it: > > >> [f (future (#(do (Thread/sleep 5000) %) 5)) ...] > > >> 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 > > -- > And what is good, Phaedrus, > And what is not good— > Need we ask anyone to tell us these things? -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Recursions under lazy-seq - how does it work?
On Wed, Nov 25, 2009 at 11:01:32PM +0100, Meikel Brandmeyer wrote: >(defmacro lazy-seq > "..." > [& body] > `(LazySeq. ~(with-meta `fn* {:once true}) [] ~...@body)) It's also probably good to explain why fn* is quoted with backquote rather than a regular quote. This took me a while to figure out, and I've only found an explanation inside of one of Rich's talks. A lot to learn even from the REPL, just by doing: => 'list list => `list clojure.core/list As I understand it, the backquoted one will be qualified in the namespace the macro is in and not be "bothered" by the user's namespace having something defined under that name as well. 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
FileNotFoundException (Access is denied)
I'm trying to write a file scanner very similar to the one on page 131 of Stuart's book: (ns user (:use [clojure.contrib.duck-streams :only [reader]])) (defn scan [dir] (for [file (file-seq dir)] (with-open [rdr (reader file)] (count (filter #(re-find #"foobar" %) (line-seq rdr)) user> (scan (java.io.File. "C:/SomeValidDir")) java.lang.RuntimeException: java.io.FileNotFoundException: C:\publishing\cosmos\trunk\web (Access is denied) (NO_SOURCE_FILE:0) The strange part: user> (file-seq (java.io.File. "C:/SomeValidDir")) ( # #) and so on, properly listing the entire contents of the directory. user> (for [file (file-seq (java.io.File. "C:/publishing/cosmos/trunk/web"))] nil) (nil nil) and so on, again working properly. user> (for [file (file-seq (java.io.File. "C:/publishing/cosmos/trunk/web"))] (with-open [rdr (reader file)] nil)) ; Evaluation aborted. (this one finally fails with the same error) So "with-open [rdr (reader file)]" is the problem, but why? -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: FileNotFoundException (Access is denied)
Forgot to mention: running Clojure 1.0.0- and Clojure-Contrib 1.0-SNAPSHOT according to the pom.. On Thu, Nov 26, 2009 at 7:38 AM, Robert Campbell wrote: > I'm trying to write a file scanner very similar to the one on page 131 > of Stuart's book: > > (ns user > (:use [clojure.contrib.duck-streams :only [reader]])) > > (defn scan [dir] > (for [file (file-seq dir)] > (with-open [rdr (reader file)] > (count (filter #(re-find #"foobar" %) (line-seq rdr)) > > user> (scan (java.io.File. "C:/SomeValidDir")) > > java.lang.RuntimeException: java.io.FileNotFoundException: > C:\publishing\cosmos\trunk\web (Access is denied) (NO_SOURCE_FILE:0) > > The strange part: > > user> (file-seq (java.io.File. "C:/SomeValidDir")) > ( # #) > and so on, properly listing the entire contents of the directory. > > user> (for [file (file-seq (java.io.File. > "C:/publishing/cosmos/trunk/web"))] nil) > (nil nil) and so on, again working properly. > > user> (for [file (file-seq (java.io.File. > "C:/publishing/cosmos/trunk/web"))] (with-open [rdr (reader file)] > nil)) > ; Evaluation aborted. (this one finally fails with the same error) > > So "with-open [rdr (reader file)]" is the problem, but why? > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Question about future
If you have this: user> (def f (future (Thread/sleep 2) :done)) #'user/f user> @f ; this immediate deref blocks for 20 sec, finally returning :block :done user> @f ; returns immediately :done What is actually happening when you call the first @f? You are waiting for the function to finish executing and return a value? Or are you waiting for a return value to appear in the ref slot? Is there any way to check if there is a value ready or if the function has returned w/out blocking? On Thu, Nov 26, 2009 at 12:32 AM, Hong Jiang wrote: > Thanks. Yeah, after adding a call to shutdown-agents, the process no > longer hangs. > > On Nov 25, 1:15 pm, Kevin Downey wrote: >> future also uses the same threadpool as agents, so once you call >> future the threadpool spins up, and just sort of sits around for a >> while before the jvm decides to exit, which is why the program would >> sit around for 50 seconds >> >> >> >> On Wed, Nov 25, 2009 at 10:30 AM, Hong Jiang wrote: >> > Thanks for your replies David and Sean. Yes, I made a mistake thinking >> > that future takes a function and its arguments, so the function was >> > never called in my program. >> >> > On Nov 25, 8:21 am, David Brown wrote: >> >> On Tue, Nov 24, 2009 at 09:04:38PM -0800, Hong Jiang wrote: >> >> >Hi all, >> >> >> >I'm new to Clojure and playing with small programs. Today I wrote a >> >> >snippet to figure out how future works: >> >> >> >(defn testf [] >> >> > (let [f (future #(do >> >> > (Thread/sleep 5000) >> >> > %) >> >> > 5) >> >> > g 7] >> >> > (+ g @f))) >> >> >> You don't ever evaluate the function containing the sleep, you just >> >> create it, and then immediately return 5. >> >> >> Future contains an explicit do, so you can just do the steps in the >> >> future: >> >> >> [f (future >> >> (Thread/sleep 5000) >> >> 5) >> >> ... >> >> >> Or, if you want to get the function call in, you'll need to call it: >> >> >> [f (future (#(do (Thread/sleep 5000) %) 5)) ...] >> >> >> 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 >> >> -- >> And what is good, Phaedrus, >> And what is not good— >> Need we ask anyone to tell us these things? > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with your > first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en -- 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: Question about future
On Thu, Nov 26, 2009 at 8:05 AM, Robert Campbell wrote: > If you have this: > > user> (def f (future (Thread/sleep 2) :done)) > #'user/f > user> @f ; this immediate deref blocks for 20 sec, finally returning > :block > :done > user> @f ; returns immediately > :done > > What is actually happening when you call the first @f? You are waiting > for the function to finish executing and return a value? Or are you > waiting for a return value to appear in the ref slot? > > Is there any way to check if there is a value ready or if the function > has returned w/out blocking? > future-done? user=> (def f (future (Thread/sleep 2) :done)) #'user/f user=> (future-done? f) false user=> @f :done user=> (future-done? f) true Christophe -- 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