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
# 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
The element with the given index
# File lib/mtk/groups/collection.rb, line 48 def [](index) elements[index] end
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
# 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
The each iterator for providing Enumerable functionality
# File lib/mtk/groups/collection.rb, line 25 def each &block elements.each(&block) end
# File lib/mtk/groups/collection.rb, line 20 def empty? elements.nil? or elements.size == 0 end
the original Enumerable#map implementation, which returns an Array
The first element
# File lib/mtk/groups/collection.rb, line 38 def first(n=nil) n ? elements.first(n) : elements.first end
The last element
# File lib/mtk/groups/collection.rb, line 43 def last(n=nil) n ? elements.last(n) : elements.last end
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
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
# File lib/mtk/groups/collection.rb, line 59 def permute clone_with elements.shuffle end
# 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
# File lib/mtk/groups/collection.rb, line 73 def reverse clone_with elements.reverse end
# File lib/mtk/groups/collection.rb, line 64 def rotate(offset=1) clone_with elements.rotate(offset) end
The number of elements in this collection
# File lib/mtk/groups/collection.rb, line 15 def size elements.size end
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
“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