为什么在这里使用 lambda 而不是两个预定义的方法?

发布于 2024-08-04 12:29:26 字数 748 浏览 2 评论 0原文

def divideset(rows, column, value)
split_function = nil

if value.is_a?(Fixnum) || value.is_a?(Float)
  split_function = lambda{|row| row[column] >= value}
else
  split_function = lambda{|row| row[column] == value}
end

set1 = rows.select{|row| split_function.call(row)}
set2 = rows.reject{|row| split_function.call(row)}
[set1, set2]
end

treepredict 的这段代码中,为什么要使用 lambda?

看来,作者可以不调用 split_function.call(row),而是预定义两种不同的方法来处理 if/else 语句的两个条件 - 一种用于处理 row 的情况[column] >= value 和另一个用于 row[column] == value 的情况

使用 lambda 是否还有其他优势?

def divideset(rows, column, value)
split_function = nil

if value.is_a?(Fixnum) || value.is_a?(Float)
  split_function = lambda{|row| row[column] >= value}
else
  split_function = lambda{|row| row[column] == value}
end

set1 = rows.select{|row| split_function.call(row)}
set2 = rows.reject{|row| split_function.call(row)}
[set1, set2]
end

In this code from treepredict, why use lambdas?

It seems that instead of calling split_function.call(row), the author could have predefined two different methods to handle the two conditions of the if/else statement -- one for the case where row[column] >= value and the other for the case where row[column] == value

Is there any additional advantage here from using the lambda?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恋竹姑娘 2024-08-11 12:29:26

在这种情况下,使用 lambda 没有额外的技术优势。

考虑一下你的建议:

def split_function_default(row, column, value)
  row[column] == value
end

def split_function_number(row, column, value)
  row[column] >= value
end

def divideset(rows, column, value)
  set1 = nil
  set2 = nil
  if value.is_a?(Fixnum) || value.is_a?(Float)
    set1 = rows.select{|row| split_function_number(row, column, value)}
    set2 = rows.reject{|row| split_function_number(row, column, value)}
  elif
    set1 = rows.select{|row| split_function_default(row, column, value)}
    set2 = rows.reject{|row| split_function_default(row, column, value)}
  end
  [set1, set2]
end

哪个更清楚?就我个人而言,我更喜欢 lambda 版本,因为它更简洁。

There is no additional technical advantage to using the lambdas in this case.

Consider your suggestion:

def split_function_default(row, column, value)
  row[column] == value
end

def split_function_number(row, column, value)
  row[column] >= value
end

def divideset(rows, column, value)
  set1 = nil
  set2 = nil
  if value.is_a?(Fixnum) || value.is_a?(Float)
    set1 = rows.select{|row| split_function_number(row, column, value)}
    set2 = rows.reject{|row| split_function_number(row, column, value)}
  elif
    set1 = rows.select{|row| split_function_default(row, column, value)}
    set2 = rows.reject{|row| split_function_default(row, column, value)}
  end
  [set1, set2]
end

Which is more clear? Personally, I prefer the lambda version as it's more succinct.

暮凉 2024-08-11 12:29:26

您可以预定义这两个函数然后使用它们。

但在这种情况下使用 lambda 就很清楚了,因为这些函数确实很简单,而且它们的范围仅限于 divideset 内。如果其他一些函数也需要使用某些功能,那么最好使用预定义函数来遵循 DRY 原则。

You could predefine these 2 function and then use them.

But using lambda in this case is much clear as these are really trival functions and their scope is only limited within divideset. If some other functions would also use the some functionality , it is better to use predefined function to follow the DRY principle.

深爱成瘾 2024-08-11 12:29:26

只是插话,以备记录。

split_function = lambda {|row| do_stuff_with(row) }

// don't do this
rows.select{|row| split_function.call(row)}

// do this
rows.select(&split_function)

Just chiming in, for the record.

split_function = lambda {|row| do_stuff_with(row) }

// don't do this
rows.select{|row| split_function.call(row)}

// do this
rows.select(&split_function)
浅语花开 2024-08-11 12:29:26

从某种意义上说,它只是风格,但一点点帮助

虽然在一种观点中这只是风格,但在另一种观点中,它确实显示了函数式语言以更少的人力完成相同数量的工作的能力。如果我们不关心冗长和笨拙,我们可以用 Java 编写所有内容...

以下是一些浮现在脑海中的相当小的“风格”变化:

def divideset(rows, column, value)
  split_function = value.is_a?(Fixnum) || value.is_a?(Float) ?
      lambda {|row| row[column] >= value} :
      lambda {|row| row[column] == value}

  [ rows.select{|row| split_function.call(row)},
    rows.reject{|row| split_function.call(row)} ]
end

def divideset(rows, column, value)
  split_function =
    if value.is_a?(Fixnum) || value.is_a?(Float) 
      lambda {|row| row[column] >= value} 
    else
      lambda {|row| row[column] == value}
    end

  [ rows.select{|row| split_function.call(row)},
    rows.reject{|row| split_function.call(row)} ]
end

也许我一直在玩 太多的代码高尔夫

In a sense it's just style, but every little bit helps

Although in one view it's just style, in another it does show the capability of a functional language to get the same amount of work done with less peon labor. If we didn't care about verbosity and clunkiness we could just write everything in Java...

Here are some rather minor "style" variations that come to mind:

def divideset(rows, column, value)
  split_function = value.is_a?(Fixnum) || value.is_a?(Float) ?
      lambda {|row| row[column] >= value} :
      lambda {|row| row[column] == value}

  [ rows.select{|row| split_function.call(row)},
    rows.reject{|row| split_function.call(row)} ]
end

def divideset(rows, column, value)
  split_function =
    if value.is_a?(Fixnum) || value.is_a?(Float) 
      lambda {|row| row[column] >= value} 
    else
      lambda {|row| row[column] == value}
    end

  [ rows.select{|row| split_function.call(row)},
    rows.reject{|row| split_function.call(row)} ]
end

Perhaps I've been playing too much code-golf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文