替换lambda - 'def'作为表达式? [英] Replacement for lambda - 'def' as an expression?

查看:82
本文介绍了替换lambda - 'def'作为表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读lambda怎么样?在Python 3000中消失(或者至少是

,这就是声明的意图),虽然我同意最合理的
部分的推理,但是同时我很难过看到匿名函数的概念

go - 部分是因为我一直使用它们。


当然,人们总是可以创建一个命名函数。但是有很多

的案例,例如多方法/泛型和其他场景,其中

函数被视为数据,其中你有很多函数

并且为每个人提供一个名称可能很乏味。


例如,我目前的爱好项目正在实施模式匹配

类似于Python中的Prolog。我正在制作的调度员允许你

创建重载一个函数的版本采用不同的模式

作为它们的输入参数,这样Simplify((add,x,y))调用一个比简化更简单的方法((log, ,x)) - 换句话说,执行哪些代码的

选择是基于传递给它的元组

的结构。但是,为了使其能够工作,我需要能够将一段Python代码分配给特定的模式,并且需要发明一个命名函数的
每个模式都是负担:)


无论如何,这里有一个例子,那就是'def''如何被使用:


add = def(a,b):

返回a + b


缺少函数名称表示这是一个匿名函数。

def关键字使用与以往相同的语法定义函数 -

参数在括号中,并且未被评估;结肠标记

套房的开头。


实际上,它看起来很像现有的lambda,有几个

差异:


1)它使用熟悉的def关键字,每个Python初学者

理解,而不是有点不熟悉的lambda

2)参数括在括号中,而不是一个裸元组

后跟一个冒号,再次重申与def的正常

使用的相似性。

3)这些陈述是真实的套件,而不是一个伪套件 - 他们可以

由多行语句组成。


像所有最后一个参数都是套件的语句一样,你可以把它放到/>
单行上的函数体:


add = def(a,b):返回a + b


(如果这是Perl,你也可以省略return,因为在Perl中,

最后在函数体中计算的表达式是返回的,如果

没有明确的退货声明。)


如果将匿名函数作为参数传递,那么最常见的情况是什么?这很棘手,因为你不能在表达式中嵌入一个套件

。或者你可以吗?


最强大的选择是利用你可以在括号内进行换行的事实。所以def关键字

会告诉解析器重新启动正常的缩进计算,

会在遇到无法匹配的大括号或者paren时终止

遇到:


a =地图(

(def(item):

item = do_some_calculation(item)

return项目

),列表)


当然,单行版本看起来更漂亮:


a = map((def(item):return item * item),list)


如果我们切换参数的顺序,它看起来更好,

因为你现在可以使用封闭函数调用的最后一个paren来终止def套件。


a = map(list,def(item):返回项目*项目)


不幸的是,我没有其他好的方法可以在没有引入的情况下发出语句块的b / b
结尾信号一些激进的新的

语言结构。


(此外,如果是一个表达式对于''收益'来说已经足够了,为什么

不应该获得同样的特权? :)

解决方案

Hall ?? chen!


" talin at acm dot有机" < 6 ***** @ gmail.com>写道:

[...]

无论如何,这里有一个例子,那就是'def''如何被使用:< br =>
add = def(a,b):
返回a + b




我真的不是功能专家编程,所以我想知道

" add = def"之间的区别是什么? (假设它有效)

和def add?


Tsch ??,

Torsten。


-

Torsten Bronger,aquisgrana,europa vetus ICQ 264-296-646


在星期二, 2005年9月6日12:19:21 +0200

Torsten Bronger写道:

" talin at acm dot org" < 6 ***** @ gmail.com>写道:

无论如何,这里有一个例子,那就是'def''如何被使用:

add = def(a,b):
返回a + b



我真的不是函数式编程方面的专家,所以我想知道add = def之间的区别是什么? ; (假设它有效)
和def add?




在前一种情况下,可以写一下


self.add [0] = def(a,b)

#等


-

jk <在

talin启发我们:

我很难过看到匿名函数的概念。去


同样在这里。我认为这是一个美丽的概念,而且非常强大。它还允许在名称为
的情况下创建动态函数。

如何将匿名函数作为参数传递,这是最常见的情况?




我不是那么喜欢。语法太乱了。只需


funcref = def(args):

...


语法就足够了。


Sybren

-

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

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


I''ve been reading about how "lambda" is going away in Python 3000 (or
at least, that''s the stated intent), and while I agree for the most
part with the reasoning, at the same time I''d be sad to see the notion
of "anonymous functions" go - partly because I use them all the time.

Of course, one can always create a named function. But there are a lot
of cases, such as multimethods / generics and other scenarios where
functions are treated as data, where you have a whole lot of functions
and it can be tedious to come up with a name for each one.

For example, my current hobby project is implementing pattern matching
similar to Prolog in Python. The dispatcher I am making allows you to
create "overloaded" versions of a function that take different patterns
as their input arguments, so that Simplify( (add, x, y) ) calls a
different method than Simplify( (log, x) ) -- in other words, the
choice of which code is executed is based on the structure of the tuple
that is passed into it. However, in order for this to work, I need to
be able to assign a block of Python code to a particular pattern, and
having to invent a named function for each pattern is a burden :)

Anyway, here''s an example, then, of how ''def'' could be used:

add = def( a, b ):
return a + b

The lack of a function name signals that this is an anonymous function.
The def keyword defines the function using the same syntax as always -
the arguments are in parentheses, and are unevaluated; The colon marks
the beginning of a suite.

In fact, it looks a lot like the existing lambda, with a couple of
differences:

1) It uses the familiar "def" keyword, which every Python beginner
understands, instead of the somewhat unfamiliar "lambda"
2) The arguments are enclosed in parentheses, instead of a bare tuple
followed by a colon, again reiterating the similarity to the normal
usage of "def".
3) The statements are a real suite instead of a pseudo-suite - they can
consist of multiple lines of statements.

Like all statements whose last argument is a suite, you can put the
body of the function on a single line:

add = def( a, b ): return a + b

(If this were Perl, you could also omit the "return", since in Perl the
last evaluated expression in the function body is what gets returned if
there''s no explicit return statement.)

What about passing an anonymous function as an argument, which is the
most common case? This gets tricky, because you can''t embed a suite
inside of an expression. Or can you?

The most powerful option would be to leverage the fact that you can
already do line breaks inside of parentheses. So the "def" keyword
would tell the parser to restart the normal indentation calculations,
which would terminate whenever an unmatched brace or paren was
encountered:

a = map(
(def( item ):
item = do_some_calculation( item )
return item
), list )

The one-liner version looks a lot prettier of course:

a = map( (def( item ): return item * item), list )

And it looks even nicer if we switch the order of the arguments around,
since you can now use the final paren of the enclosing function call to
terminate the def suite.

a = map( list, def( item ): return item * item )

Unfortunately, there''s no other good way I can think of to signal the
end of the block of statements without introducing some radical new
language construct.

(Besides, if being an expression is good enough for ''yield'', why
shouldn''t def get the same privilege? :)

解决方案

Hall??chen!

"talin at acm dot org" <vi*****@gmail.com> writes:

[...]

Anyway, here''s an example, then, of how ''def'' could be used:

add = def( a, b ):
return a + b



I''m really not an expert in functional programming, so I wonder
what''s the difference between "add = def" (assumed that it worked)
and "def add"?

Tsch??,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus ICQ 264-296-646


On Tue, 06 Sep 2005 12:19:21 +0200
Torsten Bronger wrote:

"talin at acm dot org" <vi*****@gmail.com> writes:

Anyway, here''s an example, then, of how ''def'' could be used:

add = def( a, b ):
return a + b



I''m really not an expert in functional programming, so I wonder
what''s the difference between "add = def" (assumed that it worked)
and "def add"?



In the former case one could write

self.add[0] = def(a, b)
# etc.

--
jk


talin at acm dot org enlightened us with:

I''d be sad to see the notion of "anonymous functions" go
Same here. I think it''s a beautyful concept, and very powerful. It
also allows for dynamic function creation in cases where a name would
not be available.
What about passing an anonymous function as an argument, which is
the most common case?



I don''t really like that. The syntax is way too messy. Just the

funcref = def(args):
...

syntax would suffice for me.

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 - 'def'作为表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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