Just curious what people''s opinions are on trimming items from an array. In my case this is an array of objects, not just a simple array of integers or something. Here are a few possibilities.. $A([1, 2, 3 4, 5]).collect(function(item){ if (item != 4) return true; }).compact(); // I''m concerned about this solution, for performance/memory reasons on a collection of large objects $A([1, 2, 3, 4, 5]).without(4); // not sure how this would work on a collection of objects $A([1, 2, 3, 4, 5]).reject(function(item{ if (item == 4) return true; }); // nice since I don''t have to compact Here''s a really contrived example of my usage: http://pastie.textmate.org/192080 Thanks for reading. -justin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Justin, there''s no need to "wrap" array with $A. Array.prototype is already extended with Enumerable. I''m not sure why you would want to "compact" a result in first example: [1,2,3,4,5].findAll(function(n) { return n != 4; }); // [1,2,3,5] // or [1,2,3,4,5].reject(function(n) { return n == 4; }) // [1,2,3,5] // as well as: [1,2,3,4,5].without(4); // [1,2,3,5] #without is generally slow when it comes to huge arrays. For best performance it''s obviously better to stick to native methods: var arr = [1,2,3,4,5]; arr.splice(arr.indexOf(4), 1); arr; // [1,2,3,5] Regarding your snippet, what about something like: var SomeClass = Class.create({ initialize: function(index) { this.index = index; } }) // it might sense to use iterator-like "proxy" for filtering SomeClass.condition = function(value, index) { return index % 2; } var SomeClassManager = { initialize: function() { this.objects = []; // you could use #times as an alternative (11).times(function(i) { this.objects[i] = new SomeClass(i); }.bind(this)) }, trim: function() { this.objects = this.objects.reject(SomeClass.condition); } } SomeClassManager.initialize(); SomeClassManager.objects.size(); // 11 SomeClassManager.trim(); SomeClassManager.objects.size(); // 6 Best, kangax On May 5, 6:56 pm, "Justin Perkins" <justinperk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Just curious what people''s opinions are on trimming items from an > array. In my case this is an array of objects, not just a simple array > of integers or something. > > Here are a few possibilities.. > > $A([1, 2, 3 4, 5]).collect(function(item){ if (item != 4) return true;}).compact(); // I''m concerned about this solution, for > > performance/memory reasons on a collection of large objects > > $A([1, 2, 3, 4, 5]).without(4); // not sure how this would work on a > collection of objects > > $A([1, 2, 3, 4, 5]).reject(function(item{ if (item == 4) return true; > > }); // nice since I don''t have to compact > > Here''s a really contrived example of my usage:http://pastie.textmate.org/192080 > > Thanks for reading. > > -justin--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Mon, May 5, 2008 at 7:04 PM, kangax <kangax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Justin, > there''s no need to "wrap" array with $A. Array.prototype is alreadyThanks for the tip, I''m paranoid. IE has scarred me. I''ll try to remember that. I usually initialize with this.foo = $A(), so that''s what I was doing in my example.> I''m not sure why you would want to "compact" a result in firstIf you don''t do that, then don''t you end up with an undefined wherever an item was pulled out of the array? Such as in my example: [1,2,3,undefined,5] I''m getting mixed results in firebug and my ruby coding has trained me to compact my arrays.> #without is generally slow when it comes to huge arrays.Good to know.> For best performance it''s obviously better to stick to native methods: > > var arr = [1,2,3,4,5]; > arr.splice(arr.indexOf(4), 1); > arr; // [1,2,3,5]I wasn''t aware of a splice method. After explaining my problem a co-worker was talking about splicing and lamented that JavaScript probably did not support a splice method natively :p> Regarding your snippet, what about something like: > // it might sense to use iterator-like "proxy" for filtering > SomeClass.condition = function(value, index) { > return index % 2; > } > > var SomeClassManager = { > trim: function() { > this.objects = this.objects.reject(SomeClass.condition); > } > } > SomeClassManager.trim(); > SomeClassManager.objects.size(); // 6Thanks, I think that is what I need. What I''m doing here is creating a new instance of my class for some special DOM elements on a page and I want a way to determine if the element is still in the page, so I have a isOrphaned() method on the class, and when that method returns true then I call that object''s destroy method and then rip it out of the array. I was trying to avoid cloning the enumerable, hence the original question. -justin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Can you explain this proxy method a bit more for me...> Regarding your snippet, what about something like: > > var SomeClass = Class.create({ > initialize: function(index) { > this.index = index; > } > }) > > // it might sense to use iterator-like "proxy" for filtering > SomeClass.condition = function(value, index) { > return index % 2; > }Is there a way to manually invoke this method or can it only be used as an iterator? -justin --~--~---------~--~----~------------~-------~--~----~ 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''m pretty sure Array.splice is a native method, there is documentation on the w3 schools site. http://www.w3schools.com/jsref/jsref_splice.asp -- Matt Foster Ajax Engineer Nth Penguin, LLC http://www.nthpenguin.com On May 5, 11:59 pm, "Justin Perkins" <justinperk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mon, May 5, 2008 at 7:04 PM, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Justin, > > there''s no need to "wrap" array with $A. Array.prototype is already > > Thanks for the tip, I''m paranoid. IE has scarred me. I''ll try to > remember that. I usually initialize with this.foo = $A(), so that''s > what I was doing in my example. > > > I''m not sure why you would want to "compact" a result in first > > If you don''t do that, then don''t you end up with an undefined wherever > an item was pulled out of the array? Such as in my example: > [1,2,3,undefined,5] > > I''m getting mixed results in firebug and my ruby coding has trained me > to compact my arrays. > > > #without is generally slow when it comes to huge arrays. > > Good to know. > > > For best performance it''s obviously better to stick to native methods: > > > var arr = [1,2,3,4,5]; > > arr.splice(arr.indexOf(4), 1); > > arr; // [1,2,3,5] > > I wasn''t aware of a splice method. After explaining my problem a > co-worker was talking about splicing and lamented that JavaScript > probably did not support a splice method natively :p > > > Regarding your snippet, what about something like: > > // it might sense to use iterator-like "proxy" for filtering > > SomeClass.condition = function(value, index) { > > return index % 2; > > } > > > var SomeClassManager = { > > trim: function() { > > this.objects = this.objects.reject(SomeClass.condition); > > } > > } > > SomeClassManager.trim(); > > SomeClassManager.objects.size(); // 6 > > Thanks, I think that is what I need. > > What I''m doing here is creating a new instance of my class for some > special DOM elements on a page and I want a way to determine if the > element is still in the page, so I have a isOrphaned() method on the > class, and when that method returns true then I call that object''s > destroy method and then rip it out of the array. > > I was trying to avoid cloning the enumerable, hence the original question. > > -justin--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The "proxy" is being made this way so that you can simply pass it to most of enumerable methods - i.e. "function iterator(value, index) { return <something> }". You can of course call it on its own. - kangax On May 6, 9:57 am, "Justin Perkins" <justinperk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Can you explain this proxy method a bit more for me... > > > Regarding your snippet, what about something like: > > > var SomeClass = Class.create({ > > initialize: function(index) { > > this.index = index; > > } > > }) > > > // it might sense to use iterator-like "proxy" for filtering > > SomeClass.condition = function(value, index) { > > return index % 2; > > } > > Is there a way to manually invoke this method or can it only be used > as an iterator? > > -justin--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> The "proxy" is being made this way so that you can simply pass it to > most of enumerable methods - i.e. "function iterator(value, index)Yeah I figured it out shortly after I posted that last message, as you might have guessed I''ve never used a named function for an iterator before, but it seems quite handy. Thanks for the tip. -justin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---