class Sequence

Attributes

enumerator[R]

Public Class Methods

drop(sequence, count) click to toggle source
# File lib/totally_lazy/sequence.rb, line 83
def self.drop(sequence, count)
  Sequence.new(sequence.enumerator.drop(count))
end
empty() click to toggle source
# File lib/totally_lazy/sequence.rb, line 111
def self.empty
  EMPTY
end
map_concurrently(sequence, fn=nil, &block) click to toggle source
# 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
new(enumerator) click to toggle source
# 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
repeat(item) click to toggle source
# File lib/totally_lazy/sequence.rb, line 87
def self.repeat(item)
  Sequence.new(repeat_enumerator(item))
end
repeat_fn(item) click to toggle source
# File lib/totally_lazy/sequence.rb, line 91
def self.repeat_fn(item)
  Sequence.new(repeat_fn_enumerator(item))
end
sequence(*items) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/totally_lazy/sequence.rb, line 95
def self.take(sequence, count)
  Sequence.new(sequence.enumerator.take(count))
end
zip(left, right) click to toggle source
# File lib/totally_lazy/sequence.rb, line 99
def self.zip(left, right)
  left.zip(right)
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/totally_lazy/sequence.rb, line 333
def <=>(other)
  @enumerator.entries <=> other.enumerator.entries
end
contains?(value) click to toggle source
# File lib/totally_lazy/sequence.rb, line 274
def contains?(value)
  @enumerator.member?(value)
end
cycle() click to toggle source
# File lib/totally_lazy/sequence.rb, line 325
def cycle
  Sequence.new(cycle_enumerator(@enumerator))
end
drop(count) click to toggle source
# File lib/totally_lazy/sequence.rb, line 252
def drop(count)
  Sequence.drop(self, count)
end
drop_while(fn_pred=nil, &block_pred) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
first()
Alias for: head
flat_map(fn=nil, &block) click to toggle source
# 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
flatten() click to toggle source
# File lib/totally_lazy/sequence.rb, line 266
def flatten
  Sequence.new(flatten_enumerator(enumerator))
end
fold(seed, fn=nil, &block) click to toggle source
# 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
Also aliased as: fold_left
fold_left(seed, fn=nil, &block)
Alias for: fold
fold_right(seed, fn=nil, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
head() click to toggle source
# File lib/totally_lazy/sequence.rb, line 134
def head
  @enumerator.first
end
Also aliased as: first
head_option() click to toggle source
# File lib/totally_lazy/sequence.rb, line 144
def head_option
  option(head)
end
init() click to toggle source
# File lib/totally_lazy/sequence.rb, line 167
def init
  reverse.tail.reverse
end
inspect() click to toggle source
# File lib/totally_lazy/sequence.rb, line 337
def inspect
  to_s
end
is_empty?() click to toggle source
# File lib/totally_lazy/sequence.rb, line 120
def is_empty?
  @enumerator.rewind
  begin
    @enumerator.peek
    false
  rescue
    true
  end
end
join(other) click to toggle source
# 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
last() click to toggle source
# File lib/totally_lazy/sequence.rb, line 148
def last
  reverse.head
end
last_option() click to toggle source
# File lib/totally_lazy/sequence.rb, line 152
def last_option
  reverse.head_option
end
map(fn=nil, &block) click to toggle source
# 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) click to toggle source
# 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
realise() click to toggle source
# File lib/totally_lazy/sequence.rb, line 314
def realise
  Sequence.new(@enumerator.to_a.lazy)
end
reduce(fn=nil, &block) click to toggle source
# 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
Also aliased as: reduce_left
reduce_left(fn=nil, &block)
Alias for: reduce
reduce_right(fn=nil, &block) click to toggle source
# 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) click to toggle source
# 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
reverse() click to toggle source
# File lib/totally_lazy/sequence.rb, line 156
def reverse
  Sequence.new(reverse_enumerator(@enumerator))
end
second() click to toggle source
# File lib/totally_lazy/sequence.rb, line 140
def second
  tail.head
end
size() click to toggle source
# File lib/totally_lazy/sequence.rb, line 130
def size
  @enumerator.count
end
sort_by(comparator) click to toggle source
# File lib/totally_lazy/sequence.rb, line 270
def sort_by(comparator)
  Sequence.sort(self, comparator)
end
tail() click to toggle source
# 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) click to toggle source
# File lib/totally_lazy/sequence.rb, line 243
def take(count)
  Sequence.take(self, count)
end
take_while(fn_pred=nil, &block_pred) click to toggle source
# 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_a() click to toggle source
# File lib/totally_lazy/sequence.rb, line 329
def to_a
  @enumerator.to_a
end
to_s() click to toggle source
# 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) click to toggle source
# File lib/totally_lazy/sequence.rb, line 230
def zip(other)
  Sequence.new(pair_enumerator(@enumerator, other.enumerator))
end
zip_with_index() click to toggle source
# File lib/totally_lazy/sequence.rb, line 234
def zip_with_index
  Sequence.zip(range_from(0), self)
end

Private Instance Methods

seed(enumerator, fn) click to toggle source
# File lib/totally_lazy/sequence.rb, line 347
def seed(enumerator, fn)
  enumerator.rewind
  !fn.nil? && fn.respond_to?(:identity) ? fn.identity : enumerator.next
end