Scala for ( ) vs for { }

anonygrits

I'm trying to understand for comprehensions in Scala, and I have a lot of examples that I sort of understand...

One thing I'm having a hard time figuring out is for ( ) vs for { }. I've tried both, and it seems like I can do one thing in one but it breaks in the other.

For example, this does NOT work:

def encode(number: String): Set[List[String]] =
  if (number.isEmpty) Set(List())
  else {
    for (
      split <- 1 to number.length
      word <- wordsForNum(number take split)
      rest <- encode(number drop split)
    ) yield word :: rest
  }.toSet

However, if you change it to { }, it does compile:

def encode(number: String): Set[List[String]] =
  if (number.isEmpty) Set(List())
  else {
    for {
      split <- 1 to number.length
      word <- wordsForNum(number take split)
      rest <- encode(number drop split)
    } yield word :: rest
  }.toSet                                 

These examples are from a Coursera class I'm taking. The professor didn't mention the "why" in the video & I was wondering if anyone else knows.

Thanks!

som-snytt

From the syntax in the spec, it might seem that parens and braces are interchangeable:

http://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#for-comprehensions-and-for-loops

but because the generators are separated by semis, the following rules kick in:

http://www.scala-lang.org/files/archive/spec/2.11/01-lexical-syntax.html#newline-characters

I have read and understood that section in the past, from which I vaguely recall the gist that newlines are enabled in the braces, which is to say, a newline char is taken as nl which serves as a semi.

So you can put the generators on separate lines instead of using semicolons.

This is the usual "semicolon inference" that lets you not write semicolons as statement terminators. So the newline in the middle of the generator is not taken as a semi, for instance:

scala> for (c <-
     | List(1,2,3)
     | ) yield c+1
res0: List[Int] = List(2, 3, 4)

scala> for { c <-
     | List(1,2,3)
     | i = c+1
     | } yield i
res1: List[Int] = List(2, 3, 4)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Scala列表方法`::`vs`+:`

来自分类Dev

Scala-布尔值-&vs &&,| vs ||

来自分类Dev

scala() vs {} 参数调用的定义

来自分类Dev

SCALA 上的 yld VS 产量

来自分类Dev

Scala Stream vs Scala List vs Scala Sequence 有什么区别

来自分类Dev

Scala递归:return vs if / else控制

来自分类Dev

Scala:抛出错误vs返回尝试?

来自分类Dev

Scala:传递VS应用功能

来自分类Dev

Scala FunctionN tupled vs curried result interpretation

来自分类Dev

Scala贴图-> Reduce vs.FoldRight

来自分类Dev

Scala“ def”方法声明:冒号vs等于

来自分类Dev

Scala隐式vs隐式参数

来自分类Dev

使用toSet.toList的Scala vs区别

来自分类Dev

Scala vs Haskell类型类:“ catchall”实例

来自分类Dev

Phantom vs Quill for Playframework(Scala)和Cassandra

来自分类Dev

Scala映射isDefinedAt()vs contains()方法

来自分类Dev

Scala设置vs Map进行理解

来自分类Dev

在anorm vs slick上播放scala建议

来自分类Dev

Scala:通过Vs应用功能

来自分类Dev

Scala通用vs存在类型混淆

来自分类Dev

Phantom vs Quill for Playframework(Scala)和Cassandra

来自分类Dev

Spark Scala Int vs Integer for Option vs StructType

来自分类Dev

Scala函数N元组vs咖喱结果解释

来自分类Dev

Scala导入packageName.ClassName VS导入package._开销?

来自分类Dev

Kotlin VS Scala:使用主要构造函数参数实现方法

来自分类Dev

Scala函数N元组vs咖喱结果解释

来自分类Dev

斯卡拉(Scala)演员vs阿卡(Akka)演员

来自分类Dev

Scala:Option [Seq [String]] vs Seq [Option [String]]?

来自分类Dev

Scala二进制vs Scala完整版约定