在何时何地使用Lambda? [英] Where and when to use Lambda?

查看:57
本文介绍了在何时何地使用Lambda?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解为什么我们真的需要使用ruby的lambda或proc(或与此相关的任何其他语言)?

I am trying to understand why do we really need lambda or proc in ruby (or any other language for that matter)?

#method
def add a,b
  c = a+b
end

#using proc
def add_proc a,b
  f = Proc.new {|x,y| x + y }
  f.call a,b
end

#using lambda function
def add_lambda a,b
  f = lambda {|x,y| x + y}
  f.call a,b
end

puts add 1,1
puts add_proc 1,2
puts add_lambda 1,3

我可以使用以下方法进行简单加法:1.正常函数def,2.使用proc和3.使用lambda.

I can do a simple addition using: 1. normal function def, 2. using proc and 3. using lambda.

但是为什么在现实世界中以及在何处使用lambda?任何无法使用函数而应使用lambda的示例.

But why and where use lambda in the real world? Any examples where functions cannot be used and lambda should be used.

推荐答案

是的,您不需要 匿名函数(或lambda或任何您想调用的函数).但是有很多事情是您不需要的.您不需要类,只需将所有实例变量传递给普通函数即可.然后

It's true, you don't need anonymous functions (or lambdas, or whatever you want to call them). But there are a lot of things you don't need. You don't need classes—just pass all the instance variables around to ordinary functions. Then

class Foo
  attr_accessor :bar, :baz
  def frob(x)
    bar = baz*x
  end
end

将成为

def new_Foo(bar,baz)
  [bar,baz]
end

def bar(foo)
  foo[0]
end
# Other attribute accessors stripped for brevity's sake

def frob(foo,x)
  foo[0] = foo[1]*x
end

类似地,除了loop...endifbreak之外,您不需要任何循环.我可以继续下去. 1 但是您用Ruby中的类进行编程.您希望能够使用while循环,甚至可以使用array.each { |x| ... },并且希望能够使用unless而不是if not.

Similarly, you don't need any loops except for loop...end with if and break. I could go on and on.1 But you want to program with classes in Ruby. You want to be able to use while loops, or maybe even array.each { |x| ... }, and you want to be able to use unless instead of if not.

就像这些功能一样,匿名功能可以帮助您优雅,简洁,明智地表达事物.能够写some_function(lambda { |x,y| x + f(y) })比必须写要好得多

Just like these features, anonymous functions are there to help you express things elegantly, concisely, and sensibly. Being able to write some_function(lambda { |x,y| x + f(y) }) is much nicer than having to write

def temp(x,y)
  x + f(y)
end
some_function temp

必须中断代码流以编写def fed函数,这很繁琐,但必须清楚地以内联方式编写操作,因此必须给它一个无用的名称.的确,在任何地方您都必须使用lambda,但是在很多地方我更想 使用lambda.

It's much bulkier to have to break off the flow of code to write out a deffed function, which then has to be given a useless name, when it's just as clear to write the operation in-line. It's true that there's nowhere you must use a lambda, but there are lots of places I'd much rather use a lambda.

Ruby使用块解决了很多使用lambda的情况:像eachmapopen这样的所有可以将块作为参数的函数基本上都采用了特殊情况下的匿名函数. array.map { |x| f(x) + g(x) }array.map(&lambda { |x| f(x) + g(x) })相同(其中&只是以与裸块相同的方式使lambda成为特殊"字符).同样,您可以每次都编写一个单独的def fed函数-但是您为什么要想要呢?

Ruby solves a lot of the lambda-using cases with blocks: all the functions like each, map, and open which can take a block as an argument are basically taking a special-cased anonymous function. array.map { |x| f(x) + g(x) } is the same as array.map(&lambda { |x| f(x) + g(x) }) (where the & just makes the lambda "special" in the same way that the bare block is). Again, you could write out a separate deffed function every time—but why would you want to?

除了Ruby之外,支持那种编程风格的语言没有块,但是通常支持更轻量级的lambda语法,例如Haskell的\x -> f x + g x或C#的x => f(x) + g(x); 2 .每当我有一个需要采取某些抽象行为的函数(例如mapeachon_clicked)时,我都会很感激能够传递lambda而不是命名函数的功能,因为更容易.最终,您会停止考虑它们的特殊性,它们就像数组的文字语法一样令人兴奋,而不是empty().append(1).append(2).append(3).语言的另一有用部分.

Languages other than Ruby which support that style of programming don't have blocks, but often support a lighter-weight lambda syntax, such as Haskell's \x -> f x + g x, or C#'s x => f(x) + g(x);2. Any time I have a function which needs to take some abstract behavior, such as map, or each, or on_clicked, I'm going to be thankful for the ability to pass in a lambda instead of a named function, because it's just that much easier. Eventually, you stop thinking of them as somehow special—they're about as exciting as literal syntax for arrays instead of empty().append(1).append(2).append(3). Just another useful part of the language.

1:在简并的情况下,您实际上只需要八种说明:+-<>[].,. <>沿数组移动一个假想的指针"; +-递增和递减当前单元格中的整数; []执行非零循环;和.,进行输入和输出.实际上,您实际上只需要一条指令,例如subleq a b c(减去ab跳转到c(如果结果小于或等于零).

1: In the degenerate case, you really only need eight instructions: +-<>[].,. <> move an imaginary "pointer" along an array; +- increment and decrement the integer in the current cell; [] perform a loop-while-non-zero; and ., do input and output. In fact, you really only need just one instruction, such as subleq a b c (subtract a from b and jump to c if the result is less than or equal to zero).

2 :我从未真正使用过C#,因此,如果该语法错误,请随时进行纠正.

2: I've never actually used C#, so if that syntax is wrong, feel free to correct it.

这篇关于在何时何地使用Lambda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆