Class: Array

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

Defined Under Namespace

Modules: ToCSV

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ToCSV

#to_csv, #to_tsv

Class Method Details

.matrix(height, width, initial_value = nil) ⇒ Object

Create a 2D matrix out of arrays



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/epitools/core_ext/array.rb', line 197

def self.matrix(height, width, initial_value=nil)
  if block_given?
    height.times.map do |row|
      width.times.map do |col|
        yield(row, col)
      end
    end
  else
    height.times.map do
      [initial_value] * width
    end
  end
end

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 copies of itself inside Array * String == a new string containing the elements, joined by the 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.



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

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

#^(other) ⇒ Object

XOR operator



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

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

#column(n) ⇒ Object Also known as: col

Return column n of a 2D array



189
190
191
# File 'lib/epitools/core_ext/array.rb', line 189

def column(n)
  columns[n]&.rtrim!
end

#columns ⇒ Object Also known as: cols



172
173
174
175
176
# File 'lib/epitools/core_ext/array.rb', line 172

def columns
  cols = transpose_with_padding
  cols.each &:rtrim!
  cols
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
     }


270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/epitools/core_ext/array.rb', line 270

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

#mean ⇒ Object Also known as: average

Find the statistical mean



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

def mean
  sum / size.to_f
end

#median ⇒ Object

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



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

def median
  sort.middle
end

#middle ⇒ Object

Pick the middle element



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

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

#mode ⇒ Object

Find the statistical "mode" (most frequently occurring value)



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

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

#mult ⇒ Object



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


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

def remove_if(&block)
  removed = []

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

  removed
end

#row(n) ⇒ Object

Return row n of a 2D array



182
183
184
# File 'lib/epitools/core_ext/array.rb', line 182

def row(n)
  rows[n]
end

#rows ⇒ Object

Pseudo-matrix methods



168
169
170
# File 'lib/epitools/core_ext/array.rb', line 168

def rows
  self
end

#rpad(target_width) ⇒ Object

Return a copy of this array which has been extended to target_width by adding nils to the end (right side)



230
231
232
# File 'lib/epitools/core_ext/array.rb', line 230

def rpad(target_width)
  dup.rpad!(target_width)
end

#rpad!(target_width) ⇒ Object

Extend the array the target_width by adding nils to the end (right side)



220
221
222
223
224
225
# File 'lib/epitools/core_ext/array.rb', line 220

def rpad!(target_width)
  if target_width > size and target_width > 0
    self[target_width-1] = nil
  end
  self
end

#rtrim(element = nil) ⇒ Object

Like rtrim!, but returns a trimmed copy of the array



160
161
162
# File 'lib/epitools/core_ext/array.rb', line 160

def rtrim(element=nil)
  dup.rtrim!(element)
end

#rtrim!(element = nil) ⇒ Object

Remove instances of "element" from the end of the array (using Array#pop)



152
153
154
155
# File 'lib/epitools/core_ext/array.rb', line 152

def rtrim!(element=nil)
  pop while last == element
  self
end

#rzip(other) ⇒ Object

see: Enumerable#rzip



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

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

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



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

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

#shuffle ⇒ Object



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

def shuffle
  sort_by { rand }
end

#split_at(*args, &block) ⇒ Object

See: Enumerable#split_at



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

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

#squash ⇒ Object

flatten.compact.uniq



15
16
17
# File 'lib/epitools/core_ext/array.rb', line 15

def squash
  flatten.compact.uniq
end

#to_h ⇒ Object



238
239
240
241
242
243
244
# File 'lib/epitools/core_ext/array.rb', line 238

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

#to_ostruct ⇒ Object

Convert an Array that contanis Hashes to a new Array that contains OpenStructs



251
252
253
254
255
# File 'lib/epitools/core_ext/array.rb', line 251

def to_ostruct
  map do |e|
    e.respond_to?(:to_ostruct) ? e.to_ostruct : e
  end
end

#transpose_with_padding ⇒ Object

Transpose an array that could have rows of uneven length



144
145
146
147
# File 'lib/epitools/core_ext/array.rb', line 144

def transpose_with_padding
  max_width = map(&:size).max
  map { |row| row.rpad(max_width) }.transpose
end