Module: ArrayHelpers

Defined in:
lib/nswtopo/helpers/array.rb

Instance Method Summary collapse

Instance Method Details

#in_twoObject



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

def in_two
  each_slice(1 + [length - 1, 0].max / 2)
end

#many?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/nswtopo/helpers/array.rb', line 10

def many?
  length > 1
end

#meanObject



6
7
8
# File 'lib/nswtopo/helpers/array.rb', line 6

def mean
  empty? ? nil : inject(&:+) / length
end

#medianObject



2
3
4
# File 'lib/nswtopo/helpers/array.rb', line 2

def median
  sort[length / 2]
end

#nearby_pairs(closed = false, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/nswtopo/helpers/array.rb', line 18

def nearby_pairs(closed = false, &block)
  Enumerator.new do |yielder|
    each.with_index do |element1, index|
      (closed ? rotate(index) : drop(index)).drop(1).each do |element2|
        break unless block.call [element1, element2]
        yielder << [element1, element2]
      end
    end
  end
end