[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Hash#slice

ara.t.howard

4/12/2008 9:01:00 PM


is this consistent with other peoples stdlib hacks?

class Hash
def slice *keys, &block
if block
each do |key, val|
boolean = block.call(key, val)
keys << key if boolean
end
end
hash = self
keys.inject({}){|returned, key| returned.update key => hash[key]}
end
end


??


a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




9 Answers

Trans

4/12/2008 11:12:00 PM

0


On Apr 12, 5:00 pm, ara howard <ara.t.how...@gmail.com> wrote:
> is this consistent with other peoples stdlib hacks?
>
> class Hash
> def slice *keys, &block
> if block
> each do |key, val|
> boolean = block.call(key, val)
> keys << key if boolean
> end
> end
> hash = self
> keys.inject({}){|returned, key| returned.update key => hash[key]}
> end
> end
>
> ??

Let see.... Facets:

class Hash

# Returns a new hash with only the given keys.

def slice(*keep_keys)
h = {}
keep_keys.each do |key|
h[key] = fetch(key)
end
h
end

# Replaces hash with a new hash having only the given keys.
# This return the hash of keys removed.

def slice!(*keep_keys)
removed = except(*keep_keys)
replace(slice(*keep_keys))
removed
end

end

T.

ara.t.howard

4/13/2008 12:51:00 AM

0


On Apr 12, 2008, at 5:12 PM, Trans wrote:
> Let see.... Facets:
>
> class Hash
>
> # Returns a new hash with only the given keys.
>
> def slice(*keep_keys)
> h = {}
> keep_keys.each do |key|
> h[key] = fetch(key)
> end
> h
> end
>
> # Replaces hash with a new hash having only the given keys.
> # This return the hash of keys removed.
>
> def slice!(*keep_keys)
> removed = except(*keep_keys)
> replace(slice(*keep_keys))
> removed
> end
>
> end

okay, you should add the block form ;-)

headers.slice{|k,| k =~ %r/^HTTP_/}

handy.....

cheers.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Daniel Berger

4/13/2008 6:15:00 AM

0



On Apr 12, 3:00 pm, ara howard <ara.t.how...@gmail.com> wrote:
> is this consistent with other peoples stdlib hacks?
>
> class Hash
> def slice *keys, &block
> if block
> each do |key, val|
> boolean = block.call(key, val)
> keys << key if boolean
> end
> end
> hash = self
> keys.inject({}){|returned, key| returned.update key => hash[key]}
> end
> end

http://raa.ruby-lang.org/project/...

Regards,

Dan

Robert Dober

4/13/2008 8:59:00 AM

0

On Sun, Apr 13, 2008 at 2:51 AM, ara.t.howard <ara.t.howard@gmail.com> wrote:
>
> On Apr 12, 2008, at 5:12 PM, Trans wrote:
>
> > Let see.... Facets:
> >
> > class Hash
> >
> > # Returns a new hash with only the given keys.
> >
> > def slice(*keep_keys)
> > h = {}
> > keep_keys.each do |key|
> > h[key] = fetch(key)
> > end
> > h
> > end
> >
> > # Replaces hash with a new hash having only the given keys.
> > # This return the hash of keys removed.
> >
> > def slice!(*keep_keys)
> > removed = except(*keep_keys)
> > replace(slice(*keep_keys))
> > removed
> > end
> >
> > end
> >
>
> okay, you should add the block form ;-)
>
> headers.slice{|k,| k =~ %r/^HTTP_/}
>
> handy.....
>
> cheers.
>
>
>
> a @ http://codeforp...
> --
> we can deny everything, except that we have the possibility of being
> better. simply reflect on that.
> h.h. the 14th dalai lama
>
>
>
>
>
Hmm maybe it is a more consistent approach not to add a block to slice.

FWIAC, I use hselect, kselect and vselect, the simplified code
goes like this (1) of course
def kselect &blk
hselect{ |k,| blk.call(k) }
end
def vselect &blk
hselect{ |_,v| blk.call(v) }
end
def hselect &blk
#almost Tom's code of course
end

You might write
h.slice(*[*1..10]){|k,v| whatever k, v }
which indeed I like

I would write
h.slice(*[*1..10]).hselect{|k,v| whatever k, v}
which indeed I like less, *but* I dislike the inconsistency of #select now.

The ideal solution might be to add a block to Enumerator#select too,
what about that?

Cheers
Robert


(1) simplified because the magic dot notation implementing proxy is of
no interest here.


--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Trans

4/13/2008 10:04:00 AM

0



On Apr 13, 4:58 am, "Robert Dober" <robert.do...@gmail.com> wrote:
> On Sun, Apr 13, 2008 at 2:51 AM, ara.t.howard <ara.t.how...@gmail.com> wrote:
>
> > On Apr 12, 2008, at 5:12 PM, Trans wrote:
>
> > > Let see.... Facets:
>
> > > class Hash
>
> > > # Returns a new hash with only the given keys.
>
> > > def slice(*keep_keys)
> > > h = {}
> > > keep_keys.each do |key|
> > > h[key] = fetch(key)
> > > end
> > > h
> > > end
>
> > > # Replaces hash with a new hash having only the given keys.
> > > # This return the hash of keys removed.
>
> > > def slice!(*keep_keys)
> > > removed = except(*keep_keys)
> > > replace(slice(*keep_keys))
> > > removed
> > > end
>
> > > end
>
> > okay, you should add the block form ;-)
>
> > headers.slice{|k,| k =~ %r/^HTTP_/}
>
> > handy.....
>
> > cheers.
>
> > a @http://codeforp...
> > --
> > we can deny everything, except that we have the possibility of being
> > better. simply reflect on that.
> > h.h. the 14th dalai lama
>
> Hmm maybe it is a more consistent approach not to add a block to slice.
>
> FWIAC, I use hselect, kselect and vselect, the simplified code
> goes like this (1) of course
> def kselect &blk
> hselect{ |k,| blk.call(k) }
> end
>
> def vselect &blk
> hselect{ |_,v| blk.call(v) }
> end
> def hselect &blk
> #almost Tom's code of course
> end

Why not?

hash.keys.select
hash.values.select

> You might write
> h.slice(*[*1..10]){|k,v| whatever k, v }
> which indeed I like
>
> I would write
> h.slice(*[*1..10]).hselect{|k,v| whatever k, v}
> which indeed I like less, *but* I dislike the inconsistency of #select now.
>
> The ideal solution might be to add a block to Enumerator#select too,
> what about that?

Hmm... I don't understand Enumerator#select comes from
Enumerable#select and already takes a block.

T.

Robert Dober

4/13/2008 12:22:00 PM

0

On Sun, Apr 13, 2008 at 12:04 PM, Trans <transfire@gmail.com> wrote:
>
>
> On Apr 13, 4:58 am, "Robert Dober" <robert.do...@gmail.com> wrote:
>
>
> > On Sun, Apr 13, 2008 at 2:51 AM, ara.t.howard <ara.t.how...@gmail.com> wrote:
> >
> > > On Apr 12, 2008, at 5:12 PM, Trans wrote:
> >
> > > > Let see.... Facets:
> >
> > > > class Hash
> >
> > > > # Returns a new hash with only the given keys.
> >
> > > > def slice(*keep_keys)
> > > > h = {}
> > > > keep_keys.each do |key|
> > > > h[key] = fetch(key)
> > > > end
> > > > h
> > > > end
> >
> > > > # Replaces hash with a new hash having only the given keys.
> > > > # This return the hash of keys removed.
> >
> > > > def slice!(*keep_keys)
> > > > removed = except(*keep_keys)
> > > > replace(slice(*keep_keys))
> > > > removed
> > > > end
> >
> > > > end
> >
> > > okay, you should add the block form ;-)
> >
> > > headers.slice{|k,| k =~ %r/^HTTP_/}
> >
> > > handy.....
> >
> > > cheers.
> >
> > > a @http://codeforp...
> > > --
> > > we can deny everything, except that we have the possibility of being
> > > better. simply reflect on that.
> > > h.h. the 14th dalai lama
> >
> > Hmm maybe it is a more consistent approach not to add a block to slice.
> >
> > FWIAC, I use hselect, kselect and vselect, the simplified code
> > goes like this (1) of course
> > def kselect &blk
> > hselect{ |k,| blk.call(k) }
> > end
> >
> > def vselect &blk
> > hselect{ |_,v| blk.call(v) }
> > end
> > def hselect &blk
> > #almost Tom's code of course
> > end
>
> Why not?
>
> hash.keys.select
> hash.values.select
We were talking about getting sliced hashes not sliced key or value
arrays. Maybe in the future there will be a saying
"the best thing since the invention of sliced hashes by Ara T. Howard"
the real inventor gets always forgotten sorry for Facets I can not
change future history ;)
>
>
> > You might write
> > h.slice(*[*1..10]){|k,v| whatever k, v }
> > which indeed I like
> >
> > I would write
> > h.slice(*[*1..10]).hselect{|k,v| whatever k, v}
> > which indeed I like less, *but* I dislike the inconsistency of #select now.
> >
> > The ideal solution might be to add a block to Enumerator#select too,
> > what about that?
>
> Hmm... I don't understand Enumerator#select comes from
> Enumerable#select and already takes a block.
Stupid me *again* I meant Array#slice. I feel that my brain is
terribly disconnected from the keyboard, (I believe that Scotty was
right when talking into the mouse LOL). Well proofreading my posts
would be an option though :).

I will express myself in Ruby

assert_nothing_raised ("You see it would be nice that Array#slice and
Hash#slice had (almost) the same interface") do
[*1..100].slice(1..10){ |x| x.zero? }
end


Sorry
R.
--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

ara.t.howard

4/13/2008 3:01:00 PM

0


On Apr 13, 2008, at 4:04 AM, Trans wrote:
>
> Why not?
>
> hash.keys.select
> hash.values.select

because the goal is to return a hash, not an array.... i think some
wires got crossed though ;-)

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




ara.t.howard

4/13/2008 3:02:00 PM

0


On Apr 13, 2008, at 6:22 AM, Robert Dober wrote:
>>
>> Hmm... I don't understand Enumerator#select comes from
>> Enumerable#select and already takes a block.
> Stupid me *again* I meant Array#slice. I feel that my brain is
> terribly disconnected from the keyboard, (I believe that Scotty was
> right when talking into the mouse LOL). Well proofreading my posts
> would be an option though :).
>
> I will express myself in Ruby
>
> assert_nothing_raised ("You see it would be nice that Array#slice and
> Hash#slice had (almost) the same interface") do
> [*1..100].slice(1..10){ |x| x.zero? }
> end


now that does make sense ;-)

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Michael Granger

4/14/2008 12:23:00 AM

0

On Apr 12, 2008, at 11:14 PM, Daniel Berger wrote:

> http://raa.ruby-lang.org/project/...


Ack, apologies to those who followed the link off that page to a 404.
Fixed now.

--
Michael Granger <ged@FaerieMUD.org>
Rubymage, Architect, Believer
The FaerieMUD Consortium <http://www.FaerieMU...