加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

理解Scala for For循环的范围(理解)

发布时间:2020-12-16 08:55:28 所属栏目:安全 来源:网络整理
导读:在 Programming Scala的第3章中,作者给出了for循环/ for comprehension的两个例子,但是在using()和{}之间切换.为什么会出现这种情况,因为这些本身看起来像是在做同样的事情?品种 - dogBreeds是否在示例#2的第二行? // #1 ()'sfor (breed - dogBreeds if br
在 Programming Scala的第3章中,作者给出了for循环/ for comprehension的两个例子,但是在using()和{}之间切换.为什么会出现这种情况,因为这些本身看起来像是在做同样的事情?品种< - dogBreeds是否在示例#2的第二行?

// #1 ()'s
for (breed <- dogBreeds
  if breed.contains("Terrier");
  if !breed.startsWith("Yorkshire")
) println(breed)

// #2 {}'s
for {
  breed <- dogBreeds
  upcasedBreed = breed.toUpperCase()
} println(upcasedBreed)

解决方法

如果您阅读绿色提示:

for expressions may be defined with
parenthesis or curly braces,but using
curly braces means you don’t have to
separate your filters with semicolons.
Most of the time,you’ll prefer using
curly braces when you have more than
one filter,assignment,etc.

因此,对于使用()和{}的理解是唯一改变的是使用的分隔符:for()你必须使用分号“;”作为分隔符和{}你使用新行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读