Module: Enumerable

Defined in:
lib/active_object/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#cluster(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



4
5
6
7
8
9
10
11
# File 'lib/active_object/enumerable.rb', line 4

def cluster(&block)
  result = []
  each do |ele|
    last_res = result.last
    last_res && (yield(last_res.last) == yield(ele)) ? last_res << ele : result << [ele]
  end
  result
end

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

rubocop:enable Lint/UnusedMethodArgument



14
15
16
17
18
19
20
# File 'lib/active_object/enumerable.rb', line 14

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

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



22
23
24
25
26
27
28
# File 'lib/active_object/enumerable.rb', line 22

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

#drop_last(num) ⇒ Object



30
31
32
33
34
# File 'lib/active_object/enumerable.rb', line 30

def drop_last(num)
  collection_length = to_a.length
  return self if num > collection_length
  self[0...(collection_length - num)]
end

#drop_last_ifObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/active_object/enumerable.rb', line 36

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

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

#exactly?(num) ⇒ Boolean



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

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

#exclude?(object) ⇒ Boolean



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

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

#expandObject



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

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

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



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

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

#frequenciesObject



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

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

#incase?(object) ⇒ Boolean

rubocop:disable Style/CaseEquality



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

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

#many?Boolean

rubocop:enable Style/CaseEquality



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 |val|
      found_count += 1 if yield val
      found_count > 1
    end
  else
    any? { (found_count += 1) > 1 }
  end
end

#max(identity = 0) ⇒ Object



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

def max(identity = 0)
  (length rescue count).positive? ? sort.last : identity
end

#mean(identity = 0) ⇒ Object Also known as: average



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

def mean(identity = 0)
  return identity unless length.positive?

  collection_length = length
  sum.to_f / collection_length.to_f
end

#median(identity = 0) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/active_object/enumerable.rb', line 112

def median(identity = 0)
  collection_length = length.to_f
  collection_sorted = sort

  return(identity) unless collection_length > 0.0

  half_collection = collection_length / 2.0
  sorted_collection = collection_sorted[half_collection]

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

#min(identity = 0) ⇒ Object



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

def min(identity = 0)
  (length rescue count).positive? ? sort.first : identity
end

#mode(identity = 0) ⇒ Object

rubocop:disable Metrics/AbcSize



129
130
131
132
133
134
135
136
137
138
# File 'lib/active_object/enumerable.rb', line 129

def mode(identity = 0)
  return identity unless length.positive?

  frequency_distribution = each_with_object(Hash.new(0)) { |val, hsh| hsh[val] += 1 }
  frequency_top_two = frequency_distribution.sort_by { |_, val| -val }.take(2)
  top_two_first = frequency_top_two.first

  return if frequency_top_two.length != 1 && top_two_first.last == frequency_top_two.last.last
  top_two_first.first
end

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

rubocop:ensable Metrics/AbcSize



141
142
143
144
145
146
147
# File 'lib/active_object/enumerable.rb', line 141

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

#range(identity = 0) ⇒ Object



149
150
151
152
153
154
# File 'lib/active_object/enumerable.rb', line 149

def range(identity = 0)
  return identity unless length.positive?

  collection_sorted = sort
  collection_sorted.last - collection_sorted.first
end

#several?Boolean



156
157
158
159
160
161
162
163
164
# File 'lib/active_object/enumerable.rb', line 156

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

#standard_deviation(identity = 0) ⇒ Object



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

def standard_deviation(identity = 0)
  return identity if length < 2

  Math.sqrt(variance)
end

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



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

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

#take_last(num) ⇒ Object



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

def take_last(num)
  collection_length = to_a.length

  return self if num > collection_length

  self[(collection_length - num)..-1]
end

#take_last_ifObject



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

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

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

#variance(identity = 0) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/active_object/enumerable.rb', line 196

def variance(identity = 0)
  collection_length = length

  return identity if collection_length <= 1

  total = inject(0.0) { |sum, val| sum + (val - mean)**2.0 }
  total.to_f / (collection_length.to_f - 1.0)
end