Array#squeeze

Discussion in 'Ruby' started by Martin DeMello, May 6, 2005.

  1. 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
     
    Martin DeMello, May 6, 2005
    #1
    1. Advertising

  2. On 5/6/05, 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
    >
    > 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
     
    Logan Capaldo, May 6, 2005
    #2
    1. Advertising

  3. 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
     
    David A. Black, May 6, 2005
    #3
  4. David A. Black <> 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
     
    Martin DeMello, May 6, 2005
    #4
  5. Martin DeMello

    Mark Hubbart Guest

    On 5/6/05, Logan Capaldo <> wrote:
    > On 5/6/05, 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
    > >
    > > 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
     
    Mark Hubbart, May 6, 2005
    #5
  6. On 5/6/05, Mark Hubbart <> wrote:
    > On 5/6/05, Logan Capaldo <> wrote:
    > > On 5/6/05, 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
    > > >
    > > > 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. ;)
     
    Logan Capaldo, May 6, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Big Bill
    Replies:
    7
    Views:
    429
    Big Bill
    May 7, 2004
  2. Mathias Waack

    squeeze and package imports

    Mathias Waack, Sep 25, 2003, in forum: Python
    Replies:
    2
    Views:
    276
    Mathias Waack
    Sep 27, 2003
  3. arnuld

    K&R2 section 2.8 (exercise 2.4) "squeeze"

    arnuld, Mar 13, 2007, in forum: C Programming
    Replies:
    5
    Views:
    462
    Barry Schwarz
    Mar 14, 2007
  4. luser-ex-troll

    META: relax topicality to squeeze the spam?

    luser-ex-troll, Mar 14, 2009, in forum: C Programming
    Replies:
    6
    Views:
    878
    luser-ex-troll
    Mar 17, 2009
  5. sara

    squeeze function

    sara, Oct 19, 2009, in forum: C++
    Replies:
    5
    Views:
    1,687
    Rolf Magnus
    Oct 19, 2009
Loading...

Share This Page