Module: Enumerable
- Defined in:
- lib/active_object/enumerable.rb
Instance Method Summary collapse
-
#cluster(&block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
-
#difference(identity = 0, &block) ⇒ Object
rubocop:enable Lint/UnusedMethodArgument.
- #divisible(identity = 0, &block) ⇒ Object
- #drop_last(num) ⇒ Object
- #drop_last_if ⇒ Object
- #exactly?(num) ⇒ Boolean
- #exclude?(object) ⇒ Boolean
- #expand ⇒ Object
- #exponential(identity = 0, &block) ⇒ Object
- #frequencies ⇒ Object
-
#incase?(object) ⇒ Boolean
rubocop:disable Style/CaseEquality.
-
#many? ⇒ Boolean
rubocop:enable Style/CaseEquality.
- #max(identity = 0) ⇒ Object
- #mean(identity = 0) ⇒ Object (also: #average)
- #median(identity = 0) ⇒ Object
- #min(identity = 0) ⇒ Object
-
#mode(identity = 0) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#multiple(identity = 0, &block) ⇒ Object
rubocop:ensable Metrics/AbcSize.
- #range(identity = 0) ⇒ Object
- #several? ⇒ Boolean
- #standard_deviation(identity = 0) ⇒ Object
- #sum(identity = 0, &block) ⇒ Object
- #take_last(num) ⇒ Object
- #take_last_if ⇒ Object
- #variance(identity = 0) ⇒ Object
Instance Method Details
#cluster(&block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/active_object/enumerable.rb', line 4 def cluster(&block) result = [] each do |ele| last_res = result.last if last_res && (yield(last_res.last) == yield(ele)) last_res << ele else result << [ele] end end result end |
#difference(identity = 0, &block) ⇒ Object
rubocop:enable Lint/UnusedMethodArgument
19 20 21 22 23 24 25 |
# File 'lib/active_object/enumerable.rb', line 19 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
27 28 29 30 31 32 33 |
# File 'lib/active_object/enumerable.rb', line 27 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
35 36 37 38 39 40 41 |
# File 'lib/active_object/enumerable.rb', line 35 def drop_last(num) collection_length = to_a.length return(self) if num > collection_length self[0...(collection_length - num)] end |
#drop_last_if ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/active_object/enumerable.rb', line 43 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
54 55 56 57 58 59 60 61 62 |
# File 'lib/active_object/enumerable.rb', line 54 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
64 65 66 |
# File 'lib/active_object/enumerable.rb', line 64 def exclude?(object) !include?(object) end |
#expand ⇒ Object
68 69 70 |
# File 'lib/active_object/enumerable.rb', line 68 def map { |val| val.is_a?(Enumerable) ? val. : val } end |
#exponential(identity = 0, &block) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/active_object/enumerable.rb', line 72 def exponential(identity = 0, &block) if block_given? map(&block).exponential(identity) else inject { |key, val| key**val } || identity end end |
#frequencies ⇒ Object
80 81 82 |
# File 'lib/active_object/enumerable.rb', line 80 def frequencies each_with_object(Hash.new(0)) { |key, hsh| hsh[key] += 1 } end |
#incase?(object) ⇒ Boolean
rubocop:disable Style/CaseEquality
85 86 87 |
# File 'lib/active_object/enumerable.rb', line 85 def incase?(object) any? { |val| object === val } end |
#many? ⇒ Boolean
rubocop:enable Style/CaseEquality
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/active_object/enumerable.rb', line 90 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
102 103 104 |
# File 'lib/active_object/enumerable.rb', line 102 def max(identity = 0) (length rescue count).positive? ? sort.last : identity end |
#mean(identity = 0) ⇒ Object Also known as: average
110 111 112 113 114 115 |
# File 'lib/active_object/enumerable.rb', line 110 def mean(identity = 0) return(identity) unless length.positive? collection_length = length sum.to_f / collection_length.to_f end |
#median(identity = 0) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/active_object/enumerable.rb', line 119 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
106 107 108 |
# File 'lib/active_object/enumerable.rb', line 106 def min(identity = 0) (length rescue count).positive? ? sort.first : identity end |
#mode(identity = 0) ⇒ Object
rubocop:disable Metrics/AbcSize
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/active_object/enumerable.rb', line 135 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 if frequency_top_two.length != 1 && top_two_first.last == frequency_top_two.last.last nil else top_two_first.first end end |
#multiple(identity = 0, &block) ⇒ Object
rubocop:ensable Metrics/AbcSize
150 151 152 153 154 155 156 |
# File 'lib/active_object/enumerable.rb', line 150 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
158 159 160 161 162 163 |
# File 'lib/active_object/enumerable.rb', line 158 def range(identity = 0) return(identity) unless length.positive? collection_sorted = sort collection_sorted.last - collection_sorted.first end |
#several? ⇒ Boolean
165 166 167 168 169 170 171 172 173 |
# File 'lib/active_object/enumerable.rb', line 165 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
175 176 177 178 179 |
# File 'lib/active_object/enumerable.rb', line 175 def standard_deviation(identity = 0) return(identity) if length < 2 Math.sqrt(variance) end |
#sum(identity = 0, &block) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/active_object/enumerable.rb', line 181 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
189 190 191 192 193 194 195 |
# File 'lib/active_object/enumerable.rb', line 189 def take_last(num) collection_length = to_a.length return(self) if num > collection_length self[(collection_length - num)..-1] end |
#take_last_if ⇒ Object
197 198 199 200 201 202 203 |
# File 'lib/active_object/enumerable.rb', line 197 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
205 206 207 208 209 210 211 212 |
# File 'lib/active_object/enumerable.rb', line 205 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 |