I''ve noticed there are a few tricks to using the Enumerable Filtering functions, so before I write any unneeded code, I have a quick question. Are there any quick ways to invoke a method for each value in an enumerable and filter the list based on the results of that call? Something that might work like this: var result = $(''elemID'', ''elemID'').invokeAndFilter( ''hasClassName'', ''nameOfClass'' ); Or maybe something like this: var result = $(''elemID'', ''elemID'').findAll( Function.invoke( ''hasClassName'', ''nameOfClass'' ) ); Thanks for any help you can offer. I hope I''m missing something simple. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hey Nycto, Except if you''re running into significant performance problems over fairly large nodesets, I fail to see why the custom code below would not satisfy you (here with no assumption as to the extended status of the elements you have): nodeArray.findAll(function(e) { return Element.hasClassName(e, ''nameOfClass''); }) Or assuming extension: nodeArray.findAll(function(e) { return e.hasClassName(''nameOfClass''); }) -- Christophe Porteneuve a.k.a. TDD "[They] did not know it was impossible, so they did it." --Mark Twain Email: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
It would absolutely work, but it seemed like a common enough situation that a shorthand solution might be available. Thanks for your response! - Nycto On May 6, 1:32 pm, Christophe Porteneuve <t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> wrote:> Hey Nycto, > > Except if you''re running into significant performance problems over > fairly large nodesets, I fail to see why the custom code below would not > satisfy you (here with no assumption as to the extended status of the > elements you have): > > nodeArray.findAll(function(e) { > return Element.hasClassName(e, ''nameOfClass''); }) > > Or assuming extension: > > nodeArray.findAll(function(e) { return e.hasClassName(''nameOfClass''); }) > > -- > Christophe Porteneuve a.k.a. TDD > "[They] did not know it was impossible, so they did it." --Mark Twain > Email: t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Nycto wrote:> It would absolutely work, but it seemed like a common enough situation > that a shorthand solution might be available.If you''re a player and really want to get from:> nodeArray.findAll(function(e) { return e.hasClassName(''nameOfClass''); })to: nodeArray.findAll(''hasClassName'', ''nameOfClass''); you''ll have to hack Enumerable. Here''s an example: // after loading Prototype, apply your tweaks (separate .js file, etc): Object.extend(Enumerable, { // iterator maker - if the argument it''s not a Function, // assume it''s a String containing the method name iterator: function(iterator) { if (typeof iterator === ''function'') return iterator; var args = Array.prototype.slice.apply(arguments, [1]); return function(value){return value[iterator].apply(value, args)}; }, // redefine ''findAll'', merely by making it call "the iterator maker": findAll: function() { var results = [], iterator = this.iterator.apply(this,arguments); this.each(function(value, index) { if (iterator(value, index)) results.push(value); }); return results; } }); // apply the "new" Enumerable to all desired objects (Array, Hash, etc): Object.extend(Array.prototype, Enumerable); I know, it''s pretty dirty - let''s call it a proof of concept - I wouldn''t recommend using it unless you heavily use this kind of filtering. Even then, maybe it''s time to search for a different approach, as it surely looks like having performance problems (given Enumerable''s "speed"). cheers - -- Marius Feraru -----BEGIN PGP SIGNATURE----- iD4DBQFGPmhatZHp/AYZiNkRAm0vAJUe1FxMIWPxPz9ZvooUcoYyWGgPAJ0YVACT CSxXDasRIQEypkXqQ9Qm3A==adCB -----END PGP SIGNATURE----- --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---