我刚开始学习 Erlang,非常喜欢他们的列表理解语法,例如:

Weather = [{toronto, rain}, {montreal, storms}, {london, fog}, {paris, sun}, {boston, fog}, {vancounver, snow}].
FoggyPlaces = [X || {X, fog} <- Weather].

在这种情况下,FoggyPlaces 将评估为“伦敦”和“波士顿”。

在 Ruby 中执行此操作的最佳方法是什么?

例如,像这样的数组(我相信很常见):
 weather = [{city: 'toronto', weather: :rain}, {city: 'montreal', weather: :storms}, {city: 'london', weather: :fog}, {city: 'paris', weather: :sun}, {city: 'boston', weather: :fog}, {city: 'vancounver', weather: :snow}]

到目前为止我得到的最好的是:
weather.collect {|w| w[:city] if w[:weather] == :fog }.compact

但是在这种情况下,我必须调用 compact 来删除 nil 值,并且示例本身不像 Erlang 那样可读。

更重要的是,在 Erlang 示例中, cityweather 都是原子。我什至不知道如何在 Ruby 中获得像这样有意义且看起来不错的东西。

最佳答案

首先,您的数据结构并不相同。与 Erlang 示例等效的 Ruby 数据结构更像是

weather = [[:toronto, :rain], [:montreal, :storms], [:london, :fog],
            [:paris, :sun], [:boston, :fog], [:vancouver, :snow]]

其次,是的,Ruby 没有列表推导式和模式匹配。所以,这个例子可能会更复杂。您的列表理解首先过滤所有有雾的城市,然后投影名称。让我们在 Ruby 中做同样的事情:
weather.select {|_, weather| weather == :fog }.map(&:first)
# => [:london, :boston]

然而,Ruby 以对象为中心,但您使用的是抽象数据类型。使用更面向对象的数据抽象,代码可能看起来更像
weather.select(&:foggy?).map(&:city)

这还不错,是吗?

关于ruby - Erlang 与 Ruby 列表推导式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13378987/

10-12 07:28