What are the real benefits of immutable collections?

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

What if I list subsets , permutations and combinations for example? Are there immutable collections here?

+6
source share
6 answers

What are the benefits of invariably regular data processing?

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

+10
source

This is true. Since you are enumerating a collection, presumably you must be sure that the elements will not be accidentally added or deleted during the enumeration.

Consistency is a very paradigm in functional programming. Creating immutable collections allows you to think of them in the same way as primitive data types (i.e., changing a collection or any other object leads to the creation of another object, since adding from 2 to 3 does not change 3, but creates 5)

+7
source

To expand Matt's answer: from my personal experience, I can say that the implementation of algorithms based on search trees (for example, width first, depth first, backtracking) using mutable collections regularly appears as a smoking pile of crap: either you forget to copy the collection before the recursive call, or you will not be able to revert the changes correctly if you return the collection. In this area, immutable collections are clearly superior. I ended up writing my own immutable list in Java when I was unable to get the problem using Java collections. And so, the first "immutable" implementation worked immediately.

+6
source

If your data does not change after creation, use immutable data structures. The type you choose will determine the intent of use. Anything more specifically will require knowledge of your specific problem space.

You can really find a subset, permutation, or combination generator, and then discussing data structures is controversial.

In addition, you mentioned that you understand the parallel benefits. Presumably, you throw some kind of algorithm at permutations and subsets, and there is a good chance that the algorithm can be parallelized to some extent. If, in this case, using immutable structures ahead ensures that your initial implementation of Algorithm X is easily converted to a parallel Algorithm X.

+4
source

I have several advantages for adding to the list:

  • Immutable collections cannot be canceled from under you

    That is, it is quite normal to have immutable public val members of the Scala class. By definition, they are read-only. Compare with Java, where you not only have to remember to make it private, but also write a get method that returns a copy of the object so that the source code is not changed by the calling code.

  • Continuous 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 saves time and space and compensates for some of the penalties associated with using immutability.

+2
source

Some of the immutable benefits:

1 - a smaller margin for error (you always know that your collections and variables are read-only).

2 - you can write parallel programs without worrying that threads are advancing against each other when changing variables and collections.

0
source

Source: https://habr.com/ru/post/900806/


All Articles