Class: Sequence

Inherits:
Object
  • Object
show all
Includes:
Comparable, LambdaBlock
Defined in:
lib/totally_lazy/sequence.rb

Direct Known Subclasses

Group

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enumerator) ⇒ Sequence

Returns a new instance of Sequence.



115
116
117
118
# File 'lib/totally_lazy/sequence.rb', line 115

def initialize(enumerator)
  raise "Sequence only accepts Enumerator::Lazy, not #{enumerator.class}" unless (enumerator.class == Enumerator::Lazy)
  @enumerator = enumerator
end

Instance Attribute Details

#enumeratorObject (readonly)

Returns the value of attribute enumerator.



73
74
75
# File 'lib/totally_lazy/sequence.rb', line 73

def enumerator
  @enumerator
end

Class Method Details

.drop(sequence, count) ⇒ Object



83
84
85
# File 'lib/totally_lazy/sequence.rb', line 83

def self.drop(sequence, count)
  Sequence.new(sequence.enumerator.drop(count))
end

.emptyObject



111
112
113
# File 'lib/totally_lazy/sequence.rb', line 111

def self.empty
  EMPTY
end

.map_concurrently(sequence, fn = nil, &block) ⇒ Object



75
76
77
# File 'lib/totally_lazy/sequence.rb', line 75

def self.map_concurrently(sequence, fn=nil, &block)
  call_concurrently(sequence.map(defer_return(block_given? ? ->(value) { block.call(value) } : fn)))
end

.repeat(item) ⇒ Object



87
88
89
# File 'lib/totally_lazy/sequence.rb', line 87

def self.repeat(item)
  Sequence.new(repeat_enumerator(item))
end

.repeat_fn(item) ⇒ Object



91
92
93
# File 'lib/totally_lazy/sequence.rb', line 91

def self.repeat_fn(item)
  Sequence.new(repeat_fn_enumerator(item))
end

.sequence(*items) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/totally_lazy/sequence.rb', line 103

def self.sequence(*items)
  if items.first.nil?
    empty
  else
    Sequence.new(items.lazy)
  end
end

.sort(sequence, comparator = ascending) ⇒ Object



79
80
81
# File 'lib/totally_lazy/sequence.rb', line 79

def self.sort(sequence, comparator=ascending)
  Sequence.new(sequence.enumerator.sort { |a, b| comparator.(a, b) }.lazy)
end

.take(sequence, count) ⇒ Object



95
96
97
# File 'lib/totally_lazy/sequence.rb', line 95

def self.take(sequence, count)
  Sequence.new(sequence.enumerator.take(count))
end

.zip(left, right) ⇒ Object



99
100
101
# File 'lib/totally_lazy/sequence.rb', line 99

def self.zip(left, right)
  left.zip(right)
end

Instance Method Details

#<=>(other) ⇒ Object



333
334
335
# File 'lib/totally_lazy/sequence.rb', line 333

def <=>(other)
  @enumerator.entries <=> other.enumerator.entries
end

#contains?(value) ⇒ Boolean

Returns:

  • (Boolean)


274
275
276
# File 'lib/totally_lazy/sequence.rb', line 274

def contains?(value)
  @enumerator.member?(value)
end

#cycleObject



325
326
327
# File 'lib/totally_lazy/sequence.rb', line 325

def cycle
  Sequence.new(cycle_enumerator(@enumerator))
end

#drop(count) ⇒ Object



252
253
254
# File 'lib/totally_lazy/sequence.rb', line 252

def drop(count)
  Sequence.drop(self, count)
end

#drop_while(fn_pred = nil, &block_pred) ⇒ Object



256
257
258
259
# File 'lib/totally_lazy/sequence.rb', line 256

def drop_while(fn_pred=nil, &block_pred)
  assert_funcs(fn_pred, block_given?)
  Sequence.new(@enumerator.drop_while { |value| block_given? ? block_pred.call(value) : fn_pred.(value) })
end

#each(fn = nil, &block) ⇒ Object



304
305
306
307
# File 'lib/totally_lazy/sequence.rb', line 304

def each(fn=nil, &block)
  assert_funcs(fn, block_given?)
  @enumerator.each { |value| block_given? ? block.call(value) : fn.(value) }
end

#exists?(fn_pred = nil, &block_pred) ⇒ Boolean

Returns:

  • (Boolean)


278
279
280
281
# File 'lib/totally_lazy/sequence.rb', line 278

def exists?(fn_pred=nil, &block_pred)
  assert_funcs(fn_pred, block_given?)
  @enumerator.any? { |value| block_given? ? block_pred.call(value) : fn_pred.(value) }
end

#filter(fn_pred = nil, &block_pred) ⇒ Object



288
289
290
291
# File 'lib/totally_lazy/sequence.rb', line 288

def filter(fn_pred=nil, &block_pred)
  assert_funcs(fn_pred, block_given?)
  Sequence.new(@enumerator.select { |value| block_given? ? block_pred.call(value) : fn_pred.(value) })
end

#find(fn_pred = nil, &block_pred) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/totally_lazy/sequence.rb', line 217

def find(fn_pred=nil, &block_pred)
  assert_funcs(fn_pred, block_given?)
  @enumerator.rewind
  while has_next(@enumerator)
    item = @enumerator.next
    result = block_given? ? block_pred.call(item) : fn_pred.(item)
    if result
      return(some(item))
    end
  end
  none
end

#find_index_of(fn_pred = nil, &block_pred) ⇒ Object



238
239
240
241
# File 'lib/totally_lazy/sequence.rb', line 238

def find_index_of(fn_pred=nil, &block_pred)
  assert_funcs(fn_pred, block_given?)
  zip_with_index.find(->(pair) { block_given? ? block_pred.call(pair.second) : fn_pred.(pair.second) }).map(->(pair) { pair.first })
end

#flat_map(fn = nil, &block) ⇒ Object



261
262
263
264
# File 'lib/totally_lazy/sequence.rb', line 261

def flat_map(fn=nil, &block)
  assert_funcs(fn, block_given?)
  map(block_given? ? ->(value) { block.call(value) } : fn).flatten
end

#flattenObject



266
267
268
# File 'lib/totally_lazy/sequence.rb', line 266

def flatten
  Sequence.new(flatten_enumerator(enumerator))
end

#fold(seed, fn = nil, &block) ⇒ Object Also known as: fold_left



178
179
180
181
182
183
# File 'lib/totally_lazy/sequence.rb', line 178

def fold(seed, fn=nil, &block)
  assert_funcs(fn, block_given?)
  @enumerator.inject(seed) { |accumulator, value|
    block_given? ? block.call(accumulator, value) : fn.(accumulator, value)
  }
end

#fold_right(seed, fn = nil, &block) ⇒ Object



187
188
189
190
191
192
# File 'lib/totally_lazy/sequence.rb', line 187

def fold_right(seed, fn=nil, &block)
  assert_funcs(fn, block_given?)
  reverse_enumerator(@enumerator).inject(seed) { |accumulator, value|
    block_given? ? block.call(value, accumulator) : fn.(value, accumulator)
  }
end

#for_all?(fn_pred = nil, &block_pred) ⇒ Boolean

Returns:

  • (Boolean)


283
284
285
286
# File 'lib/totally_lazy/sequence.rb', line 283

def for_all?(fn_pred=nil, &block_pred)
  assert_funcs(fn_pred, block_given?)
  @enumerator.all? { |value| block_given? ? block_pred.call(value) : fn_pred.(value) }
end

#group_by(fn = nil, &block) ⇒ Object



298
299
300
301
302
# File 'lib/totally_lazy/sequence.rb', line 298

def group_by(fn=nil, &block)
  assert_funcs(fn, block_given?)
  groups = @enumerator.group_by { |value| block_given? ? block.call(value) : fn.(value) }
  Sequence.new(groups.to_a.map { |group| Group.new(group[0], group[1].lazy) }.lazy)
end

#headObject Also known as: first



134
135
136
# File 'lib/totally_lazy/sequence.rb', line 134

def head
  @enumerator.first
end

#head_optionObject



144
145
146
# File 'lib/totally_lazy/sequence.rb', line 144

def head_option
  option(head)
end

#initObject



167
168
169
# File 'lib/totally_lazy/sequence.rb', line 167

def init
  reverse.tail.reverse
end

#inspectObject



337
338
339
# File 'lib/totally_lazy/sequence.rb', line 337

def inspect
  to_s
end

#is_empty?Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
# File 'lib/totally_lazy/sequence.rb', line 120

def is_empty?
  @enumerator.rewind
  begin
    @enumerator.peek
    false
  rescue
    true
  end
end

#join(other) ⇒ Object



318
319
320
321
322
323
# File 'lib/totally_lazy/sequence.rb', line 318

def join(other)
  Sequence.new(Enumerator.new do |y|
    @enumerator.each { |value| y << value }
    other.enumerator.each { |value| y << value }
  end.lazy)
end

#lastObject



148
149
150
# File 'lib/totally_lazy/sequence.rb', line 148

def last
  reverse.head
end

#last_optionObject



152
153
154
# File 'lib/totally_lazy/sequence.rb', line 152

def last_option
  reverse.head_option
end

#map(fn = nil, &block) ⇒ Object



171
172
173
174
175
176
# File 'lib/totally_lazy/sequence.rb', line 171

def map(fn=nil, &block)
  assert_funcs(fn, block_given?)
  Sequence.new(@enumerator.map { |value|
    block_given? ? block.call(value) : fn.(value)
  })
end

#map_concurrently(fn = nil, &block) ⇒ Object



309
310
311
312
# File 'lib/totally_lazy/sequence.rb', line 309

def map_concurrently(fn=nil, &block)
  assert_funcs(fn, block_given?)
  Sequence.map_concurrently(self, block_given? ? ->(value) { block.call(value) } : fn)
end

#realiseObject



314
315
316
# File 'lib/totally_lazy/sequence.rb', line 314

def realise
  Sequence.new(@enumerator.to_a.lazy)
end

#reduce(fn = nil, &block) ⇒ Object Also known as: reduce_left



194
195
196
197
198
199
200
201
202
# File 'lib/totally_lazy/sequence.rb', line 194

def reduce(fn=nil, &block)
  assert_funcs(fn, block_given?)
  _fn = block_given? ? ->(a, b) { block.call(a, b) } : fn
  accumulator = seed(@enumerator, fn)
  while has_next(@enumerator)
    accumulator = _fn.(accumulator, @enumerator.next)
  end
  accumulator
end

#reduce_right(fn = nil, &block) ⇒ Object



206
207
208
209
210
211
212
213
214
215
# File 'lib/totally_lazy/sequence.rb', line 206

def reduce_right(fn=nil, &block)
  assert_funcs(fn, block_given?)
  _fn = block_given? ? ->(a, b) { block.call(a, b) } : fn
  reversed = reverse_enumerator(@enumerator)
  accumulator = seed(reversed, fn)
  while has_next(reversed)
    accumulator = _fn.(reversed.next, accumulator)
  end
  accumulator
end

#reject(fn_pred = nil, &block_pred) ⇒ Object



293
294
295
296
# File 'lib/totally_lazy/sequence.rb', line 293

def reject(fn_pred=nil, &block_pred)
  assert_funcs(fn_pred, block_given?)
  filter(is_not(block_given? ? ->(value) { block_pred.call(value) } : fn_pred))
end

#reverseObject



156
157
158
# File 'lib/totally_lazy/sequence.rb', line 156

def reverse
  Sequence.new(reverse_enumerator(@enumerator))
end

#secondObject



140
141
142
# File 'lib/totally_lazy/sequence.rb', line 140

def second
  tail.head
end

#sizeObject



130
131
132
# File 'lib/totally_lazy/sequence.rb', line 130

def size
  @enumerator.count
end

#sort_by(comparator) ⇒ Object



270
271
272
# File 'lib/totally_lazy/sequence.rb', line 270

def sort_by(comparator)
  Sequence.sort(self, comparator)
end

#tailObject



160
161
162
163
164
165
# File 'lib/totally_lazy/sequence.rb', line 160

def tail
  unless has_next(@enumerator)
    raise NoSuchElementException.new
  end
  Sequence.new(@enumerator.drop(1))
end

#take(count) ⇒ Object



243
244
245
# File 'lib/totally_lazy/sequence.rb', line 243

def take(count)
  Sequence.take(self, count)
end

#take_while(fn_pred = nil, &block_pred) ⇒ Object



247
248
249
250
# File 'lib/totally_lazy/sequence.rb', line 247

def take_while(fn_pred=nil, &block_pred)
  assert_funcs(fn_pred, block_given?)
  Sequence.new(@enumerator.take_while { |value| block_given? ? block_pred.call(value) : fn_pred.(value) })
end

#to_aObject



329
330
331
# File 'lib/totally_lazy/sequence.rb', line 329

def to_a
  @enumerator.to_a
end

#to_sObject



341
342
343
344
# File 'lib/totally_lazy/sequence.rb', line 341

def to_s
  sample = take(100).to_a.to_seq
  "[#{sample.is_empty? ? '' : sample.reduce(join_with_sep(','))}]"
end

#zip(other) ⇒ Object



230
231
232
# File 'lib/totally_lazy/sequence.rb', line 230

def zip(other)
  Sequence.new(pair_enumerator(@enumerator, other.enumerator))
end

#zip_with_indexObject



234
235
236
# File 'lib/totally_lazy/sequence.rb', line 234

def zip_with_index
  Sequence.zip(range_from(0), self)
end