Module: Enumerable

Defined in:
lib/active_object/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#cluster(&block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/active_object/enumerable.rb', line 3

def cluster(&block)
  result = []

  each do |element|
    if result.last && (yield(result.last.last) == yield(element))
      result.last << element
    else
      result << [element]
    end
  end

  result
end

#difference(identity = 0, &block) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/active_object/enumerable.rb', line 17

def difference(identity=0, &block)
  if block_given?
    map(&block).difference(identity)
  else
    inject { |d,v| d - v } || identity
  end
end

#divisible(identity = 0, &block) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/active_object/enumerable.rb', line 25

def divisible(identity=0, &block)
  if block_given?
    map(&block).divisible(identity)
  else
    inject { |d,v| d / v } || identity
  end
end

#drop_last(n) ⇒ Object



33
34
35
36
37
# File 'lib/active_object/enumerable.rb', line 33

def drop_last(n)
  collection_size = to_a.size
  return(self) if n > collection_size
  self[0...(collection_size - n)]
end

#drop_last_ifObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/active_object/enumerable.rb', line 39

def drop_last_if
  return(to_enum(:drop_last_if)) unless block_given?

  result   = []
  dropping = true
  reverse_each do |value|
    result.unshift(value) unless dropping &&= yield(value)
  end
  result
end

#exactly?(n) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/active_object/enumerable.rb', line 50

def exactly?(n)
  found_count = 0
  block_given? ? each { |*o| found_count += 1 if yield(*o) } : each { |o| found_count += 1 if o }
  (found_count > n) ? false : n == found_count
end

#exclude?(object) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/active_object/enumerable.rb', line 57

def exclude?(object)
  !include?(object)
end

#expandObject



62
63
64
# File 'lib/active_object/enumerable.rb', line 62

def expand
  map { |n| n.is_a?(Enumerable) ? n.expand : n }
end

#exponential(identity = 0, &block) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/active_object/enumerable.rb', line 66

def exponential(identity=0, &block)
  if block_given?
    map(&block).exponential(identity)
  else
    inject { |d,v| d ** v } || identity
  end
end

#frequenciesObject



74
75
76
# File 'lib/active_object/enumerable.rb', line 74

def frequencies
  each_with_object(Hash.new(0)) { |e, a| a[e] += 1 }
end

#incase?(object) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/active_object/enumerable.rb', line 78

def incase?(object)
  any? { |x| object === x }
end

#many?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/active_object/enumerable.rb', line 83

def many?
  found_count = 0
  if block_given?
    any? do |v|
      found_count += 1 if yield v
      found_count > 1
    end
  else
    any? { (found_count += 1) > 1 }
  end
end

#max(identity = 0) ⇒ Object



96
97
98
# File 'lib/active_object/enumerable.rb', line 96

def max(identity=0)
  size > 0 ? sort.last : identity
end

#mean(identity = 0) ⇒ Object



104
105
106
107
108
# File 'lib/active_object/enumerable.rb', line 104

def mean(identity=0)
  return(identity) unless size > 0
  collection_size = size
  sum / collection_size.to_f
end

#median(identity = 0) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/active_object/enumerable.rb', line 110

def median(identity=0)
  collection_size   = size
  collection_sorted = sort

  return(identity) unless collection_size > 0

  if (collection_size % 2).zero?
    (collection_sorted[(collection_size / 2.0) -1.0] + collection_sorted[collection_size / 2.0]) / 2.0
  else
    collection_sorted[collection_size / 2.0]
  end
end

#min(identity = 0) ⇒ Object



100
101
102
# File 'lib/active_object/enumerable.rb', line 100

def min(identity=0)
  size > 0 ? sort.first : identity
end

#mode(identity = 0) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/active_object/enumerable.rb', line 123

def mode(identity=0)
  return(identity) unless size > 0

  frequency_distribution = inject(Hash.new(0)) { |h,v| h[v] += 1; h }
  frequency_top_two      = frequency_distribution.sort { |k,v| v[1] <=> k[1] }.take(2)

  if frequency_top_two.size == 1
    frequency_top_two.first.first
  elsif frequency_top_two.first.last == frequency_top_two.last.last
    nil
  else
    frequency_top_two.first.first
  end
end

#multiple(identity = 0, &block) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/active_object/enumerable.rb', line 138

def multiple(identity=0, &block)
  if block_given?
    map(&block).multiple(identity)
  else
    inject { |d,v| d * v } || identity
  end
end

#range(identity = 0) ⇒ Object



146
147
148
149
150
# File 'lib/active_object/enumerable.rb', line 146

def range(identity=0)
  return(identity) unless size > 0
  collection_sorted = sort
  collection_sorted.last - collection_sorted.first
end

#several?Boolean

Returns:

  • (Boolean)


152
153
154
155
156
# File 'lib/active_object/enumerable.rb', line 152

def several?
  found_count = 0
  block_given? ? each { |*o| found_count += 1 if yield(*o) } : each { |o| found_count += 1 if o }
  (found_count > 1) ? true : false
end

#standard_deviation(identity = 0) ⇒ Object



158
159
160
161
# File 'lib/active_object/enumerable.rb', line 158

def standard_deviation(identity=0)
  return(identity) if size < 2
  Math.sqrt(variance)
end

#sum(identity = 0, &block) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/active_object/enumerable.rb', line 164

def sum(identity=0, &block)
  if block_given?
    map(&block).sum(identity)
  else
    inject { |s,v| s + v } || identity
  end
end

#take_last(n) ⇒ Object



173
174
175
176
177
# File 'lib/active_object/enumerable.rb', line 173

def take_last(n)
  collection_size = to_a.size
  return(self) if n > collection_size
  self[(collection_size - n)..-1]
end

#take_last_ifObject



179
180
181
182
183
184
185
# File 'lib/active_object/enumerable.rb', line 179

def take_last_if
  return(to_enum(:take_last_if)) unless block_given?

  result = []
  reverse_each { |e| yield(e) ? result.unshift(e) : break }
  result
end

#variance(identity = 0) ⇒ Object



187
188
189
190
191
192
# File 'lib/active_object/enumerable.rb', line 187

def variance(identity=0)
  collection_size = size
  return(identity) if collection_size <= 1
  sum = inject(0.0) { |s,v| s + (v - mean) ** 2.0 }
  sum / (collection_size.to_f - 1.0)
end