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" <[EMAIL PROTECTED]> 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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---