I'm a novice user of functional programming. I might have a misconception about functional programming (FP) but, I think a pure FP language does not allow mutable object.
I have two questions. One is somehow theoretical and the other one is about a concrete Java code.
Question 1 How does a pure FP language avoid performance overhead of copying object?
var x = getMassiveObject()
var y = f(x)
I think x
must be copied in f
because x
is immutable and nobody can not touch the memory space of x
. If x
is huge, copying process seems to take a lot of time.
But, I did not read any discussion about this point, so I believe there is not this kind of overhead in FP language.
So, my first question is How does FP language avoid the overhead of copying immutable huge object?
Question2 I'd like to build Java program with FP way to avoid side-effect. My second question is how can I rewrite below code to eliminate a overhead of copy?
public void main(){
MassiveClass m = new MassiveClass();
MassiveClass n = convert(m);
}
public MassiveClass convert(MassiveClass arg){
MassiveClass arg2 = deepCopy(arg); // copy arg because arg should not be modified
arg2.var = 100;
return arg2
}