Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Array#squeeze

Reply
Thread Tools

Array#squeeze

 
 
Martin DeMello
Guest
Posts: n/a
 
      05-06-2005
The recent mention of String#squeeze made me wonder if it would not be a
good idea to add a #squeeze method to Array (or possibly even to
Enumerable) as well. Certainly I've needed it at least as often as I've
needed #uniq

martin
 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      05-06-2005
On 5/6/05, Martin DeMello <(E-Mail Removed)> wrote:
> The recent mention of String#squeeze made me wonder if it would not be a
> good idea to add a #squeeze method to Array (or possibly even to
> Enumerable) as well. Certainly I've needed it at least as often as I've
> needed #uniq
>
> martin
>
>


Well here's a possible implementation:

module Enumerable
def squeeze(*args)
raise ArgumentError.new("Provide 0 or 1 arguments") if
args.length > 1
inject([]) do |a, b|
if args.length == 0
if b == a.last
a
else
a << b
end
else
if b == a.last && a.last == args[0]
a
else
a << b
end
end
end
end
end



 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      05-06-2005
Hi --

On Fri, 6 May 2005, Martin DeMello wrote:

> The recent mention of String#squeeze made me wonder if it would not be a
> good idea to add a #squeeze method to Array (or possibly even to
> Enumerable) as well. Certainly I've needed it at least as often as I've
> needed #uniq


How close is this to what you might want, at least for Array?

irb(main):001:0> a = [1,2,3,4,1,2]
=> [1, 2, 3, 4, 1, 2]
irb(main):002:0> a |= [1,2]
=> [1, 2, 3, 4]


David

--
David A. Black
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      05-06-2005
David A. Black <(E-Mail Removed)> wrote:
> On Fri, 6 May 2005, Martin DeMello wrote:
>
> > The recent mention of String#squeeze made me wonder if it would not be a
> > good idea to add a #squeeze method to Array (or possibly even to
> > Enumerable) as well. Certainly I've needed it at least as often as I've
> > needed #uniq

>
> How close is this to what you might want, at least for Array?
>
> irb(main):001:0> a = [1,2,3,4,1,2]
> => [1, 2, 3, 4, 1, 2]
> irb(main):002:0> a |= [1,2]
> => [1, 2, 3, 4]


Nope, that's not what I meant - I wanted to compress a run of elements
into a single element, pretty much like unix's uniq does. (Logan posted
an implementation already).

martin
 
Reply With Quote
 
Mark Hubbart
Guest
Posts: n/a
 
      05-06-2005
On 5/6/05, Logan Capaldo <(E-Mail Removed)> wrote:
> On 5/6/05, Martin DeMello <(E-Mail Removed)> wrote:
> > The recent mention of String#squeeze made me wonder if it would not be a
> > good idea to add a #squeeze method to Array (or possibly even to
> > Enumerable) as well. Certainly I've needed it at least as often as I've
> > needed #uniq
> >
> > martin
> >
> >

>
> Well here's a possible implementation:
>
> module Enumerable
> def squeeze(*args)
> raise ArgumentError.new("Provide 0 or 1 arguments") if
> args.length > 1
> inject([]) do |a, b|
> if args.length == 0
> if b == a.last
> a
> else
> a << b
> end
> else
> if b == a.last && a.last == args[0]
> a
> else
> a << b
> end
> end
> end
> end
> end


String's squeeze allows arbitrary numbers of character arguments. II
would think Enumerable#squeeze should do the same:

module Enumerable
def squeeze(*args)
ary = []
check = proc{|item| args.empty?? true : args.include?(item) }
each{|elem| ary << elem unless ary.last == elem and check[elem] }
ary
end
end

cheers,
Mark



 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      05-06-2005
On 5/6/05, Mark Hubbart <(E-Mail Removed)> wrote:
> On 5/6/05, Logan Capaldo <(E-Mail Removed)> wrote:
> > On 5/6/05, Martin DeMello <(E-Mail Removed)> wrote:
> > > The recent mention of String#squeeze made me wonder if it would not be a
> > > good idea to add a #squeeze method to Array (or possibly even to
> > > Enumerable) as well. Certainly I've needed it at least as often as I've
> > > needed #uniq
> > >
> > > martin
> > >
> > >

> >
> > Well here's a possible implementation:
> >
> > module Enumerable
> > def squeeze(*args)
> > raise ArgumentError.new("Provide 0 or 1 arguments") if
> > args.length > 1
> > inject([]) do |a, b|
> > if args.length == 0
> > if b == a.last
> > a
> > else
> > a << b
> > end
> > else
> > if b == a.last && a.last == args[0]
> > a
> > else
> > a << b
> > end
> > end
> > end
> > end
> > end

>
> String's squeeze allows arbitrary numbers of character arguments. II
> would think Enumerable#squeeze should do the same:
>
> module Enumerable
> def squeeze(*args)
> ary = []
> check = proc{|item| args.empty?? true : args.include?(item) }
> each{|elem| ary << elem unless ary.last == elem and check[elem] }
> ary
> end
> end
>
> cheers,
> Mark
>
>


String's squeeze does, but their are problems with duplicating the
exact semantics of String#squeeze because according to the
documentation it uses the intersection of the strings passed as an
argument and has support for ranges of strings (ie "m-z"). Ranges
would be pretty easy just use x..y.include? but if we are following
the same semantics as String#squeeze your implementation is incorrect.
I will admit yours is more succint then my silly nested ifs.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments