module MTK::Groups::Collection

Given a method elements, which returns an Array of elements in the collection, including this module will make the class Enumerable and provide various methods you’d expect from an Array.

Public Instance Methods

==(other) click to toggle source
# File lib/mtk/groups/collection.rb, line 130
def ==(other)
  if other.respond_to? :elements
    if other.respond_to? :options
      elements == other.elements and @options == other.options
    else
      elements == other.elements
    end
  else
    elements == other
  end
end
[](index) click to toggle source

The element with the given index

# File lib/mtk/groups/collection.rb, line 48
def [](index)
  elements[index]
end
clone() click to toggle source

Create a copy of the collection. In order to use this method, the including class must implement .from_a()

# File lib/mtk/groups/collection.rb, line 144
def clone
  clone_with to_a
end
concat(other) click to toggle source
# File lib/mtk/groups/collection.rb, line 68
def concat(other)
  other_elements = (other.respond_to? :elements) ? other.elements : other
  clone_with(elements + other_elements)
end
each(&block) click to toggle source

The each iterator for providing Enumerable functionality

# File lib/mtk/groups/collection.rb, line 25
def each &block
  elements.each(&block)
end
empty?() click to toggle source
# File lib/mtk/groups/collection.rb, line 20
def empty?
  elements.nil? or elements.size == 0
end
enumerable_map(&block)

the original Enumerable#map implementation, which returns an Array

Alias for: map
first(n=nil) click to toggle source

The first element

# File lib/mtk/groups/collection.rb, line 38
def first(n=nil)
  n ? elements.first(n) : elements.first
end
last(n=nil) click to toggle source

The last element

# File lib/mtk/groups/collection.rb, line 43
def last(n=nil)
  n ? elements.last(n) : elements.last
end
length()
Alias for: size
map(&block) click to toggle source

the overriden map implementation, which returns an object of the same type

# File lib/mtk/groups/collection.rb, line 33
def map &block
  clone_with enumerable_map(&block)
end
Also aliased as: enumerable_map
partition(arg=nil, &block) click to toggle source

Partition the collection into an Array of sub-collections.

With a Numeric argument: partition the elements into collections of the given size (plus whatever’s left over).

With an Array argument: partition the elements into collections of the given sizes.

Otherwise if a block is given: partition the elements into collections with the same block return value.

# File lib/mtk/groups/collection.rb, line 87
def partition(arg=nil, &block)
  partitions = nil
  case arg
    when Numeric
      partitions = self.each_slice(arg)

    when Enumerable
      partitions = []
      items, sizes = self.to_enum, arg.to_enum
      group = []
      size = sizes.next
      loop do
        item = items.next
        if group.size < size
          group << item
        else
          partitions << group
          group = []
          size = sizes.next
          group << item
        end
      end
      partitions << group unless group.empty?

    else
      if block
        group = Hash.new{|h,k| h[k] = [] }
        if block.arity == 2
          self.each_with_index{|item,index| group[block[item,index]] << item }
        else
          self.each{|item| group[block[item]] << item }
        end
        partitions = group.values
      end
  end

  if partitions
    partitions.map{|p| self.class.from_a(p) }
  else
    self
  end
end
permute() click to toggle source
# File lib/mtk/groups/collection.rb, line 59
def permute
  clone_with elements.shuffle
end
Also aliased as: shuffle
repeat(times=2) click to toggle source
# File lib/mtk/groups/collection.rb, line 52
def repeat(times=2)
  full_repetitions, fractional_repetitions = times.floor, times%1  # split into int and fractional part
  repeated = elements * full_repetitions
  repeated += elements[0...elements.size*fractional_repetitions]
  clone_with repeated
end
retrograde()
Alias for: reverse
reverse() click to toggle source
# File lib/mtk/groups/collection.rb, line 73
def reverse
  clone_with elements.reverse
end
Also aliased as: retrograde
rotate(offset=1) click to toggle source
# File lib/mtk/groups/collection.rb, line 64
def rotate(offset=1)
  clone_with elements.rotate(offset)
end
shuffle()
Alias for: permute
size() click to toggle source

The number of elements in this collection

# File lib/mtk/groups/collection.rb, line 15
def size
  elements.size
end
Also aliased as: length
to_a() click to toggle source

A mutable array of elements in this collection

# File lib/mtk/groups/collection.rb, line 10
def to_a
  Array.new(elements) # we construct a new array since some including classes make elements be immutable
end

Private Instance Methods

clone_with(elements) click to toggle source

“clones” the object with the given elements, attempting to maintain any @options This is designed to work with 2 argument constructors: def initialize(elements, options=default)

# File lib/mtk/groups/collection.rb, line 153
def clone_with elements
  from_a = self.class.method(:from_a)
  if @options and from_a.arity == -2
    from_a[elements, @options]
  else
    from_a[elements]
  end
end