这可以用功能风格更简洁地书写 [英] Can this be written more concisely in a functional style

查看:86
本文介绍了这可以用功能风格更简洁地书写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)

def f(xs):
xs中x为


如果测试(x):返回True

返回False


我知道我可以做(2),但它在整个列表中操作并且原始的

可能会中断早出来。我想要(1)的效率,但是(2)的简洁。


2)

在地图中返回True(test,xs)

1)
def f (xs):对于xs中的x:
如果test(x):return True
return False


很明显有一个iternation提前结束的方式。

2)
在地图中返回True(测试,xs)


强烈暗示(foo in list"," map()")整个列表将被重复
。对于阅读代码的人来说,任何其他行为都是出乎意料的。

我知道我可以做(2),但它在整个列表和




我认为,为了使它更简洁,你' '还要寻求

让它变得不那么明显。任何具有循环遍布

整个列表的语义的东西在一个声明中,并不是要帮助人们理解

,一个常见的情况是迭代提前结束。这可能是没有出现的好理由。


如果要隐藏算法,请在辅助函数中执行此操作。然后

你在实际使用它的地方有你的意思,并且

实现算法的显式语义。


-

\愤世嫉俗的人是一个男人,当他闻到鲜花的时候,四处寻找......这是一个棺材。 - Henry L. Mencken |
_o__)|

Ben Finney< http://bignose.squidly.org/>


2003年11月18日11:28:09 +1050,Ben Finney写道:

2003年11月17日16:48:36 -0800,MetalOne写道:

def f(xs):对于xs中的x:
如果test(x):返回True
返回False



如果你想要隐藏算法,在辅助函数中这样做。
然后你就可以在你实际使用它的地方进行思考,并且实现算法的显式语义。




第二次看,你似乎是*把它放在一个辅助函数中,

大概是为了隐藏实现。如果是这样的话,那么这个实现在这里显而易见是好事 - 任何人如果想看看这个函数,就会明白它是如何运作的。


-

\当我真的很无聊时,我喜欢开车到市中心并获得一个|

` \很棒停车位,然后坐在我的车里,算上有多少人会问我是否要离开。 - Steven Wright |
Ben Finney< http://bignose.squidly.org/>


2003年11月17日16: 48:36 -0800, jc*@iteris.com (MetalOne)写道:

1)
def f(xs):对于xs中的x:
如果测试(x):返回True
返回False

我知道我可以做(2),但它在整个列表上运行,原始的
可能会提前爆发。我想要(1)的效率,但是(2)的简洁。

2)
在地图中返回True(test,xs)



那是不太一样的,除非你保证test(x)== bool(test(x))==当

test(x)逻辑上是真的,而且从不否则返回True。 (例如,如果测试是什么

def test(x):返回x?f(范围(5))当你点击1时会给你一个真,但是,map(测试,范围(5)) )

只是数字,并且在那里没有True。


我想用生成器表达式你很快就能写出


def f(xs):在xs中返回True(bool(test(x))for x


我们可以伪造发生器表达式和测试将告诉我们它走了多远,看看......

def test(x ):print x;返回x ==''3''
...

(好吧,确实可以保证一个bool,但其他一些测试可能没有)。

def gx(fun,seq):
... for x in seq:yield bool(fun(x))

... xs =''abc123def456''




def f(xs):在gx中返回True(test,xs)
... f(xs)



a

b

c

1

2
3

真实


问候,

Bengt Richter

1)
def f(xs):
for x in xs:
if test(x): return True
return False

I know that I can do (2), but it operates on the whole list and the original
may break out early. I want the efficiency of (1), but the conciseness of (2).

2)
return True in map(test,xs)

解决方案

On 17 Nov 2003 16:48:36 -0800, MetalOne wrote:

1)
def f(xs):
for x in xs:
if test(x): return True
return False
Makes it obvious that there is a way for the iternation to end early.
2)
return True in map(test,xs)
Strongly implies ("foo in list", "map()") that the entire list will be
iterated. Any other behaviour would be unexpected to the person reading
the code.
I know that I can do (2), but it operates on the whole list and the
original may break out early. I want the efficiency of (1), but the
conciseness of (2).



I think that in seeking to make it more concise, you''re also seeking to
make it less obvious. Anything that has the semantics of "loop over the
whole list" in a single statement, isn''t going to help people understand
that a common case is for the iteration to end early. Which is probably
a good reason for it not to appear.

If you want to hide the algorithm, do so inside a helper function. Then
you have consision in the places where you''re actually using it, and
explicit semantics where the algorithm is implemented.

--
\ "A cynic is a man who, when he smells flowers, looks around for |
`\ a coffin." -- Henry L. Mencken |
_o__) |
Ben Finney <http://bignose.squidly.org/>


On 18 Nov 2003 11:28:09 +1050, Ben Finney wrote:

On 17 Nov 2003 16:48:36 -0800, MetalOne wrote:

def f(xs):
for x in xs:
if test(x): return True
return False



If you want to hide the algorithm, do so inside a helper function.
Then you have consision in the places where you''re actually using it,
and explicit semantics where the algorithm is implemented.



On second look, you appear to *be* putting this in a helper function,
presumably for the purpose of hiding the implementation. If so, it''s a
good thing that the implementation is explicit here -- anyone who goes
looking into this function wants it obvious how it works.

--
\ "When I get real bored, I like to drive downtown and get a |
`\ great parking spot, then sit in my car and count how many |
_o__) people ask me if I''m leaving." -- Steven Wright |
Ben Finney <http://bignose.squidly.org/>


On 17 Nov 2003 16:48:36 -0800, jc*@iteris.com (MetalOne) wrote:

1)
def f(xs):
for x in xs:
if test(x): return True
return False

I know that I can do (2), but it operates on the whole list and the original
may break out early. I want the efficiency of (1), but the conciseness of (2).

2)
return True in map(test,xs)


That''s not quite the same, unless you guarantee that test(x)==bool(test(x))==True when
test(x) is logically true, and never returns True otherwise. (E.g., what if test were
def test(x): return x ? f(range(5)) will give you a True when you hit 1 but, map(test,range(5))
will just be the numbers, and there will be no True in that).

I guess with generator expressions you will soon be able to write

def f(xs): return True in (bool(test(x)) for x in xs)

We can fake the generator expression and a test that will show us how far it went, to see...

def test(x): print x; return x==''3'' ...
(Ok, that does guarantee a bool, but some other test might conceivably not).
def gx(fun, seq): ... for x in seq: yield bool(fun(x))
... xs = ''abc123def456''
and
def f(xs): return True in gx(test, xs) ... f(xs)


a
b
c
1
2
3
True

Regards,
Bengt Richter


这篇关于这可以用功能风格更简洁地书写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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