Scala中的for/yield详解
来源:互联网 发布:白垩纪往事 知乎 编辑:程序博客网 时间:2024/06/04 17:48
For each iteration of your for loop, yield generates a value which will be remembered. It's like the for loop has a buffer you can't see, and for each iteration of your for loop, another item is added to that buffer. When your for loop finishes running, it will return this collection of all the yielded values. The type of the collection that is returned is the same type that you were iterating over, so a Map yields a Map, a List yields a List, and so on.
Also, note that the initial collection is not changed; the for/yield construct creates a new collection according to the algorithm you specify.
上面那段话的意义就是,for 循环中的 yield 会把当前的元素记下来,保存在集合中,循环结束后将返回该集合。Scala 中 for 循环是有返回值的。如果被循环的是 Map,返回的就是 Map,被循环的是 List,返回的就是 List,以此类推。
基于上面的信息,来看几个例子:
scala>
for
(i <-
1
to
5
)
yield
i
res
10
:
scala.collection.immutable.IndexedSeq[Int]
=
Vector(
1
,
2
,
3
,
4
,
5
)
前面的例子都不算什么,还只是个开始. 接下来, 对我们初始集合的每个元素做一次乘以2:
scala>
for
(i <-
1
to
5
)
yield
i *
2
res
11
:
scala.collection.immutable.IndexedSeq[Int]
=
Vector(
2
,
4
,
6
,
8
,
10
)
这里是 for/yield 循环的求模操作:
scala>
for
(i <-
1
to
5
)
yield
i
%
2
res
12
:
scala.collection.immutable.IndexedSeq[Int]
=
Vector(
1
,
0
,
1
,
0
,
1
)
Scala 数组上的 for 循环 yield 的例子
之前提到过 for 循环 yield 会构造并返回与给定集合相同类型的集合. 为此, 我们来看看以下用 Scala 数组上的例子. 注意把 yield(我们可以把 yield 用作一个动词) 出来的集合类型与前面的几个例子对比:
scala>
val
a
=
Array(
1
,
2
,
3
,
4
,
5
)
a
:
Array[Int]
=
Array(
1
,
2
,
3
,
4
,
5
)
scala>
for
(e <- a)
yield
e
res
5
:
Array[Int]
=
Array(
1
,
2
,
3
,
4
,
5
)
scala>
for
(e <- a)
yield
e *
2
res
6
:
Array[Int]
=
Array(
2
,
4
,
6
,
8
,
10
)
scala>
for
(e <- a)
yield
e
%
2
res
7
:
Array[Int]
=
Array(
1
,
0
,
1
,
0
,
1
)
正如你所见, 例子中被 yield 的是 Array[Int], 而更早的例子中返回的类型是 IndexedSeq[Int].
for 循环, yield, 和守卫( guards) (for loop 'if' conditions)
守卫就是这里的if条件语句。
假如你熟悉了 Scala 复杂的语法, 你就会知道可以在 for 循环结构中加上 'if' 表达式. 它们作为测试用,通常被认为是一个守卫,你可以把它们与 yield 语法联合起来用。参见::
scala>
val
a
=
Array(
1
,
2
,
3
,
4
,
5
)
a
:
Array[Int]
=
Array(
1
,
2
,
3
,
4
,
5
)
scala>
for
(e <- a
if
e >
2
)
yield
e
res
1
:
Array[Int]
=
Array(
3
,
4
,
5
)
如上, 加上了 "if e > 2" 作为守卫条件用以限制得到了只包含了三个元素的数组.
Scala for 循环和 yield 的例子 - 总结
如果你熟悉 Scala 的 loop 结构, 就会知道在 for 后的圆括号中还可以许更多的事情. 你可以加入 "if" 表达式,或别的语句, 比如下面的例子,可以组合多个 if 语句:
def
scalaFiles
=
for
{
file <- filesHere
if
file.isFile
if
file.getName.endsWith(
".scala"
)
}
yield
file
yield 关键字的简短总结:
- 针对每一次 for 循环的迭代, yield 会产生一个值,被循环记录下来 (内部实现上,像是一个缓冲区).
- 当循环结束后, 会返回所有 yield 的值组成的集合.
- 返回集合的类型与被遍历的集合类型是一致的.
但是for/yield所做的事情可以有更简单的实现方式,那就是map()方法。
- Scala中的for/yield详解
- Scala中的yield用法
- Scala 的 yield 例子 (for 循环和 yield 的例子)
- Scala 的 yield 例子 (for 循环和 yield 的例子)
- Scala 的 yield 例子 (for 循环和 yield 的例子)
- Scala 的 yield 例子 (for 循环和 yield 的例子)
- scala里面的for...yield循环
- scala for循环 yield 用法 自我理解
- Scala:使用 for-yield 表达式打印乘法表
- Scala开篇-for循环高级应用之yield
- scala中的json详解
- Scala中的特质详解
- scala中的match详解
- Scala中的for推导式
- Scala 的 yield
- Scala yield unit 测试
- Scala细节-yield格式
- Scala yield 关键字
- Eclipse Svn 安装
- 邮件系统
- spring注解事物,不生效,不回滚,正确配置
- 微信小程序的百度地图获取地理位置 —— 微信小程序教程系列(15)
- 叉积(向量之间的计算)
- Scala中的for/yield详解
- 光学ocr识别软件(Nuance OmniPage Pro)下载 v18.1中文免费版
- 为什么375×667是移动端原型的最佳分辨率
- ubuntu16.04中永久修改 USB 设备权限
- 遍历 map 获取相同 key 值的和
- 动态规划相关概念
- BZOJ 1798: [Ahoi2009]Seq 维护序列seq 线段树题解
- Truncated incorrect DOUBLE VALUE:
- nvidia-sim 和roc-smi