Sha256: 73fb449aad8eac7abb949b8bb4e3aa6a079365f8183018e1ca320a7fe601db2e

Contents?: true

Size: 844 Bytes

Versions: 28

Compression:

Stored size: 844 Bytes

Contents

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

  # 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

28 entries across 28 versions & 1 rubygems

Version Path
picky-4.5.4 lib/picky/extensions/array.rb
picky-4.5.3 lib/picky/extensions/array.rb
picky-4.5.2 lib/picky/extensions/array.rb
picky-4.5.1 lib/picky/extensions/array.rb
picky-4.5.0 lib/picky/extensions/array.rb
picky-4.4.2 lib/picky/extensions/array.rb
picky-0.12.3 lib/picky/extensions/array.rb
picky-0.12.2 lib/picky/extensions/array.rb