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



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

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 <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.



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

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

#^(other) ⇒ Object

XOR operator



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

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



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

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

#columnsObject Also known as: cols



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

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
       }


269
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
# File 'lib/epitools/core_ext/array.rb', line 269

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



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

def mean
  sum / size.to_f
end

#medianObject

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



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

def median
  sort.middle
end

#middleObject

Pick the middle element



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

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

#modeObject

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



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

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


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

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



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

def row(n)
  rows[n]
end

#rowsObject

Pseudo-matrix methods



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

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)



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

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)



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

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



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

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`)



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

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

#rzip(other) ⇒ Object

see: Enumerable#rzip



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

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



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

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



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

def shuffle
  sort_by { rand }
end

#split_at(*args, &block) ⇒ Object

See: Enumerable#split_at



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

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

#squashObject

flatten.compact.uniq



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

def squash
  flatten.compact.uniq
end

#to_hObject



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

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

#to_ostructObject

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



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

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

#transpose_with_paddingObject

Transpose an array that could have rows of uneven length



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

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