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

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.options
    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

#cloneObject

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

Returns:

  • (Boolean)


20
21
22
# File 'lib/mtk/groups/collection.rb', line 20

def empty?
  elements.nil? or elements.size == 0
end

#enumerable_mapObject

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

#permuteObject 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

#reverseObject 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

#sizeObject 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_aObject

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