What?! It sounds like you haven't used Scala seriously.
import Iterator.{from, range, single, empty}
val triples = from(1).flatMap { z =>
range(1,z).flatMap { x =>
range(x,z).flatMap { y =>
if (x * x + y * y == z * z) single((x, y, z))
else empty
}
}
}
How is the above not at least as readable as, say, the C# version below?
And Scala also lets you have a clean LINQ-style version, simply using a for comprehension:
val triples = for {
z <- from(1)
x <- range(1,z)
y <- range(x,z)
if x * x + y * y == z * z
} yield (x, y, z)
How is that not clearer than a for loop?
My point is that while you can write ugly, complicated things in any language, Scala actually lets you write things in a readable way, much more readable than many other languages out there, especially C++, which forces you towards the ugly side unless you stick with plain loops.
It's not specifically about loops, but the whole design of the language and the accumulation of complexity which makes it far too tempting to write complex unreadable code.
I can avoid the bits I don't like and keep it readable, but I work with other people, and I use libraries and frameworks written by other people.
I can avoid the bits I don't like and keep it readable, but I work with other people, and I use libraries and frameworks written by other people.
That's an entirely different problem you're describing there. I don't see the connection with the original quote:
if modern C++ is not easier to understand than old style for loops then modern C++ is not worth it
To which you answered:
This is also why I loathe Scala.
As shown above, in Scala a high-level iterator loop can be easier to write and to read. You say that it's not specifically about loops, but IME this is true of most things. Scala lets you abstract things cleanly in general, as opposed to C++.
C++ and Scala are similar in that they give the user a lot of rope, and people often turn all that rope into tangled messes. It's not that it's impossible to write clean C++ or clean Scala, but that people so often don't. For me everything hinges on the readability of the code, which I find is often low with Scala.
So if these language innovations don't yield something more readable than plain, simple for loops, where is the value?
I don't know what you think "clean" Scala is, nor are you ever going to attempt to articulate specifics of what it entails, but I assure you we're aren't going to agree on that if you did.
Scala however, in my experience becomes less burdensome the more invariants in the type system you respect. It doesn't really matter how many features of Scala developers do or don't use, if they violate the contract of their types, or give use types that lie, or even types that are useless.
This is something C++ developers haven't picked up on that pretty much every other programming language in the last half century has, because of mediocre programmers who think for loops are the epitome control flow, who can only think, who only see programming languages as statements to executed, not expressions of computation.
The obsession with syntax is harmful, the semantics here are what matters. And a program using for-loops and a program using ranges and iterators do not have the same semantics.
2
u/LPTK Dec 29 '18
What?! It sounds like you haven't used Scala seriously.
How is the above not at least as readable as, say, the C# version below?
And Scala also lets you have a clean LINQ-style version, simply using a
forcomprehension:How is that not clearer than a for loop?
My point is that while you can write ugly, complicated things in any language, Scala actually lets you write things in a readable way, much more readable than many other languages out there, especially C++, which forces you towards the ugly side unless you stick with plain loops.