I am trying to figure out if it is possible to recursively call the current outer function from within a closure while using the prototype iterator. So... var MyNS = {}; MyNS.Package = function() {} MyNS.Package.prototype.myRecursiveFunction function(SomeComplexDataStructure) { SomeComplexDataStructure.DataPart.each( function(DataPart) { ??how do I call myRecursiveFunction?? } ); } Thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
MyNS.Package.prototype.myRecursiveFunction function(SomeComplexDataStructure) { * var outer = arguments.callee;* SomeComplexDataStructure.DataPart.each( function(DataPart) { * outer();* } ); } -Fred On Mon, May 26, 2008 at 2:05 PM, Jonathan Logue <jonathanlogue-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I am trying to figure out if it is possible to recursively call the > current outer function from within a closure while using the prototype > iterator. So... > > var MyNS = {}; > > MyNS.Package = function() {} > > MyNS.Package.prototype.myRecursiveFunction > function(SomeComplexDataStructure) { > > SomeComplexDataStructure.DataPart.each( function(DataPart) { ??how > do I call myRecursiveFunction?? } ); > > } > > Thanks! > > >-- Science answers questions; philosophy questions answers. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks so much! I kept reading about how the inner function had access to the members of the outer function but could not find an example of calling the actual function. You are a life saver. I''ll buy you a beer if I am ever in Chicago! -Jonathan On May 26, 12:41 pm, "Frederick Polgardy" <f...-SMQUYeM9IBBWk0Htik3J/w@public.gmane.org> wrote:> MyNS.Package.prototype.myRecursiveFunction > function(SomeComplexDataStructure) { > * var outer = arguments.callee;* > SomeComplexDataStructure.DataPart.each( function(DataPart) { > * outer();* > } ); > } > > -Fred > > On Mon, May 26, 2008 at 2:05 PM, Jonathan Logue <jonathanlo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > > > > > I am trying to figure out if it is possible to recursively call the > > current outer function from within a closure while using the prototype > > iterator. So... > > > var MyNS = {}; > > > MyNS.Package = function() {} > > > MyNS.Package.prototype.myRecursiveFunction > > function(SomeComplexDataStructure) { > > > SomeComplexDataStructure.DataPart.each( function(DataPart) { ??how > > do I call myRecursiveFunction?? } ); > > > } > > > Thanks! > > -- > Science answers questions; philosophy questions answers.- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I''ll never refuse a free beer. ;-) On the example code, there are two other things to keep in mind: 1) Have to save away the value of arguments.callee before going into the inner function (arguments is always bound to the innermost function); 2) If you want to actually call the function recursively *as a method of this*, the above syntax won''t work. You''ll need to do something like: MyNS.Package.prototype.myRecursiveFunction function(SomeComplexDataStructure) { * var outer_fn = arguments.callee; var outer_this = this; * SomeComplexDataStructure.DataPart.each( function(DataPart) { * outer_fn.call(outer_this, DataPart);* // or whatever the args should be } ); } On Mon, May 26, 2008 at 2:48 PM, Jonathan Logue <jonathanlogue-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Thanks so much! I kept reading about how the inner function had access > to the members of the outer function but could not find an example of > calling the actual function. You are a life saver. > > I''ll buy you a beer if I am ever in Chicago! > > -Jonathan-- Science answers questions; philosophy questions answers. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Another way to do this could be to just use proto''s .bind(), assuming the "myRecursiveFunction" is only ever called as an instance method (vs. calling it statically against the MyNS.Package''s prototype)... So: SomeComplexDataStructure.DataPart.each( function(DataPart) { *this.myRecursiveFunction(DataPart);* // or whatever the args should be }.bind(this) ); On 5/27/08, Frederick Polgardy <fred-SMQUYeM9IBBWk0Htik3J/w@public.gmane.org> wrote:> > I''ll never refuse a free beer. ;-) > > On the example code, there are two other things to keep in mind: > > 1) Have to save away the value of arguments.callee before going into the > inner function (arguments is always bound to the innermost function); > 2) If you want to actually call the function recursively *as a method of > this*, the above syntax won''t work. You''ll need to do something like: > > MyNS.Package.prototype.myRecursiveFunction > function(SomeComplexDataStructure) { > * var outer_fn = arguments.callee; > var outer_this = this; > * > SomeComplexDataStructure.DataPart.each( function(DataPart) { > * outer_fn.call(outer_this, DataPart);* // or whatever the args > should be > } ); > } > > On Mon, May 26, 2008 at 2:48 PM, Jonathan Logue <jonathanlogue-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > >> >> Thanks so much! I kept reading about how the inner function had access >> to the members of the outer function but could not find an example of >> calling the actual function. You are a life saver. >> >> I''ll buy you a beer if I am ever in Chicago! >> >> -Jonathan > > > -- > Science answers questions; philosophy questions answers. > > >-- Ryan Gahl Manager, Senior Software Engineer Nth Penguin, LLC http://www.nthpenguin.com -- WebWidgetry.com / MashupStudio.com Future Home of the World''s First Complete Web Platform -- Inquire: 1-920-574-2218 Blog: http://www.someElement.com LinkedIn Profile: http://www.linkedin.com/in/ryangahl --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Buy him a beer instead; this solution is a lot better than mine in this case. On Tue, May 27, 2008 at 1:04 PM, Ryan Gahl <ryan.gahl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Another way to do this could be to just use proto''s .bind(), assuming the > "myRecursiveFunction" is only ever called as an instance method (vs. calling > it statically against the MyNS.Package''s prototype)... > > So: > > SomeComplexDataStructure.DataPart.each( function(DataPart) { > *this.myRecursiveFunction(DataPart);* // or whatever the args should be > }.bind(this) );-- Science answers questions; philosophy questions answers. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
:) Both are good patterns to know about (mine of course only works if you are using prototype while yours is good old javascript). On 5/27/08, Frederick Polgardy <fred-SMQUYeM9IBBWk0Htik3J/w@public.gmane.org> wrote:> > Buy him a beer instead; this solution is a lot better than mine in this > case. > > On Tue, May 27, 2008 at 1:04 PM, Ryan Gahl <ryan.gahl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Another way to do this could be to just use proto''s .bind(), assuming the >> "myRecursiveFunction" is only ever called as an instance method (vs. calling >> it statically against the MyNS.Package''s prototype)... >> >> So: >> >> SomeComplexDataStructure.DataPart.each( function(DataPart) { >> *this.myRecursiveFunction(DataPart);* // or whatever the args should be >> }.bind(this) ); > > > -- > Science answers questions; philosophy questions answers. > > >-- Ryan Gahl Manager, Senior Software Engineer Nth Penguin, LLC http://www.nthpenguin.com -- WebWidgetry.com / MashupStudio.com Future Home of the World''s First Complete Web Platform -- Inquire: 1-920-574-2218 Blog: http://www.someElement.com LinkedIn Profile: http://www.linkedin.com/in/ryangahl --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Moreover, #each (and most of enumerable methods) accepts "context" object as a second argument - since this is such a common need. SomeComplexDataStructure.DataPart.each( function(DataPart) { this.myRecursiveFunction(DataPart); }, this ); It''s also good to remember that using bind is a little slower than storing context in a separate variable. - kangax On May 27, 2:44 pm, "Ryan Gahl" <ryan.g...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> :) > > Both are good patterns to know about (mine of course only works if you are > using prototype while yours is good old javascript). > > On 5/27/08, Frederick Polgardy <f...-SMQUYeM9IBBWk0Htik3J/w@public.gmane.org> wrote: > > > > > > > Buy him a beer instead; this solution is a lot better than mine in this > > case. > > > On Tue, May 27, 2008 at 1:04 PM, Ryan Gahl <ryan.g...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >> Another way to do this could be to just use proto''s .bind(), assuming the > >> "myRecursiveFunction" is only ever called as an instance method (vs. calling > >> it statically against the MyNS.Package''s prototype)... > > >> So: > > >> SomeComplexDataStructure.DataPart.each( function(DataPart) { > >> *this.myRecursiveFunction(DataPart);* // or whatever the args should be > >> }.bind(this) ); > > > -- > > Science answers questions; philosophy questions answers. > > -- > Ryan Gahl > Manager, Senior Software Engineer > Nth Penguin, LLChttp://www.nthpenguin.com > -- > WebWidgetry.com / MashupStudio.com > Future Home of the World''s First Complete Web Platform > -- > Inquire: 1-920-574-2218 > Blog:http://www.someElement.com > LinkedIn Profile:http://www.linkedin.com/in/ryangahl--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Ah yes, new(ish) niceties :) -- good call. On 5/27/08, kangax <kangax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Moreover, #each (and most of enumerable methods) accepts "context" > object as a second argument - since this is such a common need. > > > SomeComplexDataStructure.DataPart.each( function(DataPart) { > this.myRecursiveFunction(DataPart); > > }, this ); > > It''s also good to remember that using bind is a little slower than > storing context in a separate variable. > > - kangax > > > > On May 27, 2:44 pm, "Ryan Gahl" <ryan.g...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > :) > > > > Both are good patterns to know about (mine of course only works if you > are > > using prototype while yours is good old javascript). > > > > > On 5/27/08, Frederick Polgardy <f...-SMQUYeM9IBBWk0Htik3J/w@public.gmane.org> wrote: > > > > > > > > > > > > > Buy him a beer instead; this solution is a lot better than mine in this > > > case. > > > > > > On Tue, May 27, 2008 at 1:04 PM, Ryan Gahl <ryan.g...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > >> Another way to do this could be to just use proto''s .bind(), assuming > the > > >> "myRecursiveFunction" is only ever called as an instance method (vs. > calling > > >> it statically against the MyNS.Package''s prototype)... > > > > >> So: > > > > >> SomeComplexDataStructure.DataPart.each( function(DataPart) { > > >> *this.myRecursiveFunction(DataPart);* // or whatever the args should > be > > >> }.bind(this) ); > > > > > -- > > > Science answers questions; philosophy questions answers. > > > > -- > > > Ryan Gahl > > Manager, Senior Software Engineer > > > Nth Penguin, LLChttp://www.nthpenguin.com > > > -- > > WebWidgetry.com / MashupStudio.com > > Future Home of the World''s First Complete Web Platform > > -- > > Inquire: 1-920-574-2218 > > Blog:http://www.someElement.com > > LinkedIn Profile:http://www.linkedin.com/in/ryangahl > > > >-- Ryan Gahl Manager, Senior Software Engineer Nth Penguin, LLC http://www.nthpenguin.com -- WebWidgetry.com / MashupStudio.com Future Home of the World''s First Complete Web Platform -- Inquire: 1-920-574-2218 Blog: http://www.someElement.com LinkedIn Profile: http://www.linkedin.com/in/ryangahl --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Oh yeah, I think I remember seeing that this was added. It''s fairly new in Prototype isn''t it? On Tue, May 27, 2008 at 2:01 PM, kangax <kangax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Moreover, #each (and most of enumerable methods) accepts "context" > object as a second argument - since this is such a common need. > > SomeComplexDataStructure.DataPart.each( function(DataPart) { > this.myRecursiveFunction(DataPart); > }, this );-- Science answers questions; philosophy questions answers. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Wow - I left this thread thinking it was dead - boy was I wrong ;-) Let me digest all that. I ended up just passing in anything I needed from the this scope as arguments to the nested function call. I will play around with these other approaches when I get some time. Thanks for the great info. On May 27, 12:48 pm, "Frederick Polgardy" <f...-SMQUYeM9IBBWk0Htik3J/w@public.gmane.org> wrote:> Oh yeah, I think I remember seeing that this was added. It''s fairly new in > Prototype isn''t it? > > On Tue, May 27, 2008 at 2:01 PM, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Moreover, #each (and most of enumerable methods) accepts "context" > > object as a second argument - since this is such a common need. > > > SomeComplexDataStructure.DataPart.each( function(DataPart) { > > this.myRecursiveFunction(DataPart); > > }, this ); > > -- > Science answers questions; philosophy questions answers.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
So, I went the route of passing in the context... after downloading the latest prototype.js version. It is working great and now I don''t have to pass around all those arguments. Thanks everyone! On May 28, 11:29 am, Jonathan Logue <jonathanlo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Wow - I left this thread thinking it was dead - boy was I wrong ;-) > Let me digest all that. I ended up just passing in anything I needed > from the this scope as arguments to the nested function call. I will > play around with these other approaches when I get some time. > > Thanks for the great info. > > On May 27, 12:48 pm, "Frederick Polgardy" <f...-SMQUYeM9IBBWk0Htik3J/w@public.gmane.org> wrote: > > > > > Oh yeah, I think I remember seeing that this was added. It''s fairly new in > > Prototype isn''t it? > > > On Tue, May 27, 2008 at 2:01 PM, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Moreover, #each (and most of enumerable methods) accepts "context" > > > object as a second argument - since this is such a common need. > > > > SomeComplexDataStructure.DataPart.each( function(DataPart) { > > > this.myRecursiveFunction(DataPart); > > > }, this ); > > > -- > > Science answers questions; philosophy questions answers.- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---