Module: MTK::Groups::Collection
- Includes:
- Enumerable
- Included in:
- PitchCollection, Patterns::Pattern
- Defined in:
- lib/mtk/groups/collection.rb
Overview
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.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](index) ⇒ Object
The element with the given index.
-
#clone ⇒ Object
Create a copy of the collection.
- #concat(other) ⇒ Object
-
#each(&block) ⇒ Object
The each iterator for providing Enumerable functionality.
- #empty? ⇒ Boolean
-
#enumerable_map ⇒ Object
the original Enumerable#map implementation, which returns an Array.
-
#first(n = nil) ⇒ Object
The first element.
-
#last(n = nil) ⇒ Object
The last element.
-
#map(&block) ⇒ Object
the overriden #map implementation, which returns an object of the same type.
-
#partition(arg = nil, &block) ⇒ Object
Partition the collection into an Array of sub-collections.
- #permute ⇒ Object (also: #shuffle)
- #repeat(times = 2) ⇒ Object
- #reverse ⇒ Object (also: #retrograde)
- #rotate(offset = 1) ⇒ Object
-
#size ⇒ Object
(also: #length)
The number of elements in this collection.
-
#to_a ⇒ Object
A mutable array of elements in this collection.
Instance Method Details
#==(other) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# 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. else elements == other.elements end else elements == other end end |
#[](index) ⇒ Object
The element with the given index
48 49 50 |
# File 'lib/mtk/groups/collection.rb', line 48 def [](index) elements[index] end |
#clone ⇒ Object
Create a copy of the collection. In order to use this method, the including class must implement .from_a()
144 145 146 |
# File 'lib/mtk/groups/collection.rb', line 144 def clone clone_with to_a end |
#concat(other) ⇒ Object
68 69 70 71 |
# 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) ⇒ Object
The each iterator for providing Enumerable functionality
25 26 27 |
# File 'lib/mtk/groups/collection.rb', line 25 def each &block elements.each(&block) end |
#empty? ⇒ Boolean
20 21 22 |
# File 'lib/mtk/groups/collection.rb', line 20 def empty? elements.nil? or elements.size == 0 end |
#enumerable_map ⇒ Object
the original Enumerable#map implementation, which returns an Array
30 |
# File 'lib/mtk/groups/collection.rb', line 30 alias enumerable_map map |
#first(n = nil) ⇒ Object
The first element
38 39 40 |
# File 'lib/mtk/groups/collection.rb', line 38 def first(n=nil) n ? elements.first(n) : elements.first end |
#last(n = nil) ⇒ Object
The last element
43 44 45 |
# File 'lib/mtk/groups/collection.rb', line 43 def last(n=nil) n ? elements.last(n) : elements.last end |
#map(&block) ⇒ Object
the overriden #map implementation, which returns an object of the same type
33 34 35 |
# File 'lib/mtk/groups/collection.rb', line 33 def map &block clone_with enumerable_map(&block) end |
#partition(arg = nil, &block) ⇒ Object
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.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# 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 ⇒ Object Also known as: shuffle
59 60 61 |
# File 'lib/mtk/groups/collection.rb', line 59 def permute clone_with elements.shuffle end |
#repeat(times = 2) ⇒ Object
52 53 54 55 56 57 |
# 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 |
#reverse ⇒ Object Also known as: retrograde
73 74 75 |
# File 'lib/mtk/groups/collection.rb', line 73 def reverse clone_with elements.reverse end |
#rotate(offset = 1) ⇒ Object
64 65 66 |
# File 'lib/mtk/groups/collection.rb', line 64 def rotate(offset=1) clone_with elements.rotate(offset) end |
#size ⇒ Object Also known as: length
The number of elements in this collection
15 16 17 |
# File 'lib/mtk/groups/collection.rb', line 15 def size elements.size end |
#to_a ⇒ Object
A mutable array of elements in this collection
10 11 12 |
# 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 |