什么是lambda,什么是示例实现? [英] What is a lambda and what is an example implementation?

查看:82
本文介绍了什么是lambda,什么是示例实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程还很陌生,在阅读大量lambda概念的同时,我不断提出建议,但是我很难理解它的实际含义以及如何实现它,这将使我的编程生活变得如此美好.好多了.那么首先,什么是lambda,其次如何实现?

I am fairly new to programming and while doing a lot of reading this concept of a lambda keeps coming up but I'm having a hard time putting my finger on what it actually is and how implementing it will make my programming life so much better. So first, what is a lambda and second how might it be implemented?

感谢所有发布者.正如评论中提到的那样,这是重复的,但是这里有很多很棒的答案,我想将它们保存在社区中,因此我将其转变为社区帖子.这是另一个问题的链接:

Thanks to all who posted. As has been mentioned in the comments, this is a duplicate, but there are so many great answers here I want to preserve them for the community so I'm turning it into a community post. Here is the link to the other question:

什么是lambda(函数)?

推荐答案

Lambda很难捉住,但是一旦您对它们进行了描绘,就无法理解为什么以前没有得到它.

Lambda are hard to catch, but once you have pictured them, you cannot understand why you didn't get it before.

Lambda是普通函数,唯一的区别是您没有给它们命名.

Lambda are ordinary functions, the only difference is that you don't give them a name.

要了解这一点,您必须首先知道在创建函数时,代码会存储在内存中计算机唯一知道的地址上.

To understand that, you must know first that when you create a function, the code is stored in memory at an address only knowned by the computer.

因此,当您执行类似操作时:

So when you do something like that :

function Foo ()
{
 /* your code here */
}

您真正要做的是将名称"Foo"绑定到内存中的代码地址.

What you really do is binding the name "Foo" to the adress of the code in memory.

现在,还有另一种访问地址的方式:引用(还有指针,但是让我们跳过这个讨厌的家伙)

Now, there is an other way to access an address : references (and pointers, but let's skip this nasty guys)

好吧,lambda函数是一个没有名称的函数,因此只能使用其引用进行访问.

Well, a lambda function is a function that have no name, so it can be only access with its reference.

创建lambda函数时,通常只计划使用一次.

When you create a lambda function, you usually plan to use it only once.

分步过程通常是:

  1. 创建函数
  2. 获取参考
  3. 将引用传递到将要使用的地方

最后,引用丢失了,因此该函数自动销毁了.

Finally, the reference is lost, and so the function is destroyed automatically.

典型的用例是回调函数.您可以在一行中声明,创建和传递该函数,因此非常方便.

The typical use case is a callback function. You declare, create and pass the function in one row, so it's handy.

在Python中,您可以在列表推导中使用lambda:

In Python, you can use lambda in list comprehensions :

/* create a list of functions */
function_list = [(lambda x : number_to_add + x) for number_to_add in range(0, 10) ]

在Javascript中,通常将一个函数传递给其他函数.带有JQuery的示例:

In Javascript, you usually pass a function to others functions. Example with JQuery :

 $("img").each(

 /* here we pass a function without any name to the "each()" method   */

 function(i){  his.src = "test"   i   ".jpg"; }

 );

你最好知道的事情

  • 某些语言(例如Javascript或Lisp)大量使用 lambdas.可能是出于文化原因,但是功能性编程范例往往会导致lambda-mania.

    The things you'd better know

    • Some languages, like Javascript or Lisp, make a massive use of lambdas. It can be for cultural reason, but the functional programming paradigm tends to lead to lambda-mania.

      长的lambda使代码难以阅读.这就是为什么某些语言会限制lambda的可能性,例如Python不允许在其中使用"if"语句.

      Long lambdas make the code hard to read. That's why some languages restrain the possibilities of lambdas, such as Python that does not allow "if" statement in them.

      Lambda只是正常功能.无论您使用哪个函数,都可以改用普通函数.只是编码风格的问题.

      Lambdas are just normal functions. Where ever you use one, you can use an ordinary function instead. It's just a matter of coding style.

      这篇关于什么是lambda,什么是示例实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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