scala中 yield

概述
scala语言的for语法很灵活. 除了普通的直接对集合的循环, 以及循环中的判断和值返回. 非常灵活.

for 可以通过yield(生产)返回值, 最终组成for循环的对象类型.for 循环中的 yield 会把当前的元素记下来,保存在集合中,循环结束后将返回该集合。如果被循环的是 Map,返回的就是Map,被循环的是 List,返回的就是List,以此类推。

守卫( guards) (for loop ‘if’ conditions)

可以在 for 循环结构中加上 ‘if’ 表达式, 和yield联合起来用.
普通对集合或迭代循环

scala> for(i<- 1 to 5) println(i)
1
2
3
4
5
scala> for(i<- 1 until 5) println(i)
1
2
3
4

yield返回值

scala> for (i <- 1 to 5) yield i
res0: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5)

scala> val a= for (i <- 1 to 5) yield i
a: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5)

scala> a
res1: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5)

scala> val a= for (i <- 1 until 5) yield i
a: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4)

scala> a
res2: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4)

scala> val a= for (i <- 1 until 5) yield i*2
a: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8)

scala> val a = Array(1, 2, 3, 4, 5)
a: Array[Int] = Array(1, 2, 3, 4, 5)

scala> for ( e <- a) yield e
res3: Array[Int] = Array(1, 2, 3, 4, 5)

循环过滤 if 判断, 并返回值

scala> for ( e <- a if e%2 == 0) yield e
res4: Array[Int] = Array(2, 4)
scala> a
res10: Array[Int] = Array(1, 2, 3, 4, 5)

scala> val b = 6 to 7
b: scala.collection.immutable.Range.Inclusive = Range 6 to 7

scala> for {
     | x <-a
     | y <-b
     | } yield (x,y)
res11: Array[(Int, Int)] = Array((1,6), (1,7), (2,6), (2,7), (3,6), (3,7), (4,6), (4,7), (5,6), (5,7))

scala> for {
     | y <- b
     | x <- a
     | } yield (x,y)
res12: scala.collection.immutable.IndexedSeq[(Int, Int)] = Vector((1,6), (2,6), (3,6), (4,6), (5,6), (1,7), (2,7), (3,7), (4,7), (5,7)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值