Michael
Michael

Reputation: 10303

What are the real advantages of immutable collections?

Scala provides immutable collections, such as Set, List, Map. I understand that the immutability has advantages in concurrent programs. However what are exactly the advantages of the immutability in regular data processing?

What if I enumerate subsets, permutations and combinations for example? Does the immutable collections have any advantage here?

Upvotes: 6

Views: 3976

Answers (6)

Seif Tml
Seif Tml

Reputation: 2423

some of immutability advantages :

1 - smaller margin for error (you always know what’s in your collections and read-only variables).

2 - you can write concurrent programs without worrying about threads stepping on each other when modifying variables and collections.

Upvotes: 0

Connor Doyle
Connor Doyle

Reputation: 1912

I have a couple advantages to add to the list:

  1. Immutable collections can't be invalidated out from under you

    That is, it's totally fine to have immutable public val members of a Scala class. They are read-only by definition. Compare to Java where not only do you have to remember to make the member private but also write a get method that returns a copy of the object so the original is not modified by the calling code.

  2. Immutable data structures are persistent. This means that the immutable collection obtained by calling filter on your TreeSet actually shares some of its nodes with the original. This translates to time and space savings and offsets some of the penalties incurred by using immutability.

Upvotes: 2

Landei
Landei

Reputation: 54584

To expand Matt's answer: From my personal experience I can say that implementations of algorithms based on search trees (e.g. breadth first, depth first, backtracking) using mutable collections end up regularly as a steaming pile of crap: Either you forget to copy a collection before a recursive call, or you fail to take back changes correctly if you get the collection back. In that area immutable collections are clearly superior. I ended up writing my own immutable list in Java when I couldn't get a problem right with Java's collections. Lo and behold, the first "immutable" implementation worked immediately.

Upvotes: 6

Stefan Kendall
Stefan Kendall

Reputation: 67892

If your data doesn't change after creation, use immutable data structures. The type you choose will identify the intent of usage. Anything more specific would require knowledge about your particular problem space.

You may really be looking for a subset, permutation, or combination generator, and then the discussion of data structures is moot.

Also, you mentioned that you understand the concurrent advantages. Presumably, you're throwing some algorithm at permutations and subsets, and there's a good chance that algorithm can be parallelized to some extent. If that's the case, using immutable structures up front ensures your initial implementation of algorithm X will be easily transformed into concurrent algorithm X.

Upvotes: 4

Mike Dinescu
Mike Dinescu

Reputation: 55760

It does. Since you're enumerating on a collection, presumably you'd want to be certain that elements are not inadvertently added or removed while you're enumerating.

Immutability is very much a paradigm in functional programming. Making collections immutable allows one to think of them much like primitive data types (i.e. modifying a collection or any other object results in creating a different object just as adding 2 to 3 doesn't modify 3, but creates 5)

Upvotes: 7

Matt Ball
Matt Ball

Reputation: 360026

What are exactly the advantages of the immutability in regular data processing?

Generally speaking, immutable objects are easier/simpler to reason about.

Upvotes: 11

Related Questions