module Hamster::Enumerable

Helper module for Hamster's sequential collections

Classes including `Hamster::Enumerable` must implement:

Public Instance Methods

<=>(other) click to toggle source

Compare with `other`, and return 0, 1, or -1 if it is (respectively) equal to, greater than, or less than this collection.

# File lib/hamster/enumerable.rb, line 92
def <=>(other)
  return 0 if self.equal?(other)
  enum1, enum2 = self.to_enum, other.to_enum
  loop do
    item1 = enum1.next
    item2 = enum2.next
    comp  = (item1 <=> item2)
    return comp if comp != 0
  end
  size1, size2 = self.size, other.size
  return 0 if size1 == size2
  size1 > size2 ? 1 : -1
end
==(other) click to toggle source

Return true if `other` contains the same elements, in the same order. @return [Boolean]

# File lib/hamster/enumerable.rb, line 108
def ==(other)
  self.eql?(other) || other.respond_to?(:to_ary) && to_ary.eql?(other.to_ary)
end
compact() click to toggle source

Return a new collection with all `nil` elements removed.

# File lib/hamster/enumerable.rb, line 20
def compact
  select { |item| !item.nil? }
end
delete_if()
Alias for: reject
each_index(&block) click to toggle source

Yield all integers from 0 up to, but not including, the number of items in this collection. For collections which provide indexed access, these are all the valid, non-negative indices into the collection.

# File lib/hamster/enumerable.rb, line 44
def each_index(&block)
  return enum_for(:each_index) unless block_given?
  0.upto(size-1, &block)
  self
end
grep(pattern, &block) click to toggle source

Search the collection for elements which are `#===` to `item`. Yield them to the optional code block if provided, and return them as a new collection.

# File lib/hamster/enumerable.rb, line 26
def grep(pattern, &block)
  result = select { |item| pattern === item }
  result = result.map(&block) if block_given?
  result
end
grep_v(pattern, &block) click to toggle source

Search the collection for elements which are not `#===` to `item`. Yield them to the optional code block if provided, and return them as a new collection.

# File lib/hamster/enumerable.rb, line 35
def grep_v(pattern, &block)
  result = select { |item| !(pattern === item) }
  result = result.map(&block) if block_given?
  result
end
group_by(&block) click to toggle source

Groups the collection into sub-collections by the result of yielding them to the block. Returns a {Hash} where the keys are return values from the block, and the values are sub-collections (of the same type as this one).

# File lib/hamster/enumerable.rb, line 86
def group_by(&block)
  group_by_with(self.class.empty, &block)
end
inspect() click to toggle source

Convert this collection to a programmer-readable `String` representation.

# File lib/hamster/enumerable.rb, line 131
def inspect
  result = "#{self.class}["
  each_with_index { |obj, i| result << ', ' if i > 0; result << obj.inspect }
  result << "]"
end
join(separator = $,) click to toggle source

Convert all the elements into strings and join them together, separated by `separator`. By default, the `separator` is `$,`, the global default string separator, which is normally `nil`.

# File lib/hamster/enumerable.rb, line 115
def join(separator = $,)
  result = ""
  if separator
    each_with_index { |obj, i| result << separator if i > 0; result << obj.to_s }
  else
    each { |obj| result << obj.to_s }
  end
  result
end
partition() click to toggle source

Return 2 collections, the first containing all the elements for which the block evaluates to true, the second containing the rest.

Calls superclass method
# File lib/hamster/enumerable.rb, line 62
def partition
  return enum_for(:partition) if not block_given?
  a,b = super
  [self.class.new(a), self.class.new(b)].freeze
end
pretty_print(pp) click to toggle source

@private

# File lib/hamster/enumerable.rb, line 138
def pretty_print(pp)
  pp.group(1, "#{self.class}[", "]") do
    pp.breakable ''
    pp.seplist(self) { |obj| obj.pretty_print(pp) }
  end
end
product() click to toggle source

Multiply all the items (presumably numeric) in this collection together.

# File lib/hamster/enumerable.rb, line 51
def product
  reduce(1, &:*)
end
reject() { |item| ... } click to toggle source

Return a new collection with all the elements for which the block returns false.

# File lib/hamster/enumerable.rb, line 13
def reject
  return enum_for(:reject) if not block_given?
  select { |item| !yield(item) }
end
Also aliased as: delete_if
sort_by(&block) click to toggle source

Rubinius implements #sort_by using Enumerable#map Because we do our own, custom implementations of map, that doesn't work well @private

# File lib/hamster/enumerable.rb, line 154
def sort_by(&block)
  result = to_a
  result.frozen? ? result.sort_by(&block) : result.sort_by!(&block)
end
sum() click to toggle source

Add up all the items (presumably numeric) in this collection.

# File lib/hamster/enumerable.rb, line 56
def sum
  reduce(0, &:+)
end
to_set() click to toggle source

Convert this collection to a {Set}.

# File lib/hamster/enumerable.rb, line 126
def to_set
  Set.new(self)
end

Protected Instance Methods

group_by_with(empty_group, &block) click to toggle source

Groups the collection into sub-collections by the result of yielding them to the block. Returns a {Hash} where the keys are return values from the block, and the values are sub-collections. All the sub-collections are built up from `empty_group`, which should respond to `#add` by returning a new collection with an added element.

# File lib/hamster/enumerable.rb, line 73
def group_by_with(empty_group, &block)
  block ||= lambda { |item| item }
  reduce(EmptyHash) do |hash, item|
    key = block.call(item)
    group = hash.get(key) || empty_group
    hash.put(key, group.add(item))
  end
end