Re: StackOverflowError
Hi Ru, Think of it this way...In Java you can't declare something as public from within a method. Ok, in Java this would break the object's contract but even if you look at languages like python, you still don't define global variables from within functions. I have seen python code where functions mutate some global but never defining a global. THe reasons are manyfold...For starters, any function that defines global vars should not be called a function. Upon invocation it has an immediate impact on the outside world and therefore you lose all the nice properties of pure functions. Secondly, if you define global vars from functions, presumably other functions will depend on these globals and so you can immediately see you've introduced time in you program, and the world is generally much simpler and testable without time. So basically, if you absolutely need mutable globals then the way to do it is to have a top-level 'def' that points to one of Clojure's reference types (atom, ref, agent etc). Alternatively, you could have a dynamic var that can be rebound under the 'binding' scope. That said, I am not a big fan of dynamic-bindings and rarely (to never) use them... hope that helps a bit, Jim On 08/11/13 14:43, Dimitrios Jim Piliouras wrote: You don't generally do global variable assignment in Clojure...it is seriously frowned upon and goes against the functional paradigm... I need to run now and I will be busy for the next 2 hours... perhaps someone else can explain if you are in a hurry? On Nov 8, 2013 2:34 PM, ru soro...@oogis.ru mailto:soro...@oogis.ru wrote: Jim, please, recommend a correct solution for global variable assignment. John, Indeed I would prefere technique with better performance. But, as I understand from scarce documentation, disj works with sets. And doesn't it recursive? пятница, 8 ноября 2013 г., 13:17:28 UTC+4 пользователь ru написал: Dear clojure-users, StackOverflowError clojure.lang.RT.nthFrom (RT.java:789) I got this error message during evaluation of count function call on a quite short list (about 200 elements). What can be reasons of the error. Any hints would be greatly appreciated. Sincerely, Ru -- -- 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 mailto: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 mailto:clojure%2bunsubscr...@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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com mailto:clojure%2bunsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
Yes, it is. I reworked my code and removed where it is possible global assignments through *def*, particulary, from the function *retract-fact*. But, I can not do without global mutations altogether, so I have to use atom, ref, agent etc., as you suggested. But, do mutation of these structures have better performance than simple reassignment of def? I understand that from theoretical point of view assignments, states, mutation ,.. etc. break functional cleanliness, but to solve specific partial tasks can we mix differnt even conflicting paradigms to gain maximum performance. Or not? суббота, 9 ноября 2013 г., 13:47:54 UTC+3 пользователь Jim foo.bar написал: Hi Ru, Think of it this way...In Java you can't declare something as public from within a method. Ok, in Java this would break the object's contract butО©╫ even if you look at languages like python, you still don't define global variables from within functions. I have seen python code where functions mutate some global but never defining a global. THe reasons are manyfold...For starters, any function that defines global vars should not be called a function. Upon invocation it has an immediate impact on the outside world and therefore you lose all the nice properties of pure functions. Secondly, if you define global vars from functions, presumably other functions will depend on these globals and so you can immediately see you've introduced time in you program, and the world is generally much simpler and testable without time. So basically, if you absolutely need mutable globals then the way to do it is to have a top-level 'def' that points to one of Clojure's reference types (atom, ref, agent etc). Alternatively, you could have a dynamic var that can be rebound under the 'binding' scope. That said, I am not a big fan of dynamic-bindings and rarely (to never) use them... hope that helps a bit, Jim On 08/11/13 14:43, Dimitrios Jim Piliouras wrote: You don't generally do global variable assignment in Clojure...it is seriously frowned upon and goes against the functional paradigm... I need to run now and I will be busy for the next 2 hours... perhaps someone else can explain if you are in a hurry? On Nov 8, 2013 2:34 PM, ru sor...@oogis.ru javascript: wrote: Jim, please, recommend a correct solution for global variable assignment. John, Indeed I would prefere technique with better performance. But, as I understand from scarce documentation, disj works with sets. And doesn't it recursive? О©╫О©╫О©╫О©╫О©╫О©╫О©╫, 8 О©╫О©╫О©╫О©╫О©╫О©╫ 2013О©╫О©╫., 13:17:28 UTC+4 О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫О©╫ ru О©╫О©╫О©╫О©╫О©╫О©╫О©╫: Dear clojure-users, StackOverflowError О©╫ clojure.lang.RT.nthFrom (RT.java:789) I got this error message during evaluation of count function call on a quite short list (about 200 elements). What can be reasons of the error. Any hints would be greatly appreciated. Sincerely, О©╫ RuО©╫ -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clo...@googlegroups.comjavascript: Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+u...@googlegroups.com javascript: 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com javascript:. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
I cannot reproduce your result: user= (- 500 range doall count) 500 user= (- 500 range count) 500 Could you provide more details? How exactly are you calling 'count' ? Jim On 08/11/13 09:17, ru wrote: Dear clojure-users, StackOverflowError clojure.lang.RT.nthFrom (RT.java:789) I got this error message during evaluation of count function call on a quite short list (about 200 elements). What can be reasons of the error. Any hints would be greatly appreciated. Sincerely, Ru -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
Hi Jim, I forget to say that call to count have been done in not a bare repl, but inside a quite complex program after it did a lot of work on a quite big data. But, after long and profound analysis of source code I did not found any places with deep recursive calls :( Something like stack is filling against my will...? пятница, 8 ноября 2013 г., 13:17:28 UTC+4 пользователь ru написал: Dear clojure-users, StackOverflowError clojure.lang.RT.nthFrom (RT.java:789) I got this error message during evaluation of count function call on a quite short list (about 200 elements). What can be reasons of the error. Any hints would be greatly appreciated. Sincerely, Ru -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
On 08/11/13 10:11, ru wrote: I forget to say that call to count have been done in not a bare repl, but inside a quite complex program after it did a lot of work on a quite big data. so your problem does not lie with 'count' but with something else...actually 'count' doesn't hold on to the head of the seq so even tough sometimes it will take a long time (for large seqs), it will never StackOverflow: user= (- 5 range count) ;;after quite some time 5 user= But, after long and profound analysis of source code I did not found any places with deep recursive calls :( Something like stack is filling against my will...? there must be something else causing this...have you tried (clojure.stacktrace/e) right after the exception? 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
I tryed to add a call (clojure.stacktrace/e) to the code after the call to count like this: ... (if TRACE (println [:ЩАС])) (if TRACE (do (println [:OFACTS (count ofacts)]) (clojure.stacktrace/e))) And what a result I have got: ... [:ЩАС] StackOverflowError clojure.lang.RT.nth (RT.java:764) rete.core= I am afraid, that I am not sure where to put (clojure.stacktrace/e) to make it work :( Or, may be this is not an exception? пятница, 8 ноября 2013 г., 13:17:28 UTC+4 пользователь ru написал: Dear clojure-users, StackOverflowError clojure.lang.RT.nthFrom (RT.java:789) I got this error message during evaluation of count function call on a quite short list (about 200 elements). What can be reasons of the error. Any hints would be greatly appreciated. Sincerely, Ru -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
On Fri, Nov 8, 2013 at 5:11 AM, ru soro...@oogis.ru wrote: Hi Jim, I forget to say that call to count have been done in not a bare repl, but inside a quite complex program after it did a lot of work on a quite big data. But, after long and profound analysis of source code I did not found any places with deep recursive calls :( Something like stack is filling against my will...? Are you using concat, remove, or filter to build or modify a sequence? If so, you can run into a StackOverflow error if you build a long chain of operations and then try to process the whole sequence. -John -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
On 08/11/13 10:46, ru wrote: I am afraid, that I am not sure where to put (clojure.stacktrace/e) to make it work :( Or, may be this is not an exception? use (clojure.stacktrace/e) after the exception has been thrown to get some more information than just a line. Don't put it in your code, but rather call it on the repl right after the error. It will probably point you to some more places to have a look. Also, as John suggested, if you've got a lot of lazy operations that might be the cause. Have you looked at reducers? 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
In this line: (if TRACE (do (println [:OFACTS (count ofacts)]) (clojure.stacktrace/e))) the StackOverflowError must be occurring in the evaluation of ofacts. The error breaks out of the evaluation, preventing (clojure.stacktrace/e) from being evaluated. The error sends you back to the Clojure repl prompt, as you showed: StackOverflowError clojure.lang.RT.nth (RT.java:764) rete.core= Here, at the rete.core= prompt after the error, is where you want to call (clojure.stacktrace/e). - Bruce On Fri, Nov 8, 2013, at 06:10 AM, Jim wrote: On 08/11/13 10:46, ru wrote: I am afraid, that I am not sure where to put (clojure.stacktrace/e) to make it work :( Or, may be this is not an exception? use (clojure.stacktrace/e) after the exception has been thrown to get some more information than just a line. Don't put it in your code, but rather call it on the repl right after the error. It will probably point you to some more places to have a look. Also, as John suggested, if you've got a lot of lazy operations that might be the cause. Have you looked at reducers? 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
On Friday, November 8, 2013 11:11:14 AM UTC+1, ru wrote: Hi Jim, I forget to say that call to count have been done in not a bare repl, but inside a quite complex program after it did a lot of work on a quite big data. But, after long and profound analysis of source code I did not found any places with deep recursive calls :( Something like stack is filling against my will...? Recursion happens kinda invisibly with lazy seqs and may blow your stack if you hold on to your head (search for that term). So that would be something to analyze, too. -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
Thank you, all, for the very useful information. Stacktrace after StackOverflowError turns out: StackOverflowError clojure.lang.RT.nth (RT.java:764) rete.core= (clojure.stacktrace/e) java.lang.StackOverflowError: null at clojure.lang.RT.nth (RT.java:764) rete.core$fact_id.invoke (core.clj:403) rete.core$retract_fact$fn__1902.invoke (core.clj:633) clojure.core$complement$fn__4048.invoke (core.clj:1357) clojure.core$filter$fn__4226.invoke (core.clj:2533) clojure.lang.LazySeq.sval (LazySeq.java:42) clojure.lang.LazySeq.seq (LazySeq.java:60) clojure.lang.RT.seq (RT.java:484) nil rete.core= This list with a problem *ofacts* was been intensively updated by *cons* and *remove* operations (more precisely, *rete.core/retract-fact* function visible in stacktrace contains a call to *remove*). Thus, probably, John is nearer then others to solution. So, the question is: What can I do to avoid recursive *remove* operation? May be I should rewrite it using *loop*? Does this help? Or recursion remains inside *loop*? пятница, 8 ноября 2013 г., 13:17:28 UTC+4 пользователь ru написал: Dear clojure-users, StackOverflowError clojure.lang.RT.nthFrom (RT.java:789) I got this error message during evaluation of count function call on a quite short list (about 200 elements). What can be reasons of the error. Any hints would be greatly appreciated. Sincerely, Ru -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
ok we are getting somewhere...is it easy to post the source of your 'retract-fact' 'fact_id' fns? Jim On 08/11/13 13:17, ru wrote: Thank you, all, for the very useful information. Stacktrace after StackOverflowError turns out: StackOverflowError clojure.lang.RT.nth (RT.java:764) rete.core= (clojure.stacktrace/e) java.lang.StackOverflowError: null at clojure.lang.RT.nth (RT.java:764) rete.core$fact_id.invoke (core.clj:403) rete.core$retract_fact$fn__1902.invoke (core.clj:633) clojure.core$complement$fn__4048.invoke (core.clj:1357) clojure.core$filter$fn__4226.invoke (core.clj:2533) clojure.lang.LazySeq.sval (LazySeq.java:42) clojure.lang.LazySeq.seq (LazySeq.java:60) clojure.lang.RT.seq (RT.java:484) nil rete.core= This list with a problem *ofacts* was been intensively updated by *cons* and *remove* operations (more precisely, *rete.core/retract-fact* function visible in stacktrace contains a call to *remove*). Thus, probably, John is nearer then others to solution. So, the question is: What can I do to avoid recursive *remove* operation? May be I should rewrite it using *loop*? Does this help? Or recursion remains inside *loop*? пятница, 8 ноября 2013 г., 13:17:28 UTC+4 пользователь ru написал: Dear clojure-users, StackOverflowError clojure.lang.RT.nthFrom (RT.java:789) I got this error message during evaluation of count function call on a quite short list (about 200 elements). What can be reasons of the error. Any hints would be greatly appreciated. Sincerely, Ru -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
Solution have found: (remove ...) = (doall (remove ...)) Full source of 'retract-fact' function is: (defn retract-fact [fid] Retract fact for given fact-id by removing it from alpha, beta and fact memory, and also by removing from conflict set activations, containing this fact-id ;;(println [:RETRACT-FACT fid]) (if-let [frame (remove-fmem fid)] (let [[typ mp] frame ais (a-indices typ mp)] (if TRACE (println [:== [typ mp] :id fid])) (doseq [ai ais] (set-amem ai (doall (remove #(= (fact-id %) fid) (amem ai)) ) )) (retract-b fid (.get =FIDS= fid)) (def =CFSET= (filter #(not (some #{fid} (:FIDS (second % =CFSET=)) (.remove =FIDS= fid) frame))) Thank you very much once more! Best regards, Ru пятница, 8 ноября 2013 г., 13:17:28 UTC+4 пользователь ru написал: Dear clojure-users, StackOverflowError clojure.lang.RT.nthFrom (RT.java:789) I got this error message during evaluation of count function call on a quite short list (about 200 elements). What can be reasons of the error. Any hints would be greatly appreciated. Sincerely, Ru -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
Did I see a def inside a defn? This is weird... glad you solved your problem though... On Nov 8, 2013 1:51 PM, ru soro...@oogis.ru wrote: Solution have found: (remove ...) = (doall (remove ...)) Full source of 'retract-fact' function is: (defn retract-fact [fid] Retract fact for given fact-id by removing it from alpha, beta and fact memory, and also by removing from conflict set activations, containing this fact-id ;;(println [:RETRACT-FACT fid]) (if-let [frame (remove-fmem fid)] (let [[typ mp] frame ais (a-indices typ mp)] (if TRACE (println [:== [typ mp] :id fid])) (doseq [ai ais] (set-amem ai (doall (remove #(= (fact-id %) fid) (amem ai)) ) )) (retract-b fid (.get =FIDS= fid)) (def =CFSET= (filter #(not (some #{fid} (:FIDS (second % =CFSET=)) (.remove =FIDS= fid) frame))) Thank you very much once more! Best regards, Ru пятница, 8 ноября 2013 г., 13:17:28 UTC+4 пользователь ru написал: Dear clojure-users, StackOverflowError clojure.lang.RT.nthFrom (RT.java:789) I got this error message during evaluation of count function call on a quite short list (about 200 elements). What can be reasons of the error. Any hints would be greatly appreciated. Sincerely, Ru -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
On Fri, Nov 8, 2013 at 8:51 AM, ru soro...@oogis.ru wrote: Solution have found: (remove ...) = (doall (remove ...)) Be careful with this technique. It can easily make your O(n) algorithm O(n^2). It may be better to use data structures that you can use with conj and disj if you need to maintain performance. -John -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
Jim, please, recommend a correct solution for global variable assignment. John, Indeed I would prefere technique with better performance. But, as I understand from scarce documentation, disj works with sets. And doesn't it recursive? пятница, 8 ноября 2013 г., 13:17:28 UTC+4 пользователь ru написал: Dear clojure-users, StackOverflowError clojure.lang.RT.nthFrom (RT.java:789) I got this error message during evaluation of count function call on a quite short list (about 200 elements). What can be reasons of the error. Any hints would be greatly appreciated. Sincerely, Ru -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
You don't generally do global variable assignment in Clojure...it is seriously frowned upon and goes against the functional paradigm... I need to run now and I will be busy for the next 2 hours... perhaps someone else can explain if you are in a hurry? On Nov 8, 2013 2:34 PM, ru soro...@oogis.ru wrote: Jim, please, recommend a correct solution for global variable assignment. John, Indeed I would prefere technique with better performance. But, as I understand from scarce documentation, disj works with sets. And doesn't it recursive? пятница, 8 ноября 2013 г., 13:17:28 UTC+4 пользователь ru написал: Dear clojure-users, StackOverflowError clojure.lang.RT.nthFrom (RT.java:789) I got this error message during evaluation of count function call on a quite short list (about 200 elements). What can be reasons of the error. Any hints would be greatly appreciated. Sincerely, Ru -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError
No, I am not in a hurry :) Thanks in advance. пятница, 8 ноября 2013 г., 13:17:28 UTC+4 пользователь ru написал: Dear clojure-users, StackOverflowError clojure.lang.RT.nthFrom (RT.java:789) I got this error message during evaluation of count function call on a quite short list (about 200 elements). What can be reasons of the error. Any hints would be greatly appreciated. Sincerely, Ru -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: StackOverflowError caused by apply-template
I think that falls under the heading of don't do that. :) I see. Actually, I found the error when I just used clojure.test, like this: (clojure.test/are [x y] (= (f x) y) 'x 'y) ;= StackOverflowError I think it's not as much obvious as clojure.template's case. At that time, I didn't know that clojure.test uses clojure.template and why the error was raised. On Apr 23, 10:13 am, Stuart Sierra the.stuart.sie...@gmail.com wrote: I think that falls under the heading of don't do that. :) clojure.template (which I wrote) wasn't a great idea to begin with. It was slightly useful in clojure.test, but I haven't found a use for it since. -S On Sunday, April 22, 2012 8:02:45 AM UTC-4, Shogo Ohta wrote: Hi, I've run into such an error: (clojure.template/apply-template '[x] 'x '[[x]]) ;= StackOverflowError It appears to be caused by replacing x with [x] infinitely recursively. Is it a bug or spec? -- 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: StackOverflowError caused by apply-template
I think that falls under the heading of don't do that. :) clojure.template (which I wrote) wasn't a great idea to begin with. It was slightly useful in clojure.test, but I haven't found a use for it since. -S On Sunday, April 22, 2012 8:02:45 AM UTC-4, Shogo Ohta wrote: Hi, I've run into such an error: (clojure.template/apply-template '[x] 'x '[[x]]) ;= StackOverflowError It appears to be caused by replacing x with [x] infinitely recursively. Is it a bug or spec? -- 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: StackOverflowError in prime function
Hi, Am Donnerstag, 22. September 2011 17:43:30 UTC+2 schrieb Alf: What am I doing wrong here, will filter/remove or something else give StackOverflowError when used incorrectly? Yes. You pile lazy seq on lazy seq on lazy seq on and then realise the first element. This kicks off a cascade which finally causes the stack overflow when your lazy seq pile is large enough. Put a doall around the remove. This will realise the seq immediatelly and the cascade cannot happen. So it should solve the issue. 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: StackOverflowError in prime function
Jup, that solved the problem. Lesson learned. Thanks :) Alf On Thu, Sep 22, 2011 at 17:54, Meikel Brandmeyer (kotarak) m...@kotka.de wrote: Hi, Am Donnerstag, 22. September 2011 17:43:30 UTC+2 schrieb Alf: What am I doing wrong here, will filter/remove or something else give StackOverflowError when used incorrectly? Yes. You pile lazy seq on lazy seq on lazy seq on and then realise the first element. This kicks off a cascade which finally causes the stack overflow when your lazy seq pile is large enough. Put a doall around the remove. This will realise the seq immediatelly and the cascade cannot happen. So it should solve the issue. 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 -- 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: StackOverflowError in prime function
Alf, I mean it will be even better if you just generate an infinite lazy sequence that generates primes, I mean will be really more cool :) --- Wilker Lúcio http://about.me/wilkerlucio/bio Kajabi Consultant +55 81 82556600 On Thu, Sep 22, 2011 at 1:36 PM, Alf Kristian Støyle alf.krist...@gmail.com wrote: Jup, that solved the problem. Lesson learned. Thanks :) Alf On Thu, Sep 22, 2011 at 17:54, Meikel Brandmeyer (kotarak) m...@kotka.de wrote: Hi, Am Donnerstag, 22. September 2011 17:43:30 UTC+2 schrieb Alf: What am I doing wrong here, will filter/remove or something else give StackOverflowError when used incorrectly? Yes. You pile lazy seq on lazy seq on lazy seq on and then realise the first element. This kicks off a cascade which finally causes the stack overflow when your lazy seq pile is large enough. Put a doall around the remove. This will realise the seq immediatelly and the cascade cannot happen. So it should solve the issue. 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 -- 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: StackOverflowError in prime function
Well, why reinvent the wheel :) https://github.com/richhickey/clojure-contrib/blob/78ee9b3e64c5ac6082fb223fc79292175e8e4f0c/src/main/clojure/clojure/contrib/lazy_seqs.clj#L66 Cheers, Alf On Thu, Sep 22, 2011 at 19:20, Wilker wilkerlu...@gmail.com wrote: Alf, I mean it will be even better if you just generate an infinite lazy sequence that generates primes, I mean will be really more cool :) --- Wilker Lúcio http://about.me/wilkerlucio/bio Kajabi Consultant +55 81 82556600 On Thu, Sep 22, 2011 at 1:36 PM, Alf Kristian Støyle alf.krist...@gmail.com wrote: Jup, that solved the problem. Lesson learned. Thanks :) Alf On Thu, Sep 22, 2011 at 17:54, Meikel Brandmeyer (kotarak) m...@kotka.de wrote: Hi, Am Donnerstag, 22. September 2011 17:43:30 UTC+2 schrieb Alf: What am I doing wrong here, will filter/remove or something else give StackOverflowError when used incorrectly? Yes. You pile lazy seq on lazy seq on lazy seq on and then realise the first element. This kicks off a cascade which finally causes the stack overflow when your lazy seq pile is large enough. Put a doall around the remove. This will realise the seq immediatelly and the cascade cannot happen. So it should solve the issue. 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 -- 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: StackOverflowError in prime function
FWIW there's a lot of prior threads about this problem in various guises. The one I like to refer people to is on stackoverflow, and is for exactly what you're doing: http://stackoverflow.com/questions/2946764/recursive-function-causing-a-stack-overflow On Sep 22, 9:36 am, Alf Kristian Støyle alf.krist...@gmail.com wrote: Jup, that solved the problem. Lesson learned. Thanks :) Alf On Thu, Sep 22, 2011 at 17:54, Meikel Brandmeyer (kotarak) m...@kotka.de wrote: Hi, Am Donnerstag, 22. September 2011 17:43:30 UTC+2 schrieb Alf: What am I doing wrong here, will filter/remove or something else give StackOverflowError when used incorrectly? Yes. You pile lazy seq on lazy seq on lazy seq on and then realise the first element. This kicks off a cascade which finally causes the stack overflow when your lazy seq pile is large enough. Put a doall around the remove. This will realise the seq immediatelly and the cascade cannot happen. So it should solve the issue. 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 -- 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: StackOverflowError with lazy qsort of The Joy of Clojure
Looking at the stack trace, I suspect this is the old problem of layering too many filters on top of the same seq. If I understand the issue correctly, when you have enough layers of filter on top of a seq, when you finally try to access elements, as it evaluates each layer, it is going to be making a call for each layer of filter. With enough layers, you'll blow your stack. I've seen people solve these issues by forcing the intermediate seqs, but that doesn't work well for a lazy situation such as this. On Aug 26, 7:42 am, Tassilo Horn tass...@member.fsf.org wrote: Hi all, I'm toying around with the lazy, tail-recursive quick-sort implementation Michael Fogus and Chris Houser present in their book The Joy of Clojure: --8---cut here---start-8--- (in-ns 'user) (defn sort-parts Lazy, tail-recursive, incremental quicksort. Works against and creates partitions based on the pivot, defined as 'work'. [work] (lazy-seq (loop [[part parts] work] (if-let [[pivot xs] (seq part)] (let [smaller? #( % pivot)] (recur (list* (filter smaller? xs) pivot (remove smaller? xs) parts))) (when-let [[x parts] parts] (cons x (sort-parts parts))) (defn qsort [xs] (sort-parts (list xs))) -- 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: StackOverflowError with lazy qsort of The Joy of Clojure
Paul Mooser taron...@gmail.com writes: Hi Paul, Looking at the stack trace, I suspect this is the old problem of layering too many filters on top of the same seq. If I understand the issue correctly, when you have enough layers of filter on top of a seq, when you finally try to access elements, as it evaluates each layer, it is going to be making a call for each layer of filter. With enough layers, you'll blow your stack. Do you have a link to the issue? I've tried searching for filter or layer at dev.clojure.org, but I can't find anything... I've seen people solve these issues by forcing the intermediate seqs, but that doesn't work well for a lazy situation such as this. Indeed, putting a doall around the filter and remove seems to prevent the stack overflow. Bye, Tassilo -- 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: StackOverflowError with lazy qsort of The Joy of Clojure
Tassilo Horn tass...@member.fsf.org writes: I've seen people solve these issues by forcing the intermediate seqs, but that doesn't work well for a lazy situation such as this. Indeed, putting a doall around the filter and remove seems to prevent the stack overflow. A better solution seems to be to use a sequence comprehension: --8---cut here---start-8--- (defn sort-parts Lazy, tail-recursive, incremental quicksort. Works against and creates partitions based on the pivot, defined as 'work'. [work] (lazy-seq (loop [[part parts] work] (if-let [[pivot xs] (seq part)] (let [smaller? #( % pivot)] (recur (list* (for [x xs :when (smaller? x)] x) pivot (for [x xs :when ((complement smaller?) x)] x) parts))) (when-let [[x parts] parts] (cons x (sort-parts parts))) (defn qsort [xs] (sort-parts (list xs))) --8---cut here---end---8--- It also seems to perform a bit better than the original solution. However, I cannot see any big difference in speed compared to my simple straight-forward solution: --8---cut here---start-8--- (defn qsort2 [xs] (lazy-seq (when (seq xs) (let [[p r] xs] (concat (qsort (for [y r :when ( y p)] y)) [p] (qsort (for [y r :when (= y p)] y))) --8---cut here---end---8--- Bye, Tassilo -- 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: StackOverflowError with lazy qsort of The Joy of Clojure
If you search for filter and StackOverflowError in this group, you will find people discussing related issues. On Aug 26, 10:30 am, Tassilo Horn tass...@member.fsf.org wrote: Do you have a link to the issue? I've tried searching for filter or layer at dev.clojure.org, but I can't find anything... -- 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: StackOverflowError with lazy qsort of The Joy of Clojure
Paul Mooser taron...@gmail.com writes: Hi Paul, If you search for filter and StackOverflowError in this group, you will find people discussing related issues. Thanks, I've found some explanation by Meikel Brandmeier who explains the layering issue. But do we really have to live with that? I mean, replacing the filter/remove with a list comprehension solved the issue for me. And it's easy to define filter in terms of for. --8---cut here---start-8--- user (defn philter [pred coll] (for [x coll :when (pred x)] x)) #'user/philter user (filter even? (take 10 (iterate inc 0))) (0 2 4 6 8) user (philter even? (take 10 (iterate inc 0))) (0 2 4 6 8) --8---cut here---end---8--- I've tried benchmarking filter vs. philter with seqs of different sizes, and there seems to be no difference. So why not simply do it that way? Bye, Tassilo -- 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: StackOverflowError with lazy qsort of The Joy of Clojure
On Aug 26, 12:40 pm, Tassilo Horn tass...@member.fsf.org wrote: Paul Mooser taron...@gmail.com writes: Hi Paul, If you search for filter and StackOverflowError in this group, you will find people discussing related issues. Thanks, I've found some explanation by Meikel Brandmeier who explains the layering issue. But do we really have to live with that? I mean, replacing the filter/remove with a list comprehension solved the issue for me. And it's easy to define filter in terms of for. --8---cut here---start-8--- user (defn philter [pred coll] (for [x coll :when (pred x)] x)) #'user/philter user (filter even? (take 10 (iterate inc 0))) (0 2 4 6 8) user (philter even? (take 10 (iterate inc 0))) (0 2 4 6 8) --8---cut here---end---8--- I've tried benchmarking filter vs. philter with seqs of different sizes, and there seems to be no difference. So why not simply do it that way? It would make no difference. philter is exactly as lazy as filter, and will have the same stacked-laziness problem. See http://stackoverflow.com/questions/2946764/recursive-function-causing-a-stack-overflow which in my opinion is a pretty clear presentation of the 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
Re: StackOverflowError while using map inside a reduce
Hi, On 3 Nov., 00:40, Rasmus Svensson r...@lysator.liu.se wrote: I think the problem is that this reduction will build an expression like this: (map + ... (map + ... (map + ... (map + ... one million nesting levels When clojure tries to realize an element of the resulting lazy seq, every level will result in a nested method call, which will eventually blow the stack. Exactly. Much better than my try on an explanation. The doall in my example realises each sequence as it is created by reduce. Hence this deep nesting level of calls cannot happen. 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: StackOverflowError while using map inside a reduce
On Wed, Nov 3, 2010 at 2:35 AM, Meikel Brandmeyer m...@kotka.de wrote: Hi, On 3 Nov., 00:40, Rasmus Svensson r...@lysator.liu.se wrote: I think the problem is that this reduction will build an expression like this: (map + ... (map + ... (map + ... (map + ... one million nesting levels When clojure tries to realize an element of the resulting lazy seq, every level will result in a nested method call, which will eventually blow the stack. Exactly. Much better than my try on an explanation. The doall in my example realises each sequence as it is created by reduce. Hence this deep nesting level of calls cannot happen. Thank you both for the explanation. I hadn't realized that reduce could work with lazy sequences in that way. I figured it was doing the equivalent of a doall already for each element of the sequence, but apparently not. :-) So now I see how the nesting can happen. Thanks! -John -- 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: StackOverflowError possible with seq ?
Hi Robert, I tracked down the source of the StackOverflowError in my code. It boils down to the following similar (to your example) code snippet. Although, on my machine, this example gives the stack overflow, rather than the OOM error. (def acoll (ref [])) (doseq [i (range 1 3000)] (println i: i) (dosync (alter acoll concat [i]))) (println count: (count @acoll)) You can avoid this stack overflow with a doall or just replacing concat [i] with conj i. This seems obvious in hind-sight of course... Thanks for your help. Kyle On Jan 18, 6:36 am, kirschkernkissen robert.ste...@gmail.com wrote: Hi Kyle! I encountered the same problem: (defn stack-fail returns an empty lazy seq [l i] (if ( i 0) (recur (remove #{1} (concat l '(1))) (dec i)) l)) (def foo (stack-fail () 1000)) foo ; Evaluation aborted. if you call a non-lazy function (e.g. doall) on the collection from time to time this wont happen. Rgds, Robert On 15 Jan., 00:05, mudphone kyle...@gmail.com wrote: Is it possible that the Clojure core seq function can cause a stack overflow (since it calls itself)? Or is there some other manner in which misuse of a lazy seq could cause this? In the stack trace below, I'm seeing repeated calls to seq in clojure core, until the stack is blown. Thanks, Kyle Exception in thread main java.lang.StackOverflowError (session_master_boot.clj:19) at clojure.lang.Compiler.eval(Compiler.java:4617) at clojure.lang.Compiler.eval(Compiler.java:4593) at clojure.lang.Compiler.load(Compiler.java:4931) at clojure.lang.Compiler.loadFile(Compiler.java:4898) at clojure.main$load_script__6637.invoke(main.clj:210) at clojure.main$init_opt__6640.invoke(main.clj:215) at clojure.main$initialize__6650.invoke(main.clj:243) at clojure.main$null_opt__6672.invoke(main.clj:268) at clojure.main$legacy_script__6687.invoke(main.clj:299) at clojure.lang.Var.invoke(Var.java:359) at clojure.main.legacy_script(main.java:32) at clojure.lang.Script.main(Script.java:20) Caused by: java.lang.StackOverflowError at clojure.core$seq__3835.invoke(core.clj:103) at clojure.core$concat__3960$fn__3970.invoke(core.clj:427) at clojure.lang.LazySeq.sval(LazySeq.java:42) at clojure.lang.LazySeq.seq(LazySeq.java:56) at clojure.lang.RT.seq(RT.java:440) at clojure.core$seq__3835.invoke(core.clj:103) at clojure.core$concat__3960$fn__3970.invoke(core.clj:427) at clojure.lang.LazySeq.sval(LazySeq.java:42) at clojure.lang.LazySeq.seq(LazySeq.java:56) at clojure.lang.RT.seq(RT.java:440) at clojure.core$seq__3835.invoke(core.clj:103) at clojure.core$concat__3960$fn__3970.invoke(core.clj:427) at clojure.lang.LazySeq.sval(LazySeq.java:42) at clojure.lang.LazySeq.seq(LazySeq.java:56) at clojure.lang.RT.seq(RT.java:440) at clojure.core$seq__3835.invoke(core.clj:103) at clojure.core$concat__3960$fn__3970.invoke(core.clj:427) at clojure.lang.LazySeq.sval(LazySeq.java:42) at clojure.lang.LazySeq.seq(LazySeq.java:56) at clojure.lang.RT.seq(RT.java:440) -- 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: StackOverflowError possible with seq ?
Hi Kyle! I encountered the same problem: (defn stack-fail returns an empty lazy seq [l i] (if ( i 0) (recur (remove #{1} (concat l '(1))) (dec i)) l)) (def foo (stack-fail () 1000)) foo ; Evaluation aborted. if you call a non-lazy function (e.g. doall) on the collection from time to time this wont happen. Rgds, Robert On 15 Jan., 00:05, mudphone kyle...@gmail.com wrote: Is it possible that the Clojure core seq function can cause a stack overflow (since it calls itself)? Or is there some other manner in which misuse of a lazy seq could cause this? In the stack trace below, I'm seeing repeated calls to seq in clojure core, until the stack is blown. Thanks, Kyle Exception in thread main java.lang.StackOverflowError (session_master_boot.clj:19) at clojure.lang.Compiler.eval(Compiler.java:4617) at clojure.lang.Compiler.eval(Compiler.java:4593) at clojure.lang.Compiler.load(Compiler.java:4931) at clojure.lang.Compiler.loadFile(Compiler.java:4898) at clojure.main$load_script__6637.invoke(main.clj:210) at clojure.main$init_opt__6640.invoke(main.clj:215) at clojure.main$initialize__6650.invoke(main.clj:243) at clojure.main$null_opt__6672.invoke(main.clj:268) at clojure.main$legacy_script__6687.invoke(main.clj:299) at clojure.lang.Var.invoke(Var.java:359) at clojure.main.legacy_script(main.java:32) at clojure.lang.Script.main(Script.java:20) Caused by: java.lang.StackOverflowError at clojure.core$seq__3835.invoke(core.clj:103) at clojure.core$concat__3960$fn__3970.invoke(core.clj:427) at clojure.lang.LazySeq.sval(LazySeq.java:42) at clojure.lang.LazySeq.seq(LazySeq.java:56) at clojure.lang.RT.seq(RT.java:440) at clojure.core$seq__3835.invoke(core.clj:103) at clojure.core$concat__3960$fn__3970.invoke(core.clj:427) at clojure.lang.LazySeq.sval(LazySeq.java:42) at clojure.lang.LazySeq.seq(LazySeq.java:56) at clojure.lang.RT.seq(RT.java:440) at clojure.core$seq__3835.invoke(core.clj:103) at clojure.core$concat__3960$fn__3970.invoke(core.clj:427) at clojure.lang.LazySeq.sval(LazySeq.java:42) at clojure.lang.LazySeq.seq(LazySeq.java:56) at clojure.lang.RT.seq(RT.java:440) at clojure.core$seq__3835.invoke(core.clj:103) at clojure.core$concat__3960$fn__3970.invoke(core.clj:427) at clojure.lang.LazySeq.sval(LazySeq.java:42) at clojure.lang.LazySeq.seq(LazySeq.java:56) at clojure.lang.RT.seq(RT.java:440) -- 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: StackOverflowError possible with seq ?
Hi Robert, Thanks for your reply! Very interesting. I see what you're saying. But, your code results in a OutOfMemoryError, rather than a StackOverflow. At least, that's what I see on my machine. Are you seeing the same? Is there some way that this same concept could lead to a StackOverflow? Thanks, Kyle On Jan 18, 6:36 am, kirschkernkissen robert.ste...@gmail.com wrote: Hi Kyle! I encountered the same problem: (defn stack-fail returns an empty lazy seq [l i] (if ( i 0) (recur (remove #{1} (concat l '(1))) (dec i)) l)) (def foo (stack-fail () 1000)) foo ; Evaluation aborted. if you call a non-lazy function (e.g. doall) on the collection from time to time this wont happen. Rgds, Robert On 15 Jan., 00:05, mudphone kyle...@gmail.com wrote: Is it possible that the Clojure core seq function can cause a stack overflow (since it calls itself)? Or is there some other manner in which misuse of a lazy seq could cause this? In the stack trace below, I'm seeing repeated calls to seq in clojure core, until the stack is blown. Thanks, Kyle Exception in thread main java.lang.StackOverflowError (session_master_boot.clj:19) at clojure.lang.Compiler.eval(Compiler.java:4617) at clojure.lang.Compiler.eval(Compiler.java:4593) at clojure.lang.Compiler.load(Compiler.java:4931) at clojure.lang.Compiler.loadFile(Compiler.java:4898) at clojure.main$load_script__6637.invoke(main.clj:210) at clojure.main$init_opt__6640.invoke(main.clj:215) at clojure.main$initialize__6650.invoke(main.clj:243) at clojure.main$null_opt__6672.invoke(main.clj:268) at clojure.main$legacy_script__6687.invoke(main.clj:299) at clojure.lang.Var.invoke(Var.java:359) at clojure.main.legacy_script(main.java:32) at clojure.lang.Script.main(Script.java:20) Caused by: java.lang.StackOverflowError at clojure.core$seq__3835.invoke(core.clj:103) at clojure.core$concat__3960$fn__3970.invoke(core.clj:427) at clojure.lang.LazySeq.sval(LazySeq.java:42) at clojure.lang.LazySeq.seq(LazySeq.java:56) at clojure.lang.RT.seq(RT.java:440) at clojure.core$seq__3835.invoke(core.clj:103) at clojure.core$concat__3960$fn__3970.invoke(core.clj:427) at clojure.lang.LazySeq.sval(LazySeq.java:42) at clojure.lang.LazySeq.seq(LazySeq.java:56) at clojure.lang.RT.seq(RT.java:440) at clojure.core$seq__3835.invoke(core.clj:103) at clojure.core$concat__3960$fn__3970.invoke(core.clj:427) at clojure.lang.LazySeq.sval(LazySeq.java:42) at clojure.lang.LazySeq.seq(LazySeq.java:56) at clojure.lang.RT.seq(RT.java:440) at clojure.core$seq__3835.invoke(core.clj:103) at clojure.core$concat__3960$fn__3970.invoke(core.clj:427) at clojure.lang.LazySeq.sval(LazySeq.java:42) at clojure.lang.LazySeq.seq(LazySeq.java:56) at clojure.lang.RT.seq(RT.java:440) -- 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: StackOverflowError Question
Hi Chrisophe, You are correct, the doall also solves the problem. Based on that, I moved the doall out of matrixMultiply and into the computeActualResponse function, so that the caller can decide whether they want lazy evaluation for matrixMultiply or not: (defn computeActualResponse [signumFunction weights inputs] (if (and (not (nil? weights)) (not (nil? inputs))) ;;TODO create a function that will apply first to a collection until the inner item is obtained (signumFunction (first (first (doall (matrixMultiply (transpose weights) inputs))) On Apr 21, 1:45 am, Christophe Grand christo...@cgrand.net wrote: Hello, (def lazy-identity [x] (lazy-seq x)) (nth (iterate lazy-identity [1 2]) 10) ; returns (1 2) (nth (iterate lazy-identity [1 2]) 1000) ; returns (1 2) (nth (iterate lazy-identity [1 2]) 10) ; (with my JVM settings) throws a StackOverflowException Each time that you are building a lazy sequence from another one (eg through map, remove, filter etc.) you are building a closure that will call the inner closure of the source lazy seq. So when you call map on the result of a map on the result of a map on the result of a map on the result of a map on the result of a map on the result of a map on the result of a map... you are creating a closure that will call a closure that will call a closure that will call a closure that will call a closure that will call a closure that will call a closure... and, if they are too nested, will throw a StackOverflowException. However the call to the inner closure of a lazy seq takes only place if the seq hasn't been realized before. That's why Dimiters's suggestion corrects your problem. This may be another way (I didn't test it) to force the realization of your matrix: (defn matrixMultiply [matrixA matrixB] (doall (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA))) hth, Christophe jleehurt a écrit : Hi Dimiter, Thank you! I'm still a bit confused as to why this was happening. Does lazy evaluation not work well with recursion? On Apr 20, 11:06 pm, Dimiter \malkia\ Stanev mal...@gmail.com wrote: I blindly tried printing out stuff from matrixMultiply, and found out that if I print matrixA and matrixB it doesn't run out of stack, so I guess I was forcing them to work, here is a version with (dorun) that has the same side effect, without printing: (defn matrixMultiply [matrixA matrixB] (dorun matrixA) (dorun matrixB) (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA)) user (main 10) ((0.5 5.5)) user (main 100) ((0.5 50.5)) user (time (main 100)) Elapsed time: 8314.617 msecs ((0.5 50.5)) user (time (main 1000)) ; Evaluation aborted. ;; Actually not stack overflow, but HEAP overflow (it took a while though) user Thanks, Dimiter malkia Stanev. On Apr 20, 10:01 pm, jleehurt jleeh...@gmail.com wrote: Hi David, Those two are not recursive, but they call into other functions that are. Do I need to make sure that all recursive functions use the loop/ recur pattern? Or should I not nest recursive calls like this? Here is the whole source: ;; Neuron Activation Functions ;threshold (defn threshold [x] (if (= x 0) 1 0)) ;signum (threshold) (defn signum [x] (cond ( x 0) 1 (= x 0) 0 ( x 0) -1)) ;; Matrix Functions (defn transpose [matrix] (if (not (nil? matrix)) (apply map list matrix))) (defn transpose2 [matrix] (apply map (fn [ column] column) matrix)) (defn matrixMultiply [matrixA matrixB] (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA)) (defn matrixAdd [matrixA matrixB] (if (and (not (empty? matrixA)) (not (empty? matrixB))) (conj (matrixAdd (rest matrixA) (rest matrixB)) (map + (first matrixA) (first matrixB) (defn matrixMultiplyScalar [matrixA scalar] (if (not (empty? matrixA)) (conj (matrixMultiplyScalar (rest matrixA) scalar) (map (fn [arg] (* arg scalar)) (first matrixA) ;; Vector Functions (defn transposeVector [v] (if (not (nil? v)) (transpose (vector v (defn vectorMultiplyScalar [v scalar] (map * v (cycle [ scalar ]))) ;; Binary Logic Input/Output (def infiniteInputCollection (cycle [[[-1 -1]] [[-1 1]] [[1 -1]] [[1 1]]])) (def infiniteAndOutputCollection (cycle [-1 -1 -1 1])) (defn buildInputs [numberOfInputs] (loop [inputVector [] binaryInputCollection infiniteInputCollection remainingCount numberOfInputs] (if ( 0 remainingCount) inputVector (recur (conj inputVector (first binaryInputCollection)) (rest
Re: StackOverflowError Question
I blindly tried printing out stuff from matrixMultiply, and found out that if I print matrixA and matrixB it doesn't run out of stack, so I guess I was forcing them to work, here is a version with (dorun) that has the same side effect, without printing: (defn matrixMultiply [matrixA matrixB] (dorun matrixA) (dorun matrixB) (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA)) user (main 10) ((0.5 5.5)) user (main 100) ((0.5 50.5)) user (time (main 100)) Elapsed time: 8314.617 msecs ((0.5 50.5)) user (time (main 1000)) ; Evaluation aborted. ;; Actually not stack overflow, but HEAP overflow (it took a while though) user Thanks, Dimiter malkia Stanev. On Apr 20, 10:01 pm, jleehurt jleeh...@gmail.com wrote: Hi David, Those two are not recursive, but they call into other functions that are. Do I need to make sure that all recursive functions use the loop/ recur pattern? Or should I not nest recursive calls like this? Here is the whole source: ;; Neuron Activation Functions ;threshold (defn threshold [x] (if (= x 0) 1 0)) ;signum (threshold) (defn signum [x] (cond ( x 0) 1 (= x 0) 0 ( x 0) -1)) ;; Matrix Functions (defn transpose [matrix] (if (not (nil? matrix)) (apply map list matrix))) (defn transpose2 [matrix] (apply map (fn [ column] column) matrix)) (defn matrixMultiply [matrixA matrixB] (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA)) (defn matrixAdd [matrixA matrixB] (if (and (not (empty? matrixA)) (not (empty? matrixB))) (conj (matrixAdd (rest matrixA) (rest matrixB)) (map + (first matrixA) (first matrixB) (defn matrixMultiplyScalar [matrixA scalar] (if (not (empty? matrixA)) (conj (matrixMultiplyScalar (rest matrixA) scalar) (map (fn [arg] (* arg scalar)) (first matrixA) ;; Vector Functions (defn transposeVector [v] (if (not (nil? v)) (transpose (vector v (defn vectorMultiplyScalar [v scalar] (map * v (cycle [ scalar ]))) ;; Binary Logic Input/Output (def infiniteInputCollection (cycle [[[-1 -1]] [[-1 1]] [[1 -1]] [[1 1]]])) (def infiniteAndOutputCollection (cycle [-1 -1 -1 1])) (defn buildInputs [numberOfInputs] (loop [inputVector [] binaryInputCollection infiniteInputCollection remainingCount numberOfInputs] (if ( 0 remainingCount) inputVector (recur (conj inputVector (first binaryInputCollection)) (rest binaryInputCollection) (dec remainingCount) (defn buildOutputs [numberOfOutputs outputCollection] (loop [outputVector [] andOutputCollection outputCollection remainingCount numberOfOutputs] (if ( 0 remainingCount) outputVector (recur (conj outputVector (first andOutputCollection)) (rest andOutputCollection) (dec remainingCount) ;; Main ;learning rate parameter eta (def learningRateParameter 0.5) ;the weight vector of the perceptron (def weightVector (ref nil)) ;multiply the transpose of the weight vector with the input vector ;apply the signum function to the scalar result (defn computeActualResponse [signumFunction weights inputs] (if (and (not (nil? weights)) (not (nil? inputs))) (signumFunction (first (first (matrixMultiply (transpose weights) inputs)) ;return an updated weight vector of the perceptron (defn getAdaptedWeightVector [weights inputs desiredResponse actualResponse] (let [etaDeltaDesiredActual (* learningRateParameter (- desiredResponse actualResponse))] (matrixAdd weights (matrixMultiplyScalar inputs etaDeltaDesiredActual ;train the perceptron with the inputs and corresponding known outputs (defn trainPerceptron [beginningWeightVector allInputs allOutputs] (loop [weightVector beginningWeightVector inputs allInputs responses allOutputs] (if (and (not (empty? inputs)) (not (empty? responses))) (let [adaptedWeightVector (getAdaptedWeightVector weightVector (first inputs) (first responses) (computeActualResponse signum weightVector (first inputs)))] (recur adaptedWeightVector (rest inputs) (rest responses))) weightVector))) (defn main [sizeOfDataSet] (let [weights [[0 0]] inputs (buildInputs sizeOfDataSet) outputs (buildOutputs sizeOfDataSet infiniteAndOutputCollection)] (trainPerceptron weights inputs outputs))) On Apr 20, 6:32 am, David Nolen dnolen.li...@gmail.com wrote: You have two other function calls getAdaptedWeightVector computeActualResponse Are these recursive as well? On Sun, Apr 19, 2009 at 11:26 PM, jleehurt jleeh...@gmail.com wrote: Hi all, I have
Re: StackOverflowError Question
Hi Dimiter, Thank you! I'm still a bit confused as to why this was happening. Does lazy evaluation not work well with recursion? On Apr 20, 11:06 pm, Dimiter \malkia\ Stanev mal...@gmail.com wrote: I blindly tried printing out stuff from matrixMultiply, and found out that if I print matrixA and matrixB it doesn't run out of stack, so I guess I was forcing them to work, here is a version with (dorun) that has the same side effect, without printing: (defn matrixMultiply [matrixA matrixB] (dorun matrixA) (dorun matrixB) (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA)) user (main 10) ((0.5 5.5)) user (main 100) ((0.5 50.5)) user (time (main 100)) Elapsed time: 8314.617 msecs ((0.5 50.5)) user (time (main 1000)) ; Evaluation aborted. ;; Actually not stack overflow, but HEAP overflow (it took a while though) user Thanks, Dimiter malkia Stanev. On Apr 20, 10:01 pm, jleehurt jleeh...@gmail.com wrote: Hi David, Those two are not recursive, but they call into other functions that are. Do I need to make sure that all recursive functions use the loop/ recur pattern? Or should I not nest recursive calls like this? Here is the whole source: ;; Neuron Activation Functions ;threshold (defn threshold [x] (if (= x 0) 1 0)) ;signum (threshold) (defn signum [x] (cond ( x 0) 1 (= x 0) 0 ( x 0) -1)) ;; Matrix Functions (defn transpose [matrix] (if (not (nil? matrix)) (apply map list matrix))) (defn transpose2 [matrix] (apply map (fn [ column] column) matrix)) (defn matrixMultiply [matrixA matrixB] (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA)) (defn matrixAdd [matrixA matrixB] (if (and (not (empty? matrixA)) (not (empty? matrixB))) (conj (matrixAdd (rest matrixA) (rest matrixB)) (map + (first matrixA) (first matrixB) (defn matrixMultiplyScalar [matrixA scalar] (if (not (empty? matrixA)) (conj (matrixMultiplyScalar (rest matrixA) scalar) (map (fn [arg] (* arg scalar)) (first matrixA) ;; Vector Functions (defn transposeVector [v] (if (not (nil? v)) (transpose (vector v (defn vectorMultiplyScalar [v scalar] (map * v (cycle [ scalar ]))) ;; Binary Logic Input/Output (def infiniteInputCollection (cycle [[[-1 -1]] [[-1 1]] [[1 -1]] [[1 1]]])) (def infiniteAndOutputCollection (cycle [-1 -1 -1 1])) (defn buildInputs [numberOfInputs] (loop [inputVector [] binaryInputCollection infiniteInputCollection remainingCount numberOfInputs] (if ( 0 remainingCount) inputVector (recur (conj inputVector (first binaryInputCollection)) (rest binaryInputCollection) (dec remainingCount) (defn buildOutputs [numberOfOutputs outputCollection] (loop [outputVector [] andOutputCollection outputCollection remainingCount numberOfOutputs] (if ( 0 remainingCount) outputVector (recur (conj outputVector (first andOutputCollection)) (rest andOutputCollection) (dec remainingCount) ;; Main ;learning rate parameter eta (def learningRateParameter 0.5) ;the weight vector of the perceptron (def weightVector (ref nil)) ;multiply the transpose of the weight vector with the input vector ;apply the signum function to the scalar result (defn computeActualResponse [signumFunction weights inputs] (if (and (not (nil? weights)) (not (nil? inputs))) (signumFunction (first (first (matrixMultiply (transpose weights) inputs)) ;return an updated weight vector of the perceptron (defn getAdaptedWeightVector [weights inputs desiredResponse actualResponse] (let [etaDeltaDesiredActual (* learningRateParameter (- desiredResponse actualResponse))] (matrixAdd weights (matrixMultiplyScalar inputs etaDeltaDesiredActual ;train the perceptron with the inputs and corresponding known outputs (defn trainPerceptron [beginningWeightVector allInputs allOutputs] (loop [weightVector beginningWeightVector inputs allInputs responses allOutputs] (if (and (not (empty? inputs)) (not (empty? responses))) (let [adaptedWeightVector (getAdaptedWeightVector weightVector (first inputs) (first responses) (computeActualResponse signum weightVector (first inputs)))] (recur adaptedWeightVector (rest inputs) (rest responses))) weightVector))) (defn main [sizeOfDataSet] (let [weights [[0 0]] inputs (buildInputs sizeOfDataSet) outputs (buildOutputs sizeOfDataSet
Re: StackOverflowError Question
Hi Jleehurt, I'm still newbie and don't know, but you have at least two recursive functions - matrixAdd, and matrixMultiplyScalar. I've modified them to work with loop/recur, but I can't tell whether they are with same efficiency (at least no stack problem). Still if I remove the dorun from the place where I've added it originally the stack overflow still happens. In any case, here is my latest modification (Sorry I've also modified other pieces, trying to speed it up :) - failing miserably - you can ignore them!) --- ;; Neuron Activation Functions ;threshold (defn threshold [#^Double x] (if (= x 0.0) 1.0 0.0)) ;signum (threshold) (defn signum [x] (Math/signum x)) ;; Matrix Functions (defn transpose [matrix] (if (not (nil? matrix)) (apply map list matrix))) (defn matrixMultiply [matrixA matrixB] (dorun matrixA) (dorun matrixB) (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA)) (defn matrixAdd-old [matrixA matrixB] (if (and (not (empty? matrixA)) (not (empty? matrixB))) (conj (matrixAdd-old (rest matrixA) (rest matrixB)) (map + (first matrixA) (first matrixB) (defn matrixAdd [matrixA matrixB] (loop [result () matrixA matrixA matrixB matrixB] (let [rowA (first matrixA) rowB (first matrixB)] (if (or (nil? rowA) (nil? rowB)) (reverse result) (recur (conj result (map + rowA rowB)) (rest matrixA) (rest matrixB)) (defn matrixMultiplyScalar-old [matrixA scalar] (if (not (empty? matrixA)) (conj (matrixMultiplyScalar-old (rest matrixA) scalar) (map (fn [arg] (* arg scalar)) (first matrixA) (defn matrixMultiplyScalar [matrixA scalar] (loop [result () matrixA matrixA] (let [row (first matrixA)] (if (nil? row) (reverse result) (recur (conj result (map (fn [arg] (* arg scalar)) row)) (rest matrixA)) ;; Binary Logic Input/Output (def infiniteInputCollection (cycle [[[-1.0 -1.0]] [[-1.0 1.0]] [[1.0 -1.0]] [[1.0 1.0]]])) (def infiniteAndOutputCollection (cycle [-1.0 -1.0 -1.0 1.0])) (defn buildInputs [numberOfInputs] (loop [inputVector [] binaryInputCollection infiniteInputCollection remainingCount numberOfInputs] (if ( 0 remainingCount) inputVector (recur (conj inputVector (first binaryInputCollection)) (rest binaryInputCollection) (dec remainingCount) (defn buildOutputs [numberOfOutputs outputCollection] (loop [outputVector [] andOutputCollection outputCollection remainingCount numberOfOutputs] (if ( 0 remainingCount) outputVector (recur (conj outputVector (first andOutputCollection)) (rest andOutputCollection) (dec remainingCount) ;; Main ;learning rate parameter eta (def learningRateParameter 0.5) ;multiply the transpose of the weight vector with the input vector ;apply the signum function to the scalar result (defn computeActualResponse [signumFunction weights inputs] (signumFunction (first (first (matrixMultiply (transpose weights) inputs) ;return an updated weight vector of the perceptron (defn getAdaptedWeightVector [weights inputs desiredResponse actualResponse] (let [etaDeltaDesiredActual (* learningRateParameter (- desiredResponse actualResponse))] (matrixAdd weights (matrixMultiplyScalar inputs etaDeltaDesiredActual ;train the perceptron with the inputs and corresponding known outputs (defn trainPerceptron [beginningWeightVector allInputs allOutputs] (loop [weightVector beginningWeightVector inputs allInputs responses allOutputs] (if (or (empty? inputs) (empty? responses)) weightVector (recur (getAdaptedWeightVector weightVector (first inputs) (first responses) (computeActualResponse signum weightVector (first inputs))) (rest inputs) (rest responses) (defn main [sizeOfDataSet] (let [weights [[0.0 0.0]] inputs (buildInputs sizeOfDataSet) outputs (buildOutputs sizeOfDataSet infiniteAndOutputCollection)] (trainPerceptron weights inputs outputs))) --- On Apr 21, 12:58 am, jleehurt jleeh...@gmail.com wrote: Hi Dimiter, Thank you! I'm still a bit confused as to why this was happening. Does lazy evaluation not work well with recursion? On Apr 20, 11:06 pm, Dimiter \malkia\ Stanev mal...@gmail.com wrote: I blindly tried printing out stuff from matrixMultiply, and found out that if I print matrixA and matrixB it doesn't run out of stack, so I guess I was forcing them to work, here is a version with
Re: StackOverflowError Question
Hello, (def lazy-identity [x] (lazy-seq x)) (nth (iterate lazy-identity [1 2]) 10) ; returns (1 2) (nth (iterate lazy-identity [1 2]) 1000) ; returns (1 2) (nth (iterate lazy-identity [1 2]) 10) ; (with my JVM settings) throws a StackOverflowException Each time that you are building a lazy sequence from another one (eg through map, remove, filter etc.) you are building a closure that will call the inner closure of the source lazy seq. So when you call map on the result of a map on the result of a map on the result of a map on the result of a map on the result of a map on the result of a map on the result of a map... you are creating a closure that will call a closure that will call a closure that will call a closure that will call a closure that will call a closure that will call a closure... and, if they are too nested, will throw a StackOverflowException. However the call to the inner closure of a lazy seq takes only place if the seq hasn't been realized before. That's why Dimiters's suggestion corrects your problem. This may be another way (I didn't test it) to force the realization of your matrix: (defn matrixMultiply [matrixA matrixB] (doall (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA))) hth, Christophe jleehurt a écrit : Hi Dimiter, Thank you! I'm still a bit confused as to why this was happening. Does lazy evaluation not work well with recursion? On Apr 20, 11:06 pm, Dimiter \malkia\ Stanev mal...@gmail.com wrote: I blindly tried printing out stuff from matrixMultiply, and found out that if I print matrixA and matrixB it doesn't run out of stack, so I guess I was forcing them to work, here is a version with (dorun) that has the same side effect, without printing: (defn matrixMultiply [matrixA matrixB] (dorun matrixA) (dorun matrixB) (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA)) user (main 10) ((0.5 5.5)) user (main 100) ((0.5 50.5)) user (time (main 100)) Elapsed time: 8314.617 msecs ((0.5 50.5)) user (time (main 1000)) ; Evaluation aborted. ;; Actually not stack overflow, but HEAP overflow (it took a while though) user Thanks, Dimiter malkia Stanev. On Apr 20, 10:01 pm, jleehurt jleeh...@gmail.com wrote: Hi David, Those two are not recursive, but they call into other functions that are. Do I need to make sure that all recursive functions use the loop/ recur pattern? Or should I not nest recursive calls like this? Here is the whole source: ;; Neuron Activation Functions ;threshold (defn threshold [x] (if (= x 0) 1 0)) ;signum (threshold) (defn signum [x] (cond ( x 0) 1 (= x 0) 0 ( x 0) -1)) ;; Matrix Functions (defn transpose [matrix] (if (not (nil? matrix)) (apply map list matrix))) (defn transpose2 [matrix] (apply map (fn [ column] column) matrix)) (defn matrixMultiply [matrixA matrixB] (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA)) (defn matrixAdd [matrixA matrixB] (if (and (not (empty? matrixA)) (not (empty? matrixB))) (conj (matrixAdd (rest matrixA) (rest matrixB)) (map + (first matrixA) (first matrixB) (defn matrixMultiplyScalar [matrixA scalar] (if (not (empty? matrixA)) (conj (matrixMultiplyScalar (rest matrixA) scalar) (map (fn [arg] (* arg scalar)) (first matrixA) ;; Vector Functions (defn transposeVector [v] (if (not (nil? v)) (transpose (vector v (defn vectorMultiplyScalar [v scalar] (map * v (cycle [ scalar ]))) ;; Binary Logic Input/Output (def infiniteInputCollection (cycle [[[-1 -1]] [[-1 1]] [[1 -1]] [[1 1]]])) (def infiniteAndOutputCollection (cycle [-1 -1 -1 1])) (defn buildInputs [numberOfInputs] (loop [inputVector [] binaryInputCollection infiniteInputCollection remainingCount numberOfInputs] (if ( 0 remainingCount) inputVector (recur (conj inputVector (first binaryInputCollection)) (rest binaryInputCollection) (dec remainingCount) (defn buildOutputs [numberOfOutputs outputCollection] (loop [outputVector [] andOutputCollection outputCollection remainingCount numberOfOutputs] (if ( 0 remainingCount) outputVector (recur (conj outputVector (first andOutputCollection)) (rest andOutputCollection) (dec remainingCount) ;; Main ;learning rate parameter eta (def learningRateParameter 0.5) ;the weight vector of the perceptron (def weightVector (ref nil)) ;multiply the transpose of the weight
Re: StackOverflowError Question
You have two other function calls getAdaptedWeightVector computeActualResponse Are these recursive as well? On Sun, Apr 19, 2009 at 11:26 PM, jleehurt jleeh...@gmail.com wrote: Hi all, I have the following code that trains a perceptron with the given inputs and corresponding desired inputs. For input/output vectors, when the size gets to about 2000, I am getting a java.lang.StackOverflowError in the following function: (defn trainPerceptron [beginningWeightVector allInputs allOutputs] (loop [weightVector beginningWeightVector inputs allInputs responses allOutputs] (if (and (not (empty? inputs)) (not (empty? responses))) (let [adaptedWeightVector (getAdaptedWeightVector weightVector (first inputs) (first responses) (computeActualResponse signum weightVector (first inputs)))] (recur adaptedWeightVector (rest inputs) (rest responses))) weightVector))) Is not the purpose of loop/recur to avoid stack overflow problems? What am I doing wrong? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~--~~~~--~~--~--~---
Re: StackOverflowError Question
Hi David, Those two are not recursive, but they call into other recursive functions. Do I need to make sure all recursive functions use the loop/ recur pattern? Or maybe not nest recursive calls like this? Here is the whole source: ;threshold (defn threshold [x] (if (= x 0) 1 0)) ;signum (threshold) (defn signum [x] (cond ( x 0) 1 (= x 0) 0 ( x 0) -1)) ;piecewise linear (defn piecewise [x] (cond (= x 0.5) 1 (and ( x -0.5) ( x 0.5)) x (= x -0.5) 0)) ;logistic (sigmoidal) (defn sigmoid [x slopeParameter] (/ 1 (+ 1 (Math/exp (* -1 (* x slopeParameter)) ;hyberbolic tangent (sigmoidal) (defn hyperbolicTangent [x] (Math/tanh x)) ;arctangent (sigmoidal) (defn arcTangent [x] (Math/atan x)) ;gompertz curve (sigmoidal) ; a is the upper asymptote ; c is the growth rate ; b, c are negative numbers (defn gompertzCurve [x a b c] (* a (Math/pow Math/E (* b (Math/pow Math/E (* c x)) ;algebraic sigmoid (defn algebraicSigmoid [x] (/ x (Math/sqrt (+ 1 (Math/pow x 2) ;; Matrix Functions (defn transpose [matrix] (if (not (nil? matrix)) (apply map list matrix))) (defn transpose2 [matrix] (apply map (fn [ column] column) matrix)) (defn matrixMultiply [matrixA matrixB] (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA)) (defn matrixAdd [matrixA matrixB] (if (and (not (empty? matrixA)) (not (empty? matrixB))) (conj (matrixAdd (rest matrixA) (rest matrixB)) (map + (first matrixA) (first matrixB) (defn matrixMultiplyScalar [matrixA scalar] (if (not (empty? matrixA)) (conj (matrixMultiplyScalar (rest matrixA) scalar) (map (fn [arg] (* arg scalar)) (first matrixA) ;; Vector Functions (defn transposeVector [v] (if (not (nil? v)) (transpose (vector v (defn vectorMultiplyScalar [v scalar] (map * v (cycle [ scalar ]))) ;; Binary Logic Input/Output (def infiniteInputCollection (cycle [[[-1 -1]] [[-1 1]] [[1 -1]] [[1 1]]])) (def infiniteAndOutputCollection (cycle [-1 -1 -1 1])) (defn buildInputs [numberOfInputs] (loop [inputVector [] binaryInputCollection infiniteInputCollection remainingCount numberOfInputs] (if ( 0 remainingCount) inputVector (recur (conj inputVector (first binaryInputCollection)) (rest binaryInputCollection) (dec remainingCount) (defn buildOutputs [numberOfOutputs outputCollection] (loop [outputVector [] andOutputCollection outputCollection remainingCount numberOfOutputs] (if ( 0 remainingCount) outputVector (recur (conj outputVector (first andOutputCollection)) (rest andOutputCollection) (dec remainingCount) ;; Main ;learning rate parameter eta (def learningRateParameter 0.5) ;the weight vector of the perceptron (def weightVector (ref nil)) ;multiply the transpose of the weight vector with the input vector ;apply the signum function to the scalar result (defn computeActualResponse [signumFunction weights inputs] (if (and (not (nil? weights)) (not (nil? inputs))) ;;TODO create a function that will apply first to a collection until the inner item is obtained (signumFunction (first (first (matrixMultiply (transpose weights) inputs)) (defn computeActualResponse2 [signumFunction weights inputs] ;;(if (and (not (nil? weights)) (not (nil? inputs))) ;;(signumFunction (first (first (matrixMultiply (transpose weights) inputs)) 4) ;return an updated weight vector of the perceptron (defn getAdaptedWeightVector [weights inputs desiredResponse actualResponse] (let [etaDeltaDesiredActual (* learningRateParameter (- desiredResponse actualResponse))] (matrixAdd weights (matrixMultiplyScalar inputs etaDeltaDesiredActual ;;(dosync (ref-set weightVector newWeights (defn getAdaptedWeightVector2 [weights inputs desiredResponse actualResponse] (let [etaDeltaDesiredActual (* learningRateParameter (- desiredResponse actualResponse)) scaledResponses (matrixMultiplyScalar inputs etaDeltaDesiredActual)] (matrixAdd weights [[0 0]]))) ;train the perceptron with the inputs and corresponding known outputs (defn trainPerceptron [beginningWeightVector allInputs allOutputs] (loop [weightVector beginningWeightVector inputs allInputs responses allOutputs] (if (and (not (empty? inputs)) (not (empty? responses))) (let [adaptedWeightVector (getAdaptedWeightVector weightVector (first inputs) (first responses) (computeActualResponse signum weightVector (first inputs)))] (recur adaptedWeightVector (rest inputs) (rest responses))) weightVector))) (defn trainPerceptron2 [beginningWeightVector allInputs allOutputs] (loop [inputs allInputs responses allOutputs] (if (or (empty? inputs) (empty? responses))
Re: StackOverflowError Question
Hi David, Those two are not recursive, but they call into other functions that are. Do I need to make sure that all recursive functions use the loop/ recur pattern? Or should I not nest recursive calls like this? Here is the whole source: ;; Neuron Activation Functions ;threshold (defn threshold [x] (if (= x 0) 1 0)) ;signum (threshold) (defn signum [x] (cond ( x 0) 1 (= x 0) 0 ( x 0) -1)) ;; Matrix Functions (defn transpose [matrix] (if (not (nil? matrix)) (apply map list matrix))) (defn transpose2 [matrix] (apply map (fn [ column] column) matrix)) (defn matrixMultiply [matrixA matrixB] (map (fn [row] (apply map (fn [ column] (apply + (map * row column))) matrixB)) matrixA)) (defn matrixAdd [matrixA matrixB] (if (and (not (empty? matrixA)) (not (empty? matrixB))) (conj (matrixAdd (rest matrixA) (rest matrixB)) (map + (first matrixA) (first matrixB) (defn matrixMultiplyScalar [matrixA scalar] (if (not (empty? matrixA)) (conj (matrixMultiplyScalar (rest matrixA) scalar) (map (fn [arg] (* arg scalar)) (first matrixA) ;; Vector Functions (defn transposeVector [v] (if (not (nil? v)) (transpose (vector v (defn vectorMultiplyScalar [v scalar] (map * v (cycle [ scalar ]))) ;; Binary Logic Input/Output (def infiniteInputCollection (cycle [[[-1 -1]] [[-1 1]] [[1 -1]] [[1 1]]])) (def infiniteAndOutputCollection (cycle [-1 -1 -1 1])) (defn buildInputs [numberOfInputs] (loop [inputVector [] binaryInputCollection infiniteInputCollection remainingCount numberOfInputs] (if ( 0 remainingCount) inputVector (recur (conj inputVector (first binaryInputCollection)) (rest binaryInputCollection) (dec remainingCount) (defn buildOutputs [numberOfOutputs outputCollection] (loop [outputVector [] andOutputCollection outputCollection remainingCount numberOfOutputs] (if ( 0 remainingCount) outputVector (recur (conj outputVector (first andOutputCollection)) (rest andOutputCollection) (dec remainingCount) ;; Main ;learning rate parameter eta (def learningRateParameter 0.5) ;the weight vector of the perceptron (def weightVector (ref nil)) ;multiply the transpose of the weight vector with the input vector ;apply the signum function to the scalar result (defn computeActualResponse [signumFunction weights inputs] (if (and (not (nil? weights)) (not (nil? inputs))) (signumFunction (first (first (matrixMultiply (transpose weights) inputs)) ;return an updated weight vector of the perceptron (defn getAdaptedWeightVector [weights inputs desiredResponse actualResponse] (let [etaDeltaDesiredActual (* learningRateParameter (- desiredResponse actualResponse))] (matrixAdd weights (matrixMultiplyScalar inputs etaDeltaDesiredActual ;train the perceptron with the inputs and corresponding known outputs (defn trainPerceptron [beginningWeightVector allInputs allOutputs] (loop [weightVector beginningWeightVector inputs allInputs responses allOutputs] (if (and (not (empty? inputs)) (not (empty? responses))) (let [adaptedWeightVector (getAdaptedWeightVector weightVector (first inputs) (first responses) (computeActualResponse signum weightVector (first inputs)))] (recur adaptedWeightVector (rest inputs) (rest responses))) weightVector))) (defn main [sizeOfDataSet] (let [weights [[0 0]] inputs (buildInputs sizeOfDataSet) outputs (buildOutputs sizeOfDataSet infiniteAndOutputCollection)] (trainPerceptron weights inputs outputs))) On Apr 20, 6:32 am, David Nolen dnolen.li...@gmail.com wrote: You have two other function calls getAdaptedWeightVector computeActualResponse Are these recursive as well? On Sun, Apr 19, 2009 at 11:26 PM, jleehurt jleeh...@gmail.com wrote: Hi all, I have the following code that trains a perceptron with the given inputs and corresponding desired inputs. For input/output vectors, when the size gets to about 2000, I am getting a java.lang.StackOverflowError in the following function: (defn trainPerceptron [beginningWeightVector allInputs allOutputs] (loop [weightVector beginningWeightVector inputs allInputs responses allOutputs] (if (and (not (empty? inputs)) (not (empty? responses))) (let [adaptedWeightVector (getAdaptedWeightVector weightVector (first inputs) (first responses) (computeActualResponse signum weightVector (first inputs)))] (recur adaptedWeightVector (rest inputs) (rest responses))) weightVector))) Is not the purpose of loop/recur to avoid stack overflow problems? What am