Sha256: d2b3974908f1736c4c9d078cf3476f49f0c965251d0682754d4eec1661b9d894

Contents?: true

Size: 857 Bytes

Versions: 111

Compression:

Stored size: 857 Bytes

Contents

# The Array class we all know and love.
#
class Array # :nodoc:all

  # Cluster-uniqs equal neighborly elements.
  #
  # Returns a copy.
  #
  def clustered_uniq
    self.inject([]) do |result, element|
      result << element if element != result.last
      result
    end
  end
  # Around 10% faster than the above.
  #
  # Returns a copy.
  #
  def clustered_uniq_fast
    result = []
    self.inject(nil) do |last, element|
      if last == element
        last
      else
        result << element && element
      end
    end
    result
  end

  # Sort the array using distance from levenshtein.
  #
  # Will raise if encounters not to_s-able element.
  #
  def sort_by_levenshtein! from
    from = from.to_s
    sort! do |this, that|
      Text::Levenshtein.distance(this.to_s, from) <=> Text::Levenshtein.distance(that.to_s, from)
    end
  end

end

Version data entries

111 entries across 111 versions & 1 rubygems

Version Path
picky-1.4.3 lib/picky/internals/extensions/array.rb
picky-1.4.2 lib/picky/internals/extensions/array.rb
picky-1.4.1 lib/picky/extensions/array.rb
picky-1.4.0 lib/picky/extensions/array.rb
picky-1.3.4 lib/picky/extensions/array.rb
picky-1.3.3 lib/picky/extensions/array.rb
picky-1.3.2 lib/picky/extensions/array.rb
picky-1.3.1 lib/picky/extensions/array.rb
picky-1.3.0 lib/picky/extensions/array.rb
picky-1.2.4 lib/picky/extensions/array.rb
picky-1.0.0 lib/picky/extensions/array.rb