Scala for understanding: a vital feature or syntactic sugar?

When I first started looking at Scala, I liked the look for understanding. They seemed a bit like the foreach loops that I'm used to with Java 5, but with functional limitations and a lot of nice syntactic appeal.

But as I absorbed the Scala style, I found that every time I could use for-comprension, I use map , flatMap , filter , reduce and foreach instead. The intent of the code seems clearer to me, with fewer potential hidden surprises, and they are usually shorter than the code too.

As far as I know, after all, an understanding is always compiled into these methods, so I wonder: what are they really on? Did I miss some functional revaluation (this will not be the first time)? Do you understand for understanding that other functions cannot or at least will be much more awkward? Do they shine under a specific precedent? Is this really a matter of personal taste?

+19
scala list-comprehension
Sep 06 '09 at 15:32
source share
5 answers

Please refer to this question . The short answer is that understanding can be more readable. In particular, if you have many nested generators, the actual scope that you do becomes more understandable, and you do not need huge indents.

+13
Sep 07 '09 at 1:27
source share

Another great use for understanding is for internal DSL. ScalaQL is a great example. He can turn this

 val underAge = for { p <- Person c <- Company if p.company is c if p.age < 14 } yield p 

in that

 SELECT p.* FROM people p JOIN companies c ON p.company_id = c.id WHERE p.age < 14 

and much more.

+11
Sep 07 '09 at 3:15
source share

for-understanding - syntactic sugar, but this does not mean that they are not vital. They are usually shorter than their extended form, which is nice, but perhaps more importantly, they help programmers from imperative languages ​​use functional constructs.

When I first started with Scala, I used a lot to understand, because they were familiar. Then I almost completely stopped, because I felt that using the basic methods was more explicit and therefore more clear. Now I will return to the use of concepts because I think they better express the intention of what I am doing, rather than the means to do this.

+10
Sep 06 '09 at 16:29
source share

In some cases, intent may be expressed better for understanding, so when they do, use them.

Also note that for understanding, you get free template design. For example, iterating over a map is much easier to understand:

for ((key, value) <- map) println (key + "-->" + value)

than with foreach:

map foreach { case (key, value) => println (key + "-->" + value) }

+5
Sep 05 2018-11-11T00:
source share

You're right. Understanding is syntactic sugar. I find that the basic methods are more concise and easier to read as soon as you get used to them.

Compare the following equivalent statements:

 1. for (i <- 1 to 100; if (i % 3 == 0)) yield Math.pow(i, 2) 2. (1 to 100).filter(_ % 3 == 0).map(Math.pow(_, 2)) 

In my opinion, adding a semicolon to # 1 detracts from the fact that this is a simply connected statement. There is also a feeling that I am var (is it 1, or 99, or is there something in between?) That detracts from the functional style otherwise.

Option 2, obviously, is a chain of method calls for objects. Each link in the chain clearly states its responsibility. There are no intermediate variables.

Perhaps understanding is included as a convenience for developers migrating from Java. Whatever is chosen is a matter of style and preference.

+3
Sep 06 '09 at 23:09
source share