Hello everyone.
I am not sure if this is the right place to post, please redirect me
if there is a mailinglist more specifically dealing with prototype.js.
After playing around with the very inspiring prototype library I have
found a rather annoying thing, that makes the usefulness of the
Enumerable mixin for Hashes
questionable, if I am not totally wrong (again :-).
Here an example:
var a = $H({a:1, b:2, c:1, d:3});
document.writeln(a.inspect().escapeHTML());
// yields: #<Hash:{''a'': 1, ''b'': 2,
''c'': 1, ''d'': 3}>
var b = a.reject(function(val){ return (val[1]==1) });
document.writeln(b.inspect().escapeHTML());
// yields: [[''b'', 2], [''d'', 3]]
// expected: #<Hash:{''b'': 2, ''d'': 3}>
The potential beauty of the Hash reject() method is obvious, but the
returned value (b) is not a Hash anymore!
It is an Array, very close to the intended Hash, but not a Hash.
I suppose the solution would be, to overload some more functions for
the Enumerable mixin, so that the would be an _addElement() method
for all Classes, that wish to mixin Enumerable and the reject method
(as other Enumerable methods) would call that as opposed to always
creating an Array internally.
Any ideas or comments?
Sincerely yours
Alexander Presber
Why not just do...
var b = $H(a.reject(function(val){ return (val[1]==1) }));
? The reject method returning an array makes it flexible (arrays can be
enumerables too)
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of
Alexander Presber
Sent: Monday, January 23, 2006 4:21 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: [Rails-spinoffs] prototype.js: enumerable mixin for hash broken
Hello everyone.
I am not sure if this is the right place to post, please redirect me
if there is a mailinglist more specifically dealing with prototype.js.
After playing around with the very inspiring prototype library I have
found a rather annoying thing, that makes the usefulness of the
Enumerable mixin for Hashes
questionable, if I am not totally wrong (again :-).
Here an example:
var a = $H({a:1, b:2, c:1, d:3});
document.writeln(a.inspect().escapeHTML());
// yields: #<Hash:{''a'': 1, ''b'': 2,
''c'': 1, ''d'': 3}>
var b = a.reject(function(val){ return (val[1]==1) });
document.writeln(b.inspect().escapeHTML());
// yields: [[''b'', 2], [''d'', 3]]
// expected: #<Hash:{''b'': 2, ''d'': 3}>
The potential beauty of the Hash reject() method is obvious, but the
returned value (b) is not a Hash anymore!
It is an Array, very close to the intended Hash, but not a Hash.
I suppose the solution would be, to overload some more functions for
the Enumerable mixin, so that the would be an _addElement() method
for all Classes, that wish to mixin Enumerable and the reject method
(as other Enumerable methods) would call that as opposed to always
creating an Array internally.
Any ideas or comments?
Sincerely yours
Alexander Presber
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
The information transmitted in this electronic mail is intended only for the
person or entity to which it is addressed and may contain confidential,
proprietary, and/or privileged material. Any review, retransmission,
dissemination or other use of, or taking of any action in reliance upon,
this information by persons or entities other than the intended recipient
is prohibited. If you received this in error, please contact the sender and
delete the material from all computers.
Alexander Presber
2006-Jan-23 17:37 UTC
Re: prototype.js: enumerable mixin for hash broken
Because your solution yields
#<Hash:{''0'': [''b'', 2],
''1'': [''d'', 3]}>
instead of
#<Hash:{''b'': 2, ''d'': 3}>
In my opinion the difference is crucial. Remember the original Hash was
#<Hash:{''a'': 1, ''b'': 2,
''c'': 1, ''d'': 3}>
and by using the reject function I wanted to drop all hash elements
with value 1.
In Ruby (from which the inspiration for the Enumerable Mixin stems),
the reject method sure does return a hash, not an array, when acting
on an hash:
irb(main):001:0> a = {:a => 1, :b => 2, :c => 1, :d => 3}
=> {:b=>2, :c=>1, :a=>1, :d=>3}
irb(main):002:0> b = a.reject{|k,v| v==1}
=> {:b=>2, :d=>3}
The result your solution gives is of no real use, because I cannot
use it like the original hash (with some elements rejected).
I see no reason in providing an Enumerable "mixin" class, that I
cannot truly mix into another class except for Array.
It seems to defeat the purpose of an abstract mixin. I have a class
that does support an each method and want to get rid of some elements
(using a function to determine which)?
reject should do just that, shouldn''t it?
Sincerely yours
Alexander Presber
> Why not just do...
>
> var b = $H(a.reject(function(val){ return (val[1]==1) }));
>
> ? The reject method returning an array makes it flexible (arrays
> can be
> enumerables too)
>
> -----Original Message-----
> From:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of
> Alexander Presber
> Sent: Monday, January 23, 2006 4:21 AM
> To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> Subject: [Rails-spinoffs] prototype.js: enumerable mixin for hash
> broken
>
> Hello everyone.
>
> I am not sure if this is the right place to post, please redirect me
> if there is a mailinglist more specifically dealing with prototype.js.
>
> After playing around with the very inspiring prototype library I have
> found a rather annoying thing, that makes the usefulness of the
> Enumerable mixin for Hashes
> questionable, if I am not totally wrong (again :-).
>
> Here an example:
>
> var a = $H({a:1, b:2, c:1, d:3});
> document.writeln(a.inspect().escapeHTML());
> // yields: #<Hash:{''a'': 1, ''b'': 2,
''c'': 1, ''d'': 3}>
>
> var b = a.reject(function(val){ return (val[1]==1) });
> document.writeln(b.inspect().escapeHTML());
> // yields: [[''b'', 2], [''d'', 3]]
> // expected: #<Hash:{''b'': 2, ''d'':
3}>
>
> The potential beauty of the Hash reject() method is obvious, but the
> returned value (b) is not a Hash anymore!
> It is an Array, very close to the intended Hash, but not a Hash.
>
> I suppose the solution would be, to overload some more functions for
> the Enumerable mixin, so that the would be an _addElement() method
> for all Classes, that wish to mixin Enumerable and the reject method
> (as other Enumerable methods) would call that as opposed to always
> creating an Array internally.
>
> Any ideas or comments?
>
> Sincerely yours
> Alexander Presber
Ohhh. I didn''t realize that. Well I can see the solution, but it
involves overriding the reject method in the Hash object. If I were you,
I''d do that and submit the patch for inclusion in the next release.
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of
Alexander Presber
Sent: Monday, January 23, 2006 11:37 AM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: [Rails-spinoffs] Re: prototype.js: enumerable mixin for hash
broken
Because your solution yields
#<Hash:{''0'': [''b'', 2],
''1'': [''d'', 3]}>
instead of
#<Hash:{''b'': 2, ''d'': 3}>
In my opinion the difference is crucial. Remember the original Hash was
#<Hash:{''a'': 1, ''b'': 2,
''c'': 1, ''d'': 3}>
and by using the reject function I wanted to drop all hash elements
with value 1.
In Ruby (from which the inspiration for the Enumerable Mixin stems),
the reject method sure does return a hash, not an array, when acting
on an hash:
irb(main):001:0> a = {:a => 1, :b => 2, :c => 1, :d => 3}
=> {:b=>2, :c=>1, :a=>1, :d=>3}
irb(main):002:0> b = a.reject{|k,v| v==1}
=> {:b=>2, :d=>3}
The result your solution gives is of no real use, because I cannot
use it like the original hash (with some elements rejected).
I see no reason in providing an Enumerable "mixin" class, that I
cannot truly mix into another class except for Array.
It seems to defeat the purpose of an abstract mixin. I have a class
that does support an each method and want to get rid of some elements
(using a function to determine which)?
reject should do just that, shouldn''t it?
Sincerely yours
Alexander Presber
> Why not just do...
>
> var b = $H(a.reject(function(val){ return (val[1]==1) }));
>
> ? The reject method returning an array makes it flexible (arrays
> can be
> enumerables too)
>
> -----Original Message-----
> From:
rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
>
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of
> Alexander Presber
> Sent: Monday, January 23, 2006 4:21 AM
> To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> Subject: [Rails-spinoffs] prototype.js: enumerable mixin for hash
> broken
>
> Hello everyone.
>
> I am not sure if this is the right place to post, please redirect me
> if there is a mailinglist more specifically dealing with prototype.js.
>
> After playing around with the very inspiring prototype library I have
> found a rather annoying thing, that makes the usefulness of the
> Enumerable mixin for Hashes
> questionable, if I am not totally wrong (again :-).
>
> Here an example:
>
> var a = $H({a:1, b:2, c:1, d:3});
> document.writeln(a.inspect().escapeHTML());
> // yields: #<Hash:{''a'': 1, ''b'': 2,
''c'': 1, ''d'': 3}>
>
> var b = a.reject(function(val){ return (val[1]==1) });
> document.writeln(b.inspect().escapeHTML());
> // yields: [[''b'', 2], [''d'', 3]]
> // expected: #<Hash:{''b'': 2, ''d'':
3}>
>
> The potential beauty of the Hash reject() method is obvious, but the
> returned value (b) is not a Hash anymore!
> It is an Array, very close to the intended Hash, but not a Hash.
>
> I suppose the solution would be, to overload some more functions for
> the Enumerable mixin, so that the would be an _addElement() method
> for all Classes, that wish to mixin Enumerable and the reject method
> (as other Enumerable methods) would call that as opposed to always
> creating an Array internally.
>
> Any ideas or comments?
>
> Sincerely yours
> Alexander Presber
_______________________________________________
Rails-spinoffs mailing list
Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
The information transmitted in this electronic mail is intended only for the
person or entity to which it is addressed and may contain confidential,
proprietary, and/or privileged material. Any review, retransmission,
dissemination or other use of, or taking of any action in reliance upon,
this information by persons or entities other than the intended recipient
is prohibited. If you received this in error, please contact the sender and
delete the material from all computers.