I
I
irdis_132015-09-23 01:04:33
Scala
irdis_13, 2015-09-23 01:04:33

Why is immutable good?

When you read smart books on functional programming (in my case it was a book on Scala), they write everywhere that immutable is good. It seems that the result is obvious: the problem with multithreading and the side effect that mutable can lead to disappears by itself. But this approach also has its drawbacks (as I understand it).
For example, there is a task to add 1000 elements to an array, but there are no 100,000 elements. A true-fp programmer will say that immutable is good and we need to create a new array every time we add it, which consists of the elements of the old +1 (which is successfully implemented at the PL level, in the second case of Scala). Is this approach justified in terms of resources (memory in this case)? After all, the task is simple, to add elements to the array, for which we have the creation of 100,000 instances of the array. Yes, I understand that there is a GC that will clean up after us, but creating such a rather complex data structure as an array (meaning List) will take quite a lot of time.
What do you say to that?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Kondratiev, 2015-09-23
@Andruhon

Among other things, in my opinion - things that change are always additional complexity and a source of defects. Thus, immutable allows you to reduce the above risks, sometimes at the cost of resources.
Let's say there is a class in which there is some field that is set when an instance of this class is created, and by reading the code - we can be 100% sure that this field will retain its value under any conditions for this instance and we will not need to look for something else could happen to him. Again, you don't have to bother with getters and created a public immutable variable - and the data is immediately protected from writing from outside.

D
Dmytro Mantula, 2015-10-01
@diez_ua

As for immutable arrays, there are none in Scala.
There is an immutable List. This List is designed to add and remove elements at the beginning of the list - head. And if you map or filter a list, the new list is formed in the same "optimal" order.
In this case, there is no memory overhead and GC load compared to Java LinkedList: when an element is added, a NEW List object is created with a link to the new element (head) and a link to the old list (tail). Old sheets in action. Each new sheet is an object of just two references.

L
laughedelic, 2015-10-01
@laughedelic

Is this approach justified in terms of resources (memory in this case)?

In functional languages, including Scala, so-called. persistent data structures . Memory stores references to previous states of the data structure and changes. This makes sense in combination with the garbage collection you mentioned. Of course, you can just copy everything, but in reality no one does that. Because FP makes a lot of use of structures like lists and trees, it's easy to implement it efficiently for them.
I recommend reading (English) Wikipedia at the link above or this wiki in Russian .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question