Module: Enumerable

Defined in:
lib/lite/ruby/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#clusterObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/lite/ruby/enumerable.rb', line 6

def cluster
  each_with_object([]) do |ele, results|
    last_res = results.last

    if last_res && (yield(ele) == yield(last_res.last))
      last_res << ele
    else
      results << [ele]
    end
  end
end

#cluster_by(&block) ⇒ Object



18
19
20
# File 'lib/lite/ruby/enumerable.rb', line 18

def cluster_by(&block)
  group_by(&block).sort.transpose.pop || []
end

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



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

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

#drop_last(num) ⇒ Object



30
31
32
33
34
35
# File 'lib/lite/ruby/enumerable.rb', line 30

def drop_last(num)
  collection_size = to_a.size
  return self if num > collection_size

  self[0...(collection_size - num)]
end

#drop_last_ifObject



37
38
39
40
41
42
43
44
# File 'lib/lite/ruby/enumerable.rb', line 37

def drop_last_if
  dropping = true
  reverse_each.with_object([]) do |val, arr|
    next if dropping &&= yield(val)

    arr.unshift(val)
  end
end

#exactly?(num) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lite/ruby/enumerable.rb', line 46

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

#excase?(object) ⇒ Boolean

rubocop:disable Style/CaseEquality

Returns:

  • (Boolean)


59
60
61
# File 'lib/lite/ruby/enumerable.rb', line 59

def excase?(object)
  none? { |val| object === val }
end

#exclude?(object) ⇒ Boolean

rubocop:enable Style/CaseEquality

Returns:

  • (Boolean)


64
65
66
# File 'lib/lite/ruby/enumerable.rb', line 64

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

#expandObject



68
69
70
# File 'lib/lite/ruby/enumerable.rb', line 68

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

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



72
73
74
75
76
77
78
# File 'lib/lite/ruby/enumerable.rb', line 72

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

#frequencyObject Also known as: occurrences



80
81
82
# File 'lib/lite/ruby/enumerable.rb', line 80

def frequency
  each_with_object(Hash.new(0)) { |val, hash| hash[val] += 1 }
end

#incase?(object) ⇒ Boolean

rubocop:disable Style/CaseEquality

Returns:

  • (Boolean)


87
88
89
# File 'lib/lite/ruby/enumerable.rb', line 87

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

#interpose(sep, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lite/ruby/enumerable.rb', line 93

def interpose(sep, &block)
  enum = Enumerator.new do |val|
    items = each

    loop do
      begin
        val << items.next
      rescue StopIteration
        break
      end

      begin
        items.peek
      rescue StopIteration
        break
      else
        val << sep
      end
    end
  end

  block ? enum.each(&block) : enum
end

#many?Boolean

rubocop:enable Metrics/MethodLength

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/lite/ruby/enumerable.rb', line 118

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

#modulate(modulo) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lite/ruby/enumerable.rb', line 131

def modulate(modulo)
  if modulo == 1
    to_a
  elsif size % modulo != 0
    raise ArgumentError, "Invalid modulo: #{modulo.inspect}"
  else
    (0...size).each_with_object(Array.new(modulo, [])) do |i, array|
      array[i % modulo] += [self[i]]
    end
  end
end

#occur(amount = nil) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/lite/ruby/enumerable.rb', line 144

def occur(amount = nil)
  result = Hash.new { |hash, key| hash[key] = [] }

  each do |item|
    key = item
    result[key] << item
  end

  if block_given?
    result.select! { |_key, values| yield(values.size) }
  else
    raise ArgumentError, 'Invalid occur amount' unless amount

    if amount.is_a?(Range)
      result.select! { |_key, values| amount.include?(values.size) }
    else
      result.select! { |_key, values| values.size == amount }
    end
  end

  result.values.flatten.uniq
end

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

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



168
169
170
171
172
173
174
# File 'lib/lite/ruby/enumerable.rb', line 168

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

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



176
177
178
179
180
181
182
# File 'lib/lite/ruby/enumerable.rb', line 176

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

#several?Boolean

Returns:

  • (Boolean)


184
185
186
187
188
189
190
191
192
193
194
# File 'lib/lite/ruby/enumerable.rb', line 184

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
end

#squeeze(*limited_to) ⇒ Object

rubocop:disable Metrics/MethodLength



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/lite/ruby/enumerable.rb', line 197

def squeeze(*limited_to)
  first = true
  current = nil

  each_with_object([]) do |val, array|
    if !limited_to.empty? && !limited_to.include?(val)
      array << val
    elsif first || current != val
      array << val
      first = false
      current = val
    end
  end
end

#take_last(num) ⇒ Object

rubocop:enable Metrics/MethodLength



213
214
215
216
217
218
# File 'lib/lite/ruby/enumerable.rb', line 213

def take_last(num)
  collection_size = to_a.size
  return self if num > collection_size

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

#take_last_ifObject



220
221
222
223
224
225
226
# File 'lib/lite/ruby/enumerable.rb', line 220

def take_last_if
  reverse_each.with_object([]) do |val, arr|
    break arr unless yield(val)

    arr.unshift(val)
  end
end