Class: Array

Inherits:
Object show all
Defined in:
lib/epitools/permutations.rb,
lib/epitools/core_ext/array.rb

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object

Overridden multiplication operator. Now lets you multiply the Array by another Array or Enumerable.

Array * Integer == a new array with <Integer> copies of itself inside Array * String == a new string containing the elements, joined by the <String> Array * or Enumerable == the cross product (aka. cartesian product) of both arrays



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/epitools/permutations.rb', line 15

def *(other)
  if other.is_a? Array
    # cross-product
    result = []
    (0...self.size).each do |a|
      (0...other.size).each do |b|
        result << [self[a], other[b]]
      end
    end
    result
  else
    send(:mult, other)
  end
end

#**(n) ⇒ Object

Multiply the array by itself ‘n` times.



33
34
35
# File 'lib/epitools/permutations.rb', line 33

def **(exponent)
  ([self] * exponent).foldl(:*)
end

#/(pieces) ⇒ Object

Divide the array into n pieces.



135
136
137
138
# File 'lib/epitools/core_ext/array.rb', line 135

def / pieces
  piece_size = (size.to_f / pieces).ceil
  each_slice(piece_size).to_a
end

#^(other) ⇒ Object

XOR operator



98
99
100
# File 'lib/epitools/core_ext/array.rb', line 98

def ^(other)
  (self | other) - (self & other)
end

#all_pairs(reflexive = false) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/epitools/permutations.rb', line 37

def all_pairs(reflexive=false)
  (0...size).each do |a|
    start = reflexive ? a : a+1
    (start...size).each do |b|
      yield self[a], self[b]
    end
  end
end

#histogram(n_buckets = 10, options = {}) ⇒ Object

Takes an array of numbers, puts them into equal-sized buckets, and counts the buckets (aka. A Histogram!)

Examples:

[1,2,3,4,5,6,7,8,9].histogram(3) #=> [3,3,3]
[1,2,3,4,5,6,7,8,9].histogram(2) #=> [4,5]
[1,2,3,4,5,6,7,8,9].histogram(2, ranges: true)
   #=> {
         1.0...5.0 => 4,
         5.0...9.0 => 5
       }


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/epitools/core_ext/array.rb', line 166

def histogram(n_buckets=10, options={})
  
  use_ranges = options[:ranges] || options[:hash]

  min_val     = min
  max_val     = max
  range       = (max_val - min_val)
  bucket_size = range.to_f / n_buckets
  buckets     = [0]*n_buckets

  # p [range, bucket_size, buckets, min_val, max_val]

  each do |e|
    bucket = (e - min_val) / bucket_size
    bucket = n_buckets - 1 if bucket >= n_buckets
    # p [:e, e, :bucket, bucket]
    buckets[bucket] += 1
  end

  if use_ranges
    ranges = (0...n_buckets).map do |n|
      offset = n*bucket_size
      (min_val + offset) ... (min_val + offset + bucket_size)
    end
    Hash[ ranges.zip(buckets) ]
  else
    buckets
  end

end

#meanObject Also known as: average

Find the statistical mean



75
76
77
# File 'lib/epitools/core_ext/array.rb', line 75

def mean
  sum / size.to_f
end

#medianObject

Find the statistical median (middle value in the sorted dataset)



83
84
85
# File 'lib/epitools/core_ext/array.rb', line 83

def median
  sort.middle
end

#middleObject

Pick the middle element



68
69
70
# File 'lib/epitools/core_ext/array.rb', line 68

def middle
  self[(size-1) / 2]
end

#modeObject

Find the statistical “mode” (most frequently occurring value)



91
92
93
# File 'lib/epitools/core_ext/array.rb', line 91

def mode
  counts.max_by { |k,v| v }.first
end

#multObject



5
# File 'lib/epitools/permutations.rb', line 5

alias_method :mult, :"*"

#remove_if(&block) ⇒ Object

Removes the elements from the array for which the block evaluates to true. In addition, return the removed elements.

For example, if you wanted to split an array into evens and odds:

nums = [1,2,3,4,5,6,7,8,9,10,11,12]
even = nums.remove_if { |n| n.even? }   # remove all even numbers from the "nums" array and return them
odd  = nums                             # "nums" now only contains odd numbers


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/epitools/core_ext/array.rb', line 31

def remove_if(&block)
  removed = []

  delete_if do |x|
    if yield(x)
      removed << x
      true
    else
      false
    end
  end

  removed
end

#rzip(other) ⇒ Object

zip from the right (or reversed zip.)

eg:

>> [5,39].rzip([:hours, :mins, :secs])
=> [ [:mins, 5], [:secs, 39] ]


53
54
55
56
# File 'lib/epitools/core_ext/array.rb', line 53

def rzip(other)
  reverse_each.zip(other.reverse_each).reverse_each
  # reverse.zip(other.reverse).reverse # That's a lotta reverses!
end

#sample(n = 1) ⇒ Object Also known as: pick



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/epitools/core_ext/array.rb', line 115

def sample(n=1)
  return self[rand sz] if n == 1

  sz      = size
  indices = []

  loop do
    indices += (0..n*1.2).map { rand sz }
    indices.uniq
    break if indices.size >= n
  end

  values_at(*indices[0...n])
end

#shuffleObject



106
107
108
# File 'lib/epitools/core_ext/array.rb', line 106

def shuffle
  sort_by { rand }
end

#split_at(*args, &block) ⇒ Object

See: Enumerable#split_at



61
62
63
# File 'lib/epitools/core_ext/array.rb', line 61

def split_at(*args, &block)
  super.to_a
end

#squashObject

flatten.compact.uniq



13
14
15
# File 'lib/epitools/core_ext/array.rb', line 13

def squash
  flatten.compact.uniq
end

#to_hObject

Convert the array to a hash



145
146
147
148
149
150
151
# File 'lib/epitools/core_ext/array.rb', line 145

def to_h
  if self.first.is_a? Array
    Hash[self]
  else
    Hash[*self]
  end
end