Philip Cunningham music, code and stuff.

Ruby Snippet: Eliminate consecutive duplicates 23 October 2012

Programming in Ruby is really pleasant.

# P08 (**) Eliminate consecutive duplicates of list elements.

module ProblemEight
  COLLECTION = [:a, :a, :a, :a, :b, :c, :c, :a, :a, :d, :e, :e]

  def self.compress col
    col.inject [] {|arr, el| arr << el unless el == arr.last; arr}
  end

  def self.run
    puts compress(COLLECTION).to_s
  end
end

ProblemEight.run