I am trying to understand for understanding in Scala, and I have many examples that I understand ...
One thing that is hard for me to figure out is :() vs for {}. I have tried both, and it seems that I can do one 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 will 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 the Coursera class that I accept. The professor did not mention the why in the video, and I was wondering if anyone else knew.
Thanks!
source share