相关疑难解决方法(0)

在Ruby中,是否有一个结合了'select'和'map'的Array方法?

我有一个包含一些字符串值的Ruby数组.我需要:

  1. 找到与某个谓词匹配的所有元素
  2. 通过转换运行匹配元素
  3. 将结果作为数组返回

现在我的解决方案看起来像这样:

def example
  matchingLines = @lines.select{ |line| ... }
  results = matchingLines.map{ |line| ... }
  return results.uniq.sort
end
Run Code Online (Sandbox Code Playgroud)

是否有一个Array或Enumerable方法将select和map组合成一个逻辑语句?

ruby

88
推荐指数
6
解决办法
5万
查看次数

在Haskell,Python和Ruby中列出理解

我已经开始将项目Euler站点作为一种学习Haskell的方法,并改进我的Python和Ruby.我认为Haskell和Python版本都可以,但我确信Ruby必须有一个更简洁的方法.

这不是关于如何使一种语言看起来像另一种语言.

这是问题1:

问:添加1000以下的所有自然数,即3或5的倍数.

哈斯克尔:

sum [ x | x <- [1..999], mod x 3 == 0 || mod x 5 == 0 ]
Run Code Online (Sandbox Code Playgroud)

蟒蛇:

sum ( [ x for x in range(1,1000) if x % 3 == 0 or x % 5 == 0 ] )
Run Code Online (Sandbox Code Playgroud)

红宝石:

(1..999) . map {|x| x if x % 3 == 0 || x % 5 == 0 } . compact . inject(:+)
Run Code Online (Sandbox Code Playgroud)

他们都给出了相同的答案.


好的,所以Python可以成为:

sum ( x for …
Run Code Online (Sandbox Code Playgroud)

ruby python haskell list-comprehension

14
推荐指数
1
解决办法
2797
查看次数

Ruby有类似Python的列表推导吗?

Python有一个很好的功能:

print([j**2 for j in [2, 3, 4, 5]]) # => [4, 9, 16, 25]
Run Code Online (Sandbox Code Playgroud)

在Ruby中,它甚至更简单:

puts [2, 3, 4, 5].map{|j| j**2}
Run Code Online (Sandbox Code Playgroud)

但如果它是关于嵌套循环Python看起来更方便.

在Python中我们可以这样做:

digits = [1, 2, 3]
chars = ['a', 'b', 'c']    
print([str(d)+ch for d in digits for ch in chars if d >= 2 if ch == 'a'])    
# => ['2a', '3a']
Run Code Online (Sandbox Code Playgroud)

Ruby中的等价物是:

digits = [1, 2, 3]
chars = ['a', 'b', 'c']
list = []
digits.each do |d|
    chars.each do |ch|
        list.push d.to_s << ch if …
Run Code Online (Sandbox Code Playgroud)

ruby python programming-languages

10
推荐指数
1
解决办法
5715
查看次数

Erlang vs Ruby列表理解

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

Weather = [{toronto, rain}, {montreal, storms}, {london, fog}, {paris, sun}, {boston, fog}, {vancounver, snow}].                          
FoggyPlaces = [X || {X, fog} <- Weather].
Run Code Online (Sandbox Code Playgroud)

在这种情况下,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}]
Run Code Online (Sandbox Code Playgroud)

我现在最好的是:

weather.collect {|w| w[:city] if w[:weather] == :fog }.compact
Run Code Online (Sandbox Code Playgroud)

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

而更在二郎示例中,cityweather是原子.我甚至不知道如何在Ruby中获得有意义且看起来很好的东西.

ruby erlang list-comprehension

4
推荐指数
1
解决办法
558
查看次数

Ruby 相当于 Python 列表理解

如何在 Ruby 中编写这样的循环?

hashTable = [node(None, -1, None, None, -1) for i in range(0, tableLength)]
Run Code Online (Sandbox Code Playgroud)

我已经尝试过以下操作:

hash_table = [table_length.times do |x|
  x = NODE.new(@key, @val, @next, @prev, @pos)
  x
end]
Run Code Online (Sandbox Code Playgroud)

@key, @val...我在调用该类时传递值的位置,如下所示:

this = NODE.new(nil, -1, nil, nil, -1)
this.read_file("alice.txt")
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

ruby loops list-comprehension

0
推荐指数
1
解决办法
1365
查看次数