module Apricot::Seq

Every seq should include this module and define ‘first’ and ‘next’ methods. A seq may redefine ‘rest’ and ‘each’ if there is a more efficient way to implement them.

‘first’ should return the first item in the seq. ‘next’ should return a seq of the rest of the items in the seq, or nil

if there are no more items.

Public Instance Methods

<=>(other) click to toggle source
# File lib/apricot/seq.rb, line 50
def <=>(other)
  return unless other.is_a?(Seq) || other.nil?
  s, o = self, other

  while s && o
    comp = s.first <=> o.first
    return comp unless comp == 0
    s = s.next
    o = o.next
  end

  if s
    1
  elsif o
    -1
  else
    0
  end
end
cons(x) click to toggle source
# File lib/apricot/seq.rb, line 46
def cons(x)
  Cons.new(x, self)
end
each() { |first| ... } click to toggle source
# File lib/apricot/seq.rb, line 17
def each
  s = self

  while s
    yield s.first
    s = s.next
  end

  self
end
empty?() click to toggle source
# File lib/apricot/seq.rb, line 32
def empty?
  false
end
hash() click to toggle source
# File lib/apricot/seq.rb, line 72
def hash
  hashes = map {|x| x.hash }
  hashes.reduce(hashes.size) {|acc,hash| acc ^ hash }
end
inspect()
Alias for: to_s
last() click to toggle source
# File lib/apricot/seq.rb, line 36
def last
  s = self

  while s.next
    s = s.next
  end

  s.first
end
rest() click to toggle source
# File lib/apricot/seq.rb, line 13
def rest
  self.next || List::EMPTY_LIST
end
to_s() click to toggle source
# File lib/apricot/seq.rb, line 77
def to_s
  str = '('
  each {|x| str << x.apricot_inspect << ' ' }
  str.chop!
  str << ')'
end
Also aliased as: inspect
to_seq() click to toggle source
# File lib/apricot/seq.rb, line 28
def to_seq
  self
end