Class: Generics::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/generics/collections/list.rb

Direct Known Subclasses

Mutable

Defined Under Namespace

Classes: Mutable, NotSameType

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*collection) ⇒ List



112
113
114
115
# File 'lib/generics/collections/list.rb', line 112

def initialize(*collection)
  collection.each { |value| compatible!(value) }
  @collection = ::Hamster::Vector.new(collection)
end

Class Method Details

.[](type) ⇒ Class



14
15
16
17
18
19
# File 'lib/generics/collections/list.rb', line 14

def self.[](type)
  Class.new(self).tap do |klass|
    klass.instance_variable_set('@type', type)
    klass.define_singleton_method(:name) { klass.superclass.name }
  end
end

.typeObject



21
22
23
# File 'lib/generics/collections/list.rb', line 21

def self.type
  @type
end

Instance Method Details

#[](index) ⇒ Object

Enumerable



46
47
48
# File 'lib/generics/collections/list.rb', line 46

def [](index)
  @collection[index]
end

#add(value) ⇒ Object

Destructive



137
138
139
140
# File 'lib/generics/collections/list.rb', line 137

def add(value)
  compatible!(value)
  self.class.new(*@collection.add(value))
end

#cloneList

Preserves frozen state and does copies singleton state



125
126
127
# File 'lib/generics/collections/list.rb', line 125

def clone
  self.class.new(*@collection.clone)
end

#compatible!(value) ⇒ Object



29
30
31
32
# File 'lib/generics/collections/list.rb', line 29

def compatible!(value)
  fail NotSameType unless value.is_a?(self.class.type)
  true
end

#compatible?(value) ⇒ Boolean



25
26
27
# File 'lib/generics/collections/list.rb', line 25

def compatible?(value)
  value.is_a?(self.class.type)
end

#delete(value) ⇒ Object



152
153
154
155
# File 'lib/generics/collections/list.rb', line 152

def delete(value)
  compatible!(value)
  self.class.new(*@collection.delete(value))
end

#delete_at(index) ⇒ Object

Methods that returns self



97
98
99
# File 'lib/generics/collections/list.rb', line 97

def delete_at(index)
  self.class.new(*@collection.delete_at(index))
end

#dupList

Does not preserve frozen state and does not copy singleton state



119
120
121
# File 'lib/generics/collections/list.rb', line 119

def dup
  self.class.new(*@collection.dup)
end

#eachObject



50
51
52
53
# File 'lib/generics/collections/list.rb', line 50

def each
  @collection.each { |item| yield item }
  return self
end

#empty?Boolean



71
72
73
# File 'lib/generics/collections/list.rb', line 71

def empty?
  @collection.empty?
end

#insert(index, *values) ⇒ Object



147
148
149
150
# File 'lib/generics/collections/list.rb', line 147

def insert(index, *values)
  values.each { |value| compatible!(value) }
  self.class.new(*@collection.insert(index, *values))
end

#inspectObject

Object



36
37
38
# File 'lib/generics/collections/list.rb', line 36

def inspect
  "#<#{self.class.name}[#{self.class.type}]: #{to_s}>"
end

#lastObject



75
76
77
# File 'lib/generics/collections/list.rb', line 75

def last
  @collection.last
end

#lengthObject



67
68
69
# File 'lib/generics/collections/list.rb', line 67

def length
  @collection.length
end

#mapObject



55
56
57
# File 'lib/generics/collections/list.rb', line 55

def map
  self.class.new(super)
end

#popObject



79
80
81
# File 'lib/generics/collections/list.rb', line 79

def pop
  @collection.pop
end

#qdupList

Quick dup; does not duplicate, just relies on immutability



131
132
133
# File 'lib/generics/collections/list.rb', line 131

def qdup
  self.class.new(*@collection)
end

#reverseObject



83
84
85
# File 'lib/generics/collections/list.rb', line 83

def reverse
  @collection.reverse
end

#sampleObject



87
88
89
# File 'lib/generics/collections/list.rb', line 87

def sample
  @collection.sample
end

#set(index, value, &block) ⇒ Object



142
143
144
145
# File 'lib/generics/collections/list.rb', line 142

def set(index, value, &block)
  compatible!(value)
  self.class.new(*@collection.set(index, value, &block))
end

#shiftObject



101
102
103
# File 'lib/generics/collections/list.rb', line 101

def shift
  self.class.new(*@collection.shift)
end

#sizeObject

Getters



63
64
65
# File 'lib/generics/collections/list.rb', line 63

def size
  @collection.size
end

#slice(*args) ⇒ Object



91
92
93
# File 'lib/generics/collections/list.rb', line 91

def slice(*args)
  @collection.slice(*args)
end

#to_sObject



40
41
42
# File 'lib/generics/collections/list.rb', line 40

def to_s
  @collection.to_a.to_s
end

#uniq(&block) ⇒ Object



105
106
107
# File 'lib/generics/collections/list.rb', line 105

def uniq(&block)
  self.class.new(*@collection.uniq(&block))
end

#unshift(value) ⇒ Object



161
162
163
164
# File 'lib/generics/collections/list.rb', line 161

def unshift(value)
  compatible!(value)
  self.class.new(*@collection.unshift(value))
end