0

I have heard that it is a good practice in Scala to eliminate for loops and do things "the Scala way". I even found a Scala style checker at http://www.scalastyle.org. Are for loops a no-no in Scala? In a course at https://www.udemy.com/course/apache-spark-with-scala-hands-on-with-big-data/learn/lecture/5363798#overview I found this example, which makes me thing that for looks are okay to use, but using the Scala format and syntax of course, in a single line and not like the traditional Java for looks in multiple lines of code. See this example I found from that Udemy course:

val shipList = List("Enterprise", "Defiant", "Voyager", "Deep Space Nine")
for (ship <- shipList) {println(ship)}

That for loop prints this result, as expected:

Enterprise Defiant Voyager Deep Space Nine

I was wondering if using for as in the example above is acceptable Scala style code, or it if is a no-no and why. Thank you!

2 Answers 2

3

There is no problem in this for loop, but you can use functions form List object for your work in more functional way.

e.g. instead of using

val shipList = List("Enterprise", "Defiant", "Voyager", "Deep Space Nine")
for (ship <- shipList) {println(ship)}

You can use

val shipList = List("Enterprise", "Defiant", "Voyager", "Deep Space Nine")
 shipList.foreach(element => println(element) )

or

shipList.foreach(println)
    
Sign up to request clarification or add additional context in comments.

Comments

0

You can use for loops in Scala, there is no problem with that. But the difference is that this for-loop is not an expression and does not return a value, so you need to use a variable in order to return any value. Scala gives preference to work with immutable types.

In your example you print messages in the console, you need to perform a "side effect" to extract the value breaking the referencial transparency, I mean, you depend on the IO operation to extract a value, or you have mutate a variable which is in the scope which maybe is being accessed by another thread or another concurrent task thereby there is no guarantee that the value that you collect wont be what you are expecting. Obviously, all these hypothesis are related to concurrent/parallel programming and there is where Scala and the immutable style help.

To show the elements of a collection you can use a for loop, but if you want to count the total number of chars in Scala you do that using a expression like:

val chars = shipList.foldLeft(0)((a, b) => a + b.length)

To sum up, most of the times the Scala code that you will read uses immutable style of programming although not always because Scala supports the other way of coding too, but it is weird to find something using a classic Java OOP style, mutating object instances and using getters and setters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.