Module: RRList::Functions

Defined in:
lib/rrlist/functions.rb

Overview

A helper module that provides some Proc object to be use with RRList::List

Examples:

RRList::List.new :size => 10, :range => 5, &RRList::Functions.max
RRList::List.new :size => 10, :range => 5, &RRList::Functions.incr

Author:

  • Federico Dayan

Class Method Summary collapse

Class Method Details

.avgProc

Returns A proc to be used in a RRList::List that calculates the average of the values inserted at and index.

Returns:

  • (Proc)

    A proc to be used in a RRList::List that calculates the average of the values inserted at and index



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rrlist/functions.rb', line 11

def self.avg
  Proc.new do |index, old_value, new_value|
    case new_value
      when Hash
        if old_value
          {
            value: RRList::Functions.calc_average(old_value[:size],old_value[:value],new_value[:value],new_value[:size]),
            size: old_value[:size] + new_value[:size]
          }
        else
          {
            value: new_value[:value],
            size: new_value[:size],
          }
        end
      else
         if old_value
          {
            value: RRList::Functions.calc_average(old_value[:size],old_value[:value],new_value),
            size: old_value[:size] + 1
          }
        else
          {
            value: new_value,
            size: 1,
          }
        end
     end
  end
end

.decrProc

Returns A proc to be used in a RRList::List decrements the values inserted at and index.

Returns:

  • (Proc)

    A proc to be used in a RRList::List decrements the values inserted at and index



64
65
66
67
68
# File 'lib/rrlist/functions.rb', line 64

def self.decr
  Proc.new do |index, old_value, new_value|
    old_value ? (old_value - new_value) : new_value
  end
end

.incrProc

Returns A proc to be used in a RRList::List increments the values inserted at and index.

Returns:

  • (Proc)

    A proc to be used in a RRList::List increments the values inserted at and index



57
58
59
60
61
# File 'lib/rrlist/functions.rb', line 57

def self.incr
  Proc.new do |index, old_value, new_value|
    old_value ? (old_value + new_value) : new_value
  end
end

.maxProc

Returns A proc to be used in a RRList::List that calculates the max of the values inserted at and index.

Returns:

  • (Proc)

    A proc to be used in a RRList::List that calculates the max of the values inserted at and index



43
44
45
46
47
# File 'lib/rrlist/functions.rb', line 43

def self.max
  Proc.new do |index, old_value, new_value|
    old_value.nil? || (new_value > old_value) ? new_value : old_value
  end
end

.minProc

Returns A proc to be used in a RRList::List that calculates the min of the values inserted at and index.

Returns:

  • (Proc)

    A proc to be used in a RRList::List that calculates the min of the values inserted at and index



50
51
52
53
54
# File 'lib/rrlist/functions.rb', line 50

def self.min
  Proc.new do |index, old_value, new_value|
    old_value.nil? || (new_value < old_value) ? new_value : old_value
  end
end