我有一个地图,改变一个值或设置它为nil。然后我想从列表中删除nil项。这个清单不需要保存。

这是我目前拥有的:

# A simple example function, which returns a value or nil
def transform(n)
  rand > 0.5 ? n * 10 : nil }
end

items.map! { |x| transform(x) } # [1, 2, 3, 4, 5] => [10, nil, 30, 40, nil]
items.reject! { |x| x.nil? } # [10, nil, 30, 40, nil] => [10, 30, 40]

我知道我可以只做一个循环,并有条件地收集另一个数组,像这样:

new_items = []
items.each do |x|
    x = transform(x)
    new_items.append(x) unless x.nil?
end
items = new_items

但它看起来并不是那么地道。有没有一个很好的方法来映射一个函数在一个列表,删除/排除nils,因为你去?


当前回答

你可以使用compact:

[1, nil, 3, nil, nil].compact
=> [1, 3] 

我想提醒人们,如果您获得一个包含nils的数组作为映射块的输出,并且该块试图有条件地返回值,那么您就有了代码气味,需要重新考虑您的逻辑。

例如,如果你正在做这样的事情:

[1,2,3].map{ |i|
  if i % 2 == 0
    i
  end
}
# => [nil, 2, nil]

那就不要。相反,在地图之前,拒绝你不想要的东西或选择你想要的东西:

[1,2,3].select{ |i| i % 2 == 0 }.map{ |i|
  i
}
# => [2]

I consider using compact to clean up a mess as a last-ditch effort to get rid of things we didn't handle correctly, usually because we didn't know what was coming at us. We should always know what sort of data is being thrown around in our program; Unexpected/unknown data is bad. Anytime I see nils in an array I'm working on, I dig into why they exist, and see if I can improve the code generating the array, rather than allow Ruby to waste time and memory generating nils then sifting through the array to remove them later.

'Just my $%0.2f.' % [2.to_f/100]

其他回答

你可以使用compact:

[1, nil, 3, nil, nil].compact
=> [1, 3] 

我想提醒人们,如果您获得一个包含nils的数组作为映射块的输出,并且该块试图有条件地返回值,那么您就有了代码气味,需要重新考虑您的逻辑。

例如,如果你正在做这样的事情:

[1,2,3].map{ |i|
  if i % 2 == 0
    i
  end
}
# => [nil, 2, nil]

那就不要。相反,在地图之前,拒绝你不想要的东西或选择你想要的东西:

[1,2,3].select{ |i| i % 2 == 0 }.map{ |i|
  i
}
# => [2]

I consider using compact to clean up a mess as a last-ditch effort to get rid of things we didn't handle correctly, usually because we didn't know what was coming at us. We should always know what sort of data is being thrown around in our program; Unexpected/unknown data is bad. Anytime I see nils in an array I'm working on, I dig into why they exist, and see if I can improve the code generating the array, rather than allow Ruby to waste time and memory generating nils then sifting through the array to remove them later.

'Just my $%0.2f.' % [2.to_f/100]

Each_with_object可能是这里最简洁的方法:

new_items = items.each_with_object([]) do |x, memo|
    ret = process_x(x)
    memo << ret unless ret.nil?
end

在我看来,在有条件的情况下,each_with_object比inject/reduce更好,因为你不必担心块的返回值。

您可以对结果数组使用#compact方法。

[10, nil, 30, 40, nil].compact => [10, 30, 40]

在你的例子中:

items.map! { |x| process_x url } # [1, 2, 3, 4, 5] => [1, nil, 3, nil, nil]

它看起来不像值发生了变化,只是被替换为nil。如果是这样的话,那么:

items.select{|x| process_x url}

就足够了。

完成它的另一种方法如下所示。在这里,我们使用Enumerable#each_with_object来收集值,并使用object# tap来消除临时变量,否则需要对process_x方法的结果进行nil检查。

items.each_with_object([]) {|x, obj| (process x).tap {|r| obj << r unless r.nil?}}

完整的示例说明:

items = [1,2,3,4,5]
def process x
    rand(10) > 5 ? nil : x
end

items.each_with_object([]) {|x, obj| (process x).tap {|r| obj << r unless r.nil?}}

备选方案:

通过查看调用process_x url的方法,并不清楚在该方法中输入x的目的是什么。如果我假设你将通过传递一些url来处理x的值,并确定哪些xs真的被处理成有效的非nil结果-那么,可能是Enumerabble。group_by比Enumerable#map更好。

h = items.group_by {|x| (process x).nil? ? "Bad" : "Good"}
#=> {"Bad"=>[1, 2], "Good"=>[3, 4, 5]}

h["Good"]
#=> [3,4,5]