MOCKSTACKS
EN
Questions And Answers

More Tutorials









Scala Working with data in immutable style

Examples

It is not just val vs. var


val and var

scala> val a = 123
a: Int = 123
scala> a = 456
<console>:8: error: reassignment to val
 a = 456
scala> var b = 123
b: Int = 123
scala> b = 321
b: Int = 321

• val references are unchangeable: like a final variable in Java, once it has been initialized you cannot change it

• var references are reassignable as a simple variable declaration in Java

Immutable and Mutable collections


 val mut = scala.collection.mutable.Map.empty[String, Int]
 mut += ("123" -> 123)
 mut += ("456" -> 456)
 mut += ("789" -> 789)
 val imm = scala.collection.immutable.Map.empty[String, Int]
 imm + ("123" -> 123)
 imm + ("456" -> 456)
 imm + ("789" -> 789)
 scala> mut
 Map(123 -> 123, 456 -> 456, 789 -> 789)
 scala> imm
 Map()
scala> imm + ("123" -> 123) + ("456" -> 456) + ("789" -> 789)
 Map(123 -> 123, 456 -> 456, 789 -> 789)

The Scala standard library offers both immutable and mutable data structures, not the reference to it. Each time an immutable data structure get "modified", a new instance is produced instead of modifying the original collection in-place. Each instance of the collection may share significant structure with another instance.

But I can't use immutability in this case!


Let's pick as an example a function that takes 2 Map and return a Map containing every element in ma and mb:

def merge2Maps(ma: Map[String, Int], mb: Map[String, Int]): Map[String, Int]

A first attempt could be iterating through the elements of one of the maps using for ((k, v) <- map) and somehow return the merged map.

def merge2Maps(ma: ..., mb: ...): Map[String, Int] = {
 for ((k, v) <- mb) {
 ???
 }
}

This very first move immediately add a constrain: a mutation outside that for is now needed. This is more clear when de-sugaring the for:

// this:
for ((k, v) <- map) { ??? }
// is equivalent to:
map.foreach { case (k, v) => ??? }


Conclusion

In this page (written and validated by ) you learned about Scala Working with data in immutable style . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala Working With Gradle.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.