Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/evoc/array.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.powerset(set) ⇒ Object



48
49
50
51
52
53
# 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

Instance Method Details

#array_difference(other) ⇒ Object

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



44
45
46
# File 'lib/evoc/array.rb', line 44

def array_difference(other)
  self.map {|a| a - other}.array_union
end

#array_intersectionObject

returns the intersection of a list of lists



34
35
36
37
38
39
40
# File 'lib/evoc/array.rb', line 34

def array_intersection
  if intersection = self.inject(:&)
    return intersection
  else
    return []
  end
end

#array_unionObject

returns the union of an array of arraya



24
25
26
27
28
29
30
# File 'lib/evoc/array.rb', line 24

def array_union
  if union = self.inject(:|)
    return union
  else
    return []
  end
end

#include_any?(other) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/evoc/array.rb', line 18

def include_any?(other)
  (self & other).size > 0
end

#meanObject



3
4
5
# 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) ⇒ Object



7
8
9
10
11
12
# 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) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/evoc/array.rb', line 14

def subset?(other)
  self & other == self
end