class CooCoo::Sequence

Public Class Methods

[](value, max_size = nil) click to toggle source
# File lib/coo-coo/sequence.rb, line 13
def self.[](value, max_size = nil)
  self.new(value.to_a)
  # ret = new(max_size || value.size)
  # value.each_with_index do |v, i|
  #   ret[i] = v
  # end
  # ret
end
new(length, &init) click to toggle source
# File lib/coo-coo/sequence.rb, line 5
def initialize(length, &init)
  if length.kind_of?(Array)
    @elements = length
  else
    @elements = Array.new(length, &init)
  end
end

Public Instance Methods

*(other) click to toggle source
# File lib/coo-coo/sequence.rb, line 151
def *(other)
  v = if other.respond_to?(:each)
        raise ArgumentError.new("Size mismatch: #{size} != #{other.size}") if size != other.size
        other.each.zip(each).collect do |oe, se|
      se * oe
    end
      else
        each.collect do |e|
      e * other
    end
      end

  self.class[v]
end
+(other) click to toggle source
# File lib/coo-coo/sequence.rb, line 113
def +(other)
  v = if other.respond_to?(:each)
        raise ArgumentError.new("Size mismatch: #{size} != #{other.size}") if size != other.size
        other.each.zip(each).collect do |oe, se|
      se + oe
    end
      else
        each.collect do |e|
      e + other
    end
      end
  
  self.class[v]
end
-(other) click to toggle source
# File lib/coo-coo/sequence.rb, line 128
def -(other)
  v = if other.respond_to?(:each)
        raise ArgumentError.new("Size mismatch #{size} != #{other.size}") if size != other.size
        other.each.zip(each).collect do |oe, se|
      se - oe
    end
      else
        each.collect do |e|
      e - other
    end
      end
  
  self.class[v]
end
-@() click to toggle source
# File lib/coo-coo/sequence.rb, line 109
def -@
  self.class[@elements.collect(&:-@)]
end
/(other) click to toggle source
# File lib/coo-coo/sequence.rb, line 166
def /(other)
  v = if other.respond_to?(:each)
        raise ArgumentError.new("Size mismatch: #{size} != #{other.size}") if size != other.size
        other.each.zip(each).collect do |oe, se|
      se / oe
    end
      else
        each.collect do |e|
      e / other
    end
      end

  self.class[v]
end
==(other) click to toggle source
# File lib/coo-coo/sequence.rb, line 181
def ==(other)
  other.size == size && each.zip(other.each).all? do |a, b|
    a == b
  end
end
[](i, len = nil) click to toggle source
# File lib/coo-coo/sequence.rb, line 42
def [](i, len = nil)
  v = @elements[i, len || 1]
  if len
    self.class[v]
  else
    v[0]
  end
end
[]=(i, v) click to toggle source
# File lib/coo-coo/sequence.rb, line 51
def []=(i, v)
  @elements[i] = v
  self
end
append(other) click to toggle source
# File lib/coo-coo/sequence.rb, line 78
def append(other)
  v = self.class.new(size + other.size)
  each_with_index do |e, i|
    v[i] = e
  end
  other.each_with_index do |e, i|
    v[i + size] = e
  end
  v
end
average() click to toggle source
# File lib/coo-coo/sequence.rb, line 101
def average
  sum / size.to_f
end
collect(&block) click to toggle source
Calls superclass method
# File lib/coo-coo/sequence.rb, line 66
def collect(&block)
  self.class[super]
end
each(&block) click to toggle source
# File lib/coo-coo/sequence.rb, line 56
def each(&block)
  @elements.each(&block)
end
each_with_index(&block) click to toggle source
# File lib/coo-coo/sequence.rb, line 60
def each_with_index(&block)
  each.each_with_index(&block)
end
last(*args) click to toggle source
# File lib/coo-coo/sequence.rb, line 74
def last(*args)
  @elements.last(*args)
end
length() click to toggle source
# File lib/coo-coo/sequence.rb, line 147
def length
  @elements.size
end
reverse() click to toggle source
# File lib/coo-coo/sequence.rb, line 70
def reverse
  self.class[@elements.reverse]
end
size() click to toggle source
# File lib/coo-coo/sequence.rb, line 143
def size
  @elements.size
end
sqrt() click to toggle source
# File lib/coo-coo/sequence.rb, line 105
def sqrt
  self.class[@elements.collect(&:sqrt)]
end
sum() click to toggle source
# File lib/coo-coo/sequence.rb, line 95
def sum
  @elements.drop(1).inject(@elements[0]) do |acc, e|
    acc + e
  end
end
to_a() click to toggle source

def coerce(other)

if other.respond_to?(:each)
  return self.class[other], self
else
  return self.class.new(self.size, other), self
end

end

# File lib/coo-coo/sequence.rb, line 30
def to_a
  @elements
end
to_s() click to toggle source
# File lib/coo-coo/sequence.rb, line 34
def to_s
  values = each.collect do |e|
    e.to_s
  end

  "[#{values.join(', ')}]"
end
zero() click to toggle source
# File lib/coo-coo/sequence.rb, line 89
def zero
  self.class.new(size) do |i|
    self[0].zero
  end
end