Scala 中的 for 表达式与 foreach
我正在通过 Scala 编程,尽管我很想从 Python 的角度看待事物,我不想编写“Python in Scala”。
我不太确定就控制流而言该怎么做:在 Python 中,我们使用 for x in some_iterable
到死,而且我们喜欢它。 Scala 中存在一个非常相似的构造,Odersky 将其称为 for 表达式,可能是为了将其与 Java for 循环区分开来。另外,Scala 对于可迭代数据类型有一个 foreach
属性(我猜它是一个属性,我对 Scala 的了解不够,无法正确命名它)。不过,似乎我不能使用 foreach 做更多事情,而只是为容器中的每一项调用一个函数。
这给我留下了几个问题。首先,Scala 中的 for 表达式是重要/频繁使用的结构,就像在 Python 中一样;其次,我什么时候应该使用 foreach
而不是 for 表达式(除了在每个函数上调用函数的明显情况之外)容器中的物品)?
我希望我没有太含糊或过于宽泛,但我只是想了解 Scala 中的一些设计/语言基础知识(到目前为止看起来很酷)。
I'm working my way through Programming in Scala, and though I'm tempted to look at things from the perspective of Python, I don't want to program "Python in Scala."
I'm not quite sure what to do as far as control flow goes: in Python, we use for x in some_iterable
to death, and we love it. A very similar construct exists in Scala which Odersky calls a for expression, probably to distinguish it from the Java for loop. Also, Scala has a foreach
attribute (I guess it would be an attribute, I don't know enough about Scala to name it correctly) for iterable data types. It doesn't seem like I can use foreach
to do much more than call one function for each item in the container, though.
This leaves me with a few questions. First, are for expressions important/heavily used constructs in Scala like they are in Python, and second, when should I use foreach
instead of a for expression (other than the obvious case of calling a function on each item of a container)?
I hope I'm not being terribly ambiguous or overbroad, but I'm just trying to grok some of the design/language fundamentals in Scala (which seems very cool so far).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
python 在列表推导式和生成器表达式中使用
for
。这些与 scalafor
表达式非常相似:这是 python
这是 scala
每个构造都可以采用一个数字生成器/迭代器,应用过滤器表达式并生成组合表达式。在Python中,
(expr for v1 in gen1 if expr1 for v2 in gen2 if expr2)
大致相当于:在scala中
for (v1 <- gen1 if expr1; v2 <- gen2 if expr2) yield expr
大致相当于:如果您喜欢 python
for x in xs
语法,您可能会喜欢 scalafor
表达式。Scala 有一些额外的语法和翻译变化。语法方面的
for
可以与大括号一起使用,以便您可以将语句放在单独的行上。您还可以执行值分配。而且
v1 <- gen1
确实执行了匹配case v1 =>第 1 代
。如果没有匹配,则迭代中将忽略这些元素。我认为
for
在该语言中占有重要地位。我可以从你正在阅读的书中有一整章(23)来介绍它!python uses
for
in list comprehensions and generator expressions. Those are very similar to the scalafor
expression:this is python
this is scala
Each construct can take a number of generators/iterators, apply filters expressions and yield a combined expression. In python the
(expr for v1 in gen1 if expr1 for v2 in gen2 if expr2)
is roughly equivalent to:In scala
for (v1 <- gen1 if expr1; v2 <- gen2 if expr2) yield expr
is roughly equivalent to:If you love the python
for x in xs
syntax, you'll likely love the scalafor
expression.Scala has some additional syntax and translation twists. Syntax wise
for
can be used with braces so that you can put statements on separate lines. You can also perform value assignments.Also
v1 <- gen1
really performs a matchcase v1 => gen1
. If there is no match those elements are ignored from the iteration.I think
for
has an important place in the language. I can tell from the fact there is a whole chapter (23) about it in the book you're reading!是的,Scala 推导式(众所周知)被大量使用,但它们实际上只是特定方法组合的语法糖,许多人更喜欢直接调用这些方法而不是使用语法糖。
为了更好地理解 Scala 的理解,请参阅此问题。特别是,您会看到
for (x <- xs) f(x)
与xs.foreach(x => f(x))
相同代码>.现在,您提到您似乎不太使用
foreach
方法,但我要指出的是,几乎所有 Scala 集合的方法都是(或可以)仅使用实现的foreach
。请参阅Traversable
的文档 - 它的所有方法都可以仅使用foreach
来实现。请注意,Scala 的
yield
与 Python 的yield
没有相似之处 - 您可以查找这个问题也是如此。Yes, Scala for comprehensions (as they are commonly known) are used a lot, but they are really just syntactic sugar for a particular combination of methods, and many prefer to call these methods directly instead of using the syntactic sugar.
To better understand Scala for comprehensions, please refer to this question. In particular, you'll see that
for (x <- xs) f(x)
is the same thing asxs.foreach(x => f(x))
.Now, you mention that you don't seem much use with
foreach
method, but I'll point out that almost all of the methods of Scala collections are (or can be) implemented with justforeach
. See the documentation forTraversable
-- all of its methods can be implemented with onlyforeach
.Note that Scala's
yield
bears no resemblance to Python'syield
-- you can look up that question too.凭借对嵌套迭代、过滤器和转换的支持,我认为 Scala 的
for
是该语言的强项之一,并且非常核心。与使用foreach
、map
和filter
相比,我更倾向于使用它。With its support for nested iteration, filters, and transformation, I'd say Scala's
for
is one of the strong points of the language and very central. I tend to favor it over usingforeach
,map
andfilter
.foreach 是函数式风格,而 for 是命令式风格。如果您曾经使用过 lisp 或方案,那么您已经熟悉函数式编程。如果你还没有,那么一开始可能会有点混乱。我要做的第一件事是阅读闭包语法,这些语法是传递给 foreach 之类的匿名函数。一旦你明白了,一切都会变得更有意义。
The foreach is a functional style while the for is an imperative style. If you've ever done any lisp or scheme, you're already familiar with functional programming. If you haven't then it might be a bit confusing at first. The first thing I would do is read up on the closure syntax which are anonymous functions you pass into things like foreach. Once you understand that it will all make more sense.
您的问题主要通过以下内容得到解答:
Scala 的 For Compressives
Scala Yield
总结一下:它很大程度上是风格上的。就我个人而言,我喜欢函数式方法,但更喜欢处理嵌套循环时推导式的简洁性。
Your questions are largely answered by the following:
Scala's For Comprehensions
Scala Yield
To summaraize: It's largely stylistic. Personally, I favor the functional methodology, but prefer the succinctness of comprehensions when dealing with nested loops.