Hi Mitchell,

There's a couple of patches about this on trac already and it's
certainly worth discussing.

Originally these iterators are mapped on ruby ones, so it's worth
checking those out, and especially the changes coming up in ruby 1.9.

Best,

Tobie


On Jan 18, 12:11 am, mostly_magic <[EMAIL PROTECTED]> wrote:
> Hey all,
>
> I was playing with Hash recently and I came across some unexpected
> behavior.  When I called certain methods inherited from Enumerable
> (findAll and partition among others) they returned results as arrays
> instead of hashes.  After looking through the code I understand the
> cause of this, but I wonder if this should be the result.  I created a
> solution for this and wanted your thoughts and feedback.  I've placed
> the (abbreviated) code below:
>
> var Enumerable = {
>
>   // This function has been added to provide the default return type
>   _createNew: function() { return []; },
>
>   findAll: function(iterator, context) {
>     iterator = iterator.bind(context);
>
>     // Retrieve the enumerable type
>     var results = this._createNew();
>
>     this.each(function(value, index) {
>       if (iterator(value, index))
>         results.push(value);
>     });
>     return results;
>   }
>
> };
>
> var Hash = Class.create(Enumerable, (function() {
>   return {
>     // Override the default to return a hash object.
>     _createNew: function() { return $H({}); },
>
>     // Provide an implementation for Array.push to store items.
>     push: function(value) {
>       this.set(value.key, value.value);
>     }
>   }
>
> })());
>
> There's still some grey areas about this, such as whether the slices
> in eachSlice should be hashes and whether collect and grep should
> associate the new values with the keys.  I also haven't given a lot of
> thought about how to maintain backwards compatibility.  I wanted to
> put it out there, though, to see what others think of it.
>
> Cheers,
> Mitchell Cowie
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to