Python 和 Javascript 中的理解只是非常基本的?
看看 Python 和 Javascript 中的推导式,到目前为止,我还看不到一些我认为在 Haskell 等语言的推导式中最强大的主要功能。
他们允许使用多个发电机之类的东西吗?或者它们只是一个基本的地图过滤器形式?
如果他们不允许使用多个生成器,我觉得他们非常令人失望 - 为什么这些东西被排除在外?
Looking at comprehensions in Python and Javascript, so far I can't see some of the main features that I consider most powerful in comprehensions in languages like Haskell.
Do they allow things like multiple generators? Or are they just a basic map-filter form?
If they don't allow multiple generators, I find them quite disappointing - why have such things been left out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Python 允许多个生成器:
还有限制:
更新:Javascript 的语法类似(使用 的结果Firefox 上的 javascript shell):(
出于某种原因,在 javascript shell 中评估有关上下文的内容需要 eval 间接才能使列表推导式工作。
标签当然不需要这样)
Python allows multiple generators:
And also restrictions:
Update: Javascript's syntax is similar (results from using the javascript shell on firefox):
(For some reason, something about the context stuff is evaluated in in the javascript shell requires the eval indirection to have list comprehensions work. Javascript inside a
<script>
tag doesn't require that, of course)是的,您可以在 Python 列表理解中拥有多个迭代:
Yes, you can have multiple iterables in a Python list comprehension:
还添加一个 if 语句...
Add an if statement as well...
推导式在 Haskell 中非常强大,很大程度上是因为 Haskell 是函数式的,所以它们的存在非常有意义。 Python 不是函数式的,所以它的意义不大。
你可以用 Python 中的推导式来做很多复杂的事情,但它很快就会变得难以阅读,从而违背了整个目的(意味着你应该以其他方式来做)。
然而,正如这里所指出的,Python 确实允许在推导式中使用多个生成器。
Comprehensions is very powerful in Haskell to a large extent because Haskell is functional, so it makes extremely much sense for them to be. Python is not functional so it makes less sense.
You can make a lot of complex things with comprehensions in Python but it quickly becomes hard to read, thereby defeating the whole purpose (meaning you should do it some other way).
However, as pointed out here, python does allow multiple generators in comprehensions.