Skip over iteration in Enumerable#collect

43.4k Views Asked by At
(1..4).collect do |x|
  next if x == 3
  x + 1
end # => [2, 3, nil, 5]
    # desired => [2, 3, 5]

If the condition for next is met, collect puts nil in the array, whereas what I'm trying to do is put no element in the returned array if the condition is met. Is this possible without calling delete_if { |x| x == nil } on the returned array?

My code excerpt is heavily abstracted, so looking for a general solution to the problem.

6

There are 6 best solutions below

1
On BEST ANSWER

In Ruby 2.7+, it’s possible to use filter_map for this exact purpose. From the docs:

Returns an array containing truthy elements returned by the block.

(0..9).filter_map {|i| i * 2 if i.even? }   #=> [0, 4, 8, 12, 16]
{foo: 0, bar: 1, baz: 2}.filter_map {|key, value| key if value.even? }  #=> [:foo, :baz]

For the example in the question: (1..4).filter_map { |x| x + 1 unless x == 3 }.

See this post for comparison with alternative methods, including benchmarks.

8
On

I would simply call .compact on the resultant array, which removes any instances of nil in an array. If you'd like it to modify the existing array (no reason not to), use .compact!:

(1..4).collect do |x|
  next if x == 3
  x
end.compact!
1
On

i would suggest to use:

(1..4).to_a.delete_if {|x| x == 3}

instead of the collect + next statement.

8
On

just a suggestion, why don't you do it this way:

result = []
(1..4).each do |x|
  next if x == 3
  result << x
end
result # => [1, 2, 4]

in that way you saved another iteration to remove nil elements from the array. hope it helps =)

2
On

There is method Enumerable#reject which serves just the purpose:

(1..4).reject{|x| x == 3}.collect{|x| x + 1}

The practice of directly using an output of one method as an input of another is called method chaining and is very common in Ruby.

BTW, map (or collect) is used for direct mapping of input enumerable to the output one. If you need to output different number of elements, chances are that you need another method of Enumerable.

Edit: If you are bothered by the fact that some of the elements are iterated twice, you can use less elegant solution based on inject (or its similar method named each_with_object):

(1..4).each_with_object([]){|x,a| a << x + 1 unless x == 3}
0
On

You could pull the decision-making into a helper method, and use it via Enumerable#reduce:

def potentially_keep(list, i)
  if i === 3
    list
  else
    list.push i
  end
end
# => :potentially_keep

(1..4).reduce([]) { |memo, i| potentially_keep(memo, i) }
# => [1, 2, 4]