多行lambda提案。 [英] Multi-line lambda proposal.

查看:78
本文介绍了多行lambda提案。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读最近交叉发布的火焰战争,并阅读了Guido的

文章,他假设在

表达式中嵌入了多行lambda是一个无法解决的难题。


所以在过去的15分钟里,我将自己应用于这个问题,然后用这个为你提出的这个现成的建议出现了
人。也许这个想法之前已经提出了

,我不知道。


我见过的解决方案都假设lambda必须完全

在表达式中内联:表达式被

lambda中断,然后完全指定(参数和正文)和

表达式以某种方式继续(这就是语法问题发生的地方,产生了非类似于Python的反复无常)。


但是假设表达式和多行lambda的身体是否重新订购了?b $ b?也就是说,表达式是正常写入的,并且其中的b / b $ m mmbmbda表达式用作/ markers /表示主体

材料。这导致了类似Python的解决方案。


假设lambda()没有冒号发生


a = lambda(x,y), lambda(s,t),lambda(u,w):u + w

statement1

statement2

lambda:

声明3

声明4

前两个lambdas后面没有冒号。这意味着

他们有多行身体遵循这个陈述,并且必须

和lambda一样多的身体。第三个lambda是一个常规的

单表达式lambda,完全写在那里。


这些正文由后面的陈述组成。如果一个身体只有一个b
,它就是缩进的材料。如果表达式中有两个或更多

lambdas,则需要其他正文,由lambda:语句引入

,与

表达式,其中包含lambda标记。


当然,实体在范围内有各自的lambda参数。

所以statement1和statement2有访问x和y,以及statement3和

statement4可以访问s和t。


问题解决了,没有Python的重复性。


你可以在表达式中嵌入缩进材料的方式不是实际嵌入它的



如果你想完全嵌入它anally retentive,你可以要求

表达式有一个lambda体后必须用

冒号终止:


a = lambda(x,y),lambda(s,t),lambda(u,w):u + w:

statement1

statement2

lambda:

statement3

statement4


如果我们取出最后两个lambdas,这将减少到:


a = lambda(x,y):

statement1

statement2


这里,冒号终止包含lambda的语句。它不是引入单表达式lambda体的冒号。例如:


a = lambda(x,y):x + y


a = lambda(x,y):

返回x + y


这两个因以下内容消除歧义。你得到了图片。


更多例子:在函数调用参数中定义的lambda


a = foo(lambda(x,y)):

返回x + y


令人困惑?如果您正确阅读,请勿使用。 一个lambda函数是

用参数x,y构造并传递给foo,结果是

分配给a。哦,顺便说一下,

lambda的正文是:返回x + y。"

I''ve been reading the recent cross-posted flamewar, and read Guido''s
article where he posits that embedding multi-line lambdas in
expressions is an unsolvable puzzle.

So for the last 15 minutes I applied myself to this problem and come up
with this off-the-wall proposal for you people. Perhaps this idea has
been proposed before, I don''t know.

The solutions I have seen all assume that the lambda must be completely
inlined within the expression: the expression is interrupted by the
lambda, which is then completely specified (arguments and body) and the
expression somehow continues (and this is where syntactic problems
occur, giving rise to un-Python-like repugnancies).

But suppose that the expression and the multi-line lambda body are
reordered? That is to say, the expression is written normally, and the
mlambda expressions in it serve as /markers/ indicating that body
material follows. This results in the most Python-like solution.

Suppose lambda() occurs without a colon

a = lambda(x, y), lambda(s, t), lambda(u, w): u + w
statement1
statement2
lambda:
statement3
statement4

The first two lambdas do not have a colon after them. This means that
they have multi-line bodies which follow this statement, and there must
be as many bodies as there are lambdas. The third lambda is a regular
one-expression lambda, entirely written right there.

The bodies are made up of the statements which follow. If there is only
one body, it''s simply the indented material. If there are two or more
lambdas in the expression, additional bodies are required, introduced
by lambda: statements, which are at the same indentation level as the
expression which contains the lambda markers.

Of course, the bodies have their respective lambda parameters in scope.
So statement1 and statement2 have access to x and y, and statement3 and
statement4 have access to s and t.

Problem solved, with no Python repugnancies.

The way you can embed indented material in an expression is by not
physically embedding it.

If you want to be completely anally retentive, you can require that the
expression which has lambda bodies after it has to be terminated by a
colon:

a = lambda(x, y), lambda(s, t), lambda(u, w): u + w:
statement1
statement2
lambda:
statement3
statement4

If we take out the last two lambdas, this reduces to:

a = lambda(x, y):
statement1
statement2

Here, the colon terminates the lambda-containing statement. It is not
the colon which introduces the body of a one-expression lambda. E.g.:

a = lambda(x, y): x + y

a = lambda(x, y):
return x + y

The two are disambiguated by what follows. You get the picture.

More examples: lambda defined in a function call argument

a = foo(lambda (x, y)):
return x + y

Confusing? Not if you read it properly. "A lambda function is
constructed with arguments x, y and passed to foo, and the result is
assigned to a. Oh, and by the way, the body of the
lambda is: return x + y."

推荐答案

Kaz Kylheku写道:
Kaz Kylheku wrote:
我一直在阅读最近交叉发布的火焰战,并阅读了Guido'的文章,他认为该文章中嵌入了多行lambda
表达是一个无法解决的难题。

所以在过去的15分钟里,我将自己应用于这个问题,并为你们提供这个现成的建议。也许这个想法之前已经提出过了,我不知道。
I''ve been reading the recent cross-posted flamewar, and read Guido''s
article where he posits that embedding multi-line lambdas in
expressions is an unsolvable puzzle.

So for the last 15 minutes I applied myself to this problem and come up
with this off-the-wall proposal for you people. Perhaps this idea has
been proposed before, I don''t know.




好​​吧,问问自己,复杂性是多少,什么是

福利。你正在解决一个导致很少痛苦的问题,并引入一种可能激发成千上万行不可维护代码的机制。基本上,

规则说,如果它不是一个简单的表达式,请为它选择一个

名称。这不是一个坏主意。


--Scott David Daniels
sc *********** @ acm.org


Kaz Kylheku写道:
Kaz Kylheku wrote:
但是假设表达式和多行lambda体重新排序?也就是说,表达式是正常写入的,并且其中的mlambda表达式用作/ markers /表示主体
材料。这导致了类似Python的解决方案。
But suppose that the expression and the multi-line lambda body are
reordered? That is to say, the expression is written normally, and the
mlambda expressions in it serve as /markers/ indicating that body
material follows. This results in the most Python-like solution.




我认为你的提议很好,但是如果我们可以更容易理解

给lambda命名,这样就可以很容易地在源代码中找到它们。它可能看起来像这样:


func(lambda foo):

foo:

print" Foo!"


想想看,也许我们可以把lambdas放在声明之前

他们用来代替它。这可能看起来像这样:b $ b这样:

lambda foo:

print" Foo!"

func(lambda foo)


然后,再次使用现有的

块启动关键字可能会更简单。也许是这样的?


def foo():

print" Foo!"

func(foo)



I think your proposal is good, but it might be more easily understood if
we gave names to the lambdas, so that they could be easily picked out in
the source code. It could look something like this:

func(lambda foo):
foo:
print "Foo!"

Come to think of it, maybe we could put the lambdas before the statement
they''re used in instead of in front of it. That could look something
like this:

lambda foo:
print "Foo!"
func(lambda foo)

Then again, it would probably be simpler to reuse an existing
block-starting keyword for the purpose. Maybe something like this?

def foo():
print "Foo!"
func(foo)


Kaz Kylheku启发我们:
Kaz Kylheku enlightened us with:
我一直在阅读最近交叉发布的火焰战争,并阅读Guido''s
他认为在
表达式中嵌入多行lambda的文章是一个无法解决的难题。
[...]
a = lambda(x,y),lambda(s,t) ,lambda(你,w):你+ w
声明1
声明2
lambda:
声明3
声明4


我认为这是一个非常难看的解决方案。如果你的

lambda中需要多行,请使用内部函数。如果你需要几行单行

lambdas并在多行表达式中使用它们,请将它们分配给一个名称

并代替它使用该名称。 br />
a = lambda(x,y):
返回x + y


以上这个有什么好处?


def a(x,y):

返回x + y

更多示例:函数调用参数中定义的lambda

a = foo(lambda(x,y)):
返回x + y

令人困惑?如果您正确阅读,请勿使用。 一个lambda函数用参数x,y构造并传递给foo,结果被分配给a。哦,顺便说一下,
lambda的主体是:返回x + y。
I''ve been reading the recent cross-posted flamewar, and read Guido''s
article where he posits that embedding multi-line lambdas in
expressions is an unsolvable puzzle.
[...]
a = lambda(x, y), lambda(s, t), lambda(u, w): u + w
statement1
statement2
lambda:
statement3
statement4
I think it''s a very ugly solution. If you need multiple lines in your
lambda, use an inner function. If you need a couple of single-line
lambdas and use them in a multi-line expression, assign them to a name
and use that name in their stead.
a = lambda(x, y):
return x + y
And what''s the advantage of that above this?

def a(x, y):
return x + y
More examples: lambda defined in a function call argument

a = foo(lambda (x, y)):
return x + y

Confusing? Not if you read it properly. "A lambda function is
constructed with arguments x, y and passed to foo, and the result is
assigned to a. Oh, and by the way, the body of the
lambda is: return x + y."




我认为它非常非常丑陋。使用内部函数。他们是比这更清洁的解决方案。


Sybren

-

问题与世界是愚蠢的。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉

安全标签来解决问题呢? br />
Frank Zappa



I think it''s very, very ugly. Use inner functions for that. They are a
much cleaner solution than this.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa


这篇关于多行lambda提案。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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