I agree with you, Simon. The solutions that iterate over the loop 2x have
poorer BigO than the grep method. That is, as the array size increases, the
two iterations are O(N^2), while the grep method is more on the order of
O(N).
On Tue, Sep 16, 2008 at 9:08 AM, Simon Charette [EMAIL PROTECTED]wrote:
Sorry Yannick,
but even if I agree with you that findAll().each() is the 'Prototype' way
which i would prefer to the grep method i suggested.
It still creates two iterations while the grep solutions doesn't.
Thats the main reason why i suggested this formula.
2008/9/16 Yanick [EMAIL PROTECTED]
I have seen two responses using filter().each() (or grep().each()) and
I just wanted to say that, IMHO, this is not good practice, and thus
should not be recommanded as this creates two iterations over the same
array for the same purpose and encourages bad coding habbits. The
question is not would this work? but rather is this the best way?
The OP must've felt the same way, otherwise he would've used such
double sequential loops way in the first place. In fact, what T.J.
Crowder suggested is the most Prototype way. I hope we'll all learn
from this :)
yanick
On Sep 15, 12:24 pm, Grant Hutchins [EMAIL PROTECTED] wrote:
It seems to me that Enumerable#findAll (aliased as select) is what you
want for your first example.
myArray.findAll( function(obj){ obj.className ==
'MyProtoClass' } ).each( function(obj){ obj.doStuff } );
This way you're using the official Prototype way to do these
operations.
Grant
On Sep 11, 6:17 pm, EMoreth [EMAIL PROTECTED] wrote:
Working with prototype from a long time ago I always need a function
that could do something just like the Strongly Type languages, as Java
or many others, that is a for ( ObjectClass object in
arrayLikeStructure ) to get all objects from that array that are from
the class ObjectClass and do something...
Since Javascript does not have this kind of Typed Class its is very
hard and annoying to do such thing... I recently tought two ways that
could make it work...
First (and simpler) is a foreach function that applyes on Enumerable
items and Accepts two functions.. the first one is a boolean that will
tell the code when the object is the class/type that I want, and the
second function should be the action at real... to apply in that
object. Something like :
myArray.foreach(function(obj){ return obj.className ==
'MyProtoClass'}, function(obj) { obj.doStuff() })
The second one is just a little different (more Java-Like)... in this
case we would need a previous custom method on all objects that would
say if this one matches a defined parameter or not... something like
a.className = 'MyClass';
b.className = 'MyProtoClass';
a.isMyObject = function(check) { return this.className == check }
b.isMyObject = function(check) { return this.className == check }
myArray.foreach( 'MyProtoClass', function(obj) { obj.doStuff() })
and it would only apply on b.doStuff()
I cant tell if this would have the same utility for everyone that
would have for me... But i thing that would be very good for people
who are used to other Typed Languages...
Please tell me what you think about that..
EMoreth
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
Prototype: Core group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/prototype-core?hl=en
-~--~~~~--~~--~--~---