class Array

Public Class Methods

powerset(set) click to toggle source
# File lib/evoc/array.rb, line 48
def self.powerset(set)
  return [set] if set.empty?
  p = set.pop
  subset = powerset(set)  
  subset | subset.map { |x| x | [p] }
end

Public Instance Methods

array_difference(other) click to toggle source

returns the list of items in self that was not in other

# File lib/evoc/array.rb, line 44
def array_difference(other)
  self.map {|a| a - other}.array_union
end
array_intersection() click to toggle source

returns the intersection of a list of lists

# File lib/evoc/array.rb, line 34
def array_intersection
  if intersection = self.inject(:&)
    return intersection
  else
    return []
  end
end
array_union() click to toggle source

returns the union of an array of arraya

# File lib/evoc/array.rb, line 24
def array_union
  if union = self.inject(:|)
    return union
  else
    return []
  end
end
include_any?(other) click to toggle source
# File lib/evoc/array.rb, line 18
def include_any?(other)
  (self & other).size > 0
end
mean() click to toggle source
# File lib/evoc/array.rb, line 3
def mean
  self.inject(0) { |sum, x| sum += x } / self.size.to_f
end
median(already_sorted=false) click to toggle source
# File lib/evoc/array.rb, line 7
def median(already_sorted=false)
  return nil if self.empty?
  array = (already_sorted ? self : self.sort)
  m_pos = array.size / 2
  return array.size % 2 == 1 ? array[m_pos] : array[m_pos-1..m_pos].mean
end
subset?(other) click to toggle source
# File lib/evoc/array.rb, line 14
def subset?(other)
  self & other == self
end