I often see people using fancy library methods, like Prototype''s Enumerable.each(), to loop over arrays. What''s the point of this? Why not just do a standard for loop, like so: for ( var i = 0; i < myArray.length; i++ ) { // ... } It seems like it''d be faster, since it''s working closer to the bare metal. Am I wrong about this, or is there some other nuance I''m missing? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
In some cases, yes, it''s pure sugar. In some cases, yes, you may notice a speed difference. But only if you''re looping through a megaton of items and doing a supermegaton of processing. In most case, it''s effect on performance is not worth mentioning. The main point of it, though, is it''s a nice clean way to iterate a collection AND have a new local scope for each iteration of the loop. The closure provided by .each(fn(item, index)) comes in very handy in many cases. Plus: for (var i = 0; i < blah.length; i++) { var item = blah[i]; doSomething(item); } is more verbose than: blah.each(doSomething); And finally, you''re right that .each is one of the lesser value-add methods in Enumerable. Certainly .invoke, .collect, etc... are doing more work. But personally I like the "predicate and delegate" patterns - also prevalent in C# and Java (via Generics) - that Enumerable provides with it methods, including each. I''ve been bitten several times with a for loop that didn''t quite work as expected because I, or someone, forgot about the absence of block level scoping in javascript. On Mon, Jun 30, 2008 at 7:10 PM, greenie2600 <greenie2600-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> > I often see people using fancy library methods, like Prototype''s > Enumerable.each(), to > loop over arrays. > > What''s the point of this? Why not just do a standard for loop, like > so: > > for ( var i = 0; i < myArray.length; i++ ) { > // ... > } > > It seems like it''d be faster, since it''s working closer to the bare > metal. Am I wrong about this, or is there some other nuance I''m > missing? > > > >-- 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 -~----------~----~----~----~------~----~------~--~---
Ah. The bit about creating a clean scope for each iteration makes sense. I''ve been simulating that by resetting my "block-level" variables to null at the top of the loop. Thanks! I''ll keep this in mind. On Jun 30, 8:27 pm, "Ryan Gahl" <ryan.g...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> In some cases, yes, it''s pure sugar. > > In some cases, yes, you may notice a speed difference. But only if you''re > looping through a megaton of items and doing a supermegaton of processing. > > In most case, it''s effect on performance is not worth mentioning. > > The main point of it, though, is it''s a nice clean way to iterate a > collection AND have a new local scope for each iteration of the loop. The > closure provided by .each(fn(item, index)) comes in very handy in many > cases. > > Plus: > > for (var i = 0; i < blah.length; i++) { > var item = blah[i]; > doSomething(item); > > } > > is more verbose than: > > blah.each(doSomething); > > And finally, you''re right that .each is one of the lesser value-add methods > in Enumerable. Certainly .invoke, .collect, etc... are doing more work. But > personally I like the "predicate and delegate" patterns - also prevalent in > C# and Java (via Generics) - that Enumerable provides with it methods, > including each. > > I''ve been bitten several times with a for loop that didn''t quite work as > expected because I, or someone, forgot about the absence of block level > scoping in javascript. > > > > On Mon, Jun 30, 2008 at 7:10 PM, greenie2600 <greenie2...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > > I often see people using fancy library methods, like Prototype''s > > Enumerable.each(), to > > loop over arrays. > > > What''s the point of this? Why not just do a standard for loop, like > > so: > > > for ( var i = 0; i < myArray.length; i++ ) { > > // ... > > } > > > It seems like it''d be faster, since it''s working closer to the bare > > metal. Am I wrong about this, or is there some other nuance I''m > > missing? > > -- > 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 -~----------~----~----~----~------~----~------~--~---
I don''t know if I could ever go back to programming in a language without closures anymore. :-) That used to bite me so many times in JS, mindlessly assuming that a block has a closure. -Fred On Mon, Jun 30, 2008 at 7:33 PM, greenie2600 <greenie2600-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> > Ah. The bit about creating a clean scope for each iteration makes > sense. I''ve been simulating that by resetting my "block-level" > variables to null at the top of the loop. > > Thanks! I''ll keep this in mind.-- 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 -~----------~----~----~----~------~----~------~--~---