3

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
}
Light Yagmi
  • 5,085
  • 12
  • 43
  • 64
  • 1
    If you want to change something, but don't want to modify the original, then how would you possibly not copy it? At best, you don't need a deep copy if you're only changing one field. – user253751 May 20 '15 at 10:12
  • 2
    One option is to use wrappers. I am not saying that this is done, but it is certainly possible to have WrapperClass extends MassiveClass and implement the methods of MassiveClass by delegation into a contained, unmodified massive. – tucuxi May 20 '15 at 10:13
  • 1
    shallow copy is enough – kutschkem May 20 '15 at 10:28
  • 1
    @immibis thanks. You means FP language always suffers from overhead of copy when a modified instance is needed? – Light Yagmi May 20 '15 at 10:43
  • 1
    This question has a nice answer about functional programming and mutability. http://stackoverflow.com/questions/1020653/how-can-you-do-anything-useful-without-mutable-state and you may also be interested in Functional Java at http://www.functionaljava.org/ and take a look at some of the materials created by Neal Ford such as the series of articles on IBM Developer Works library https://www.ibm.com/developerworks/library/j-ft1/ – Richard Chambers May 20 '15 at 23:49

1 Answers1

1

First question: x is not copied. It's passed to function f. Immutability means x will remain the same and its state will not change after construction and until the end of object's lifecycle.

Second question: In order an object to be immutable in Java, all of its fields should be immutable. If you put List as a field in MassiveClass, it's not immutable anymore. You have to do defensive copying, as already you did.

mtyurt
  • 3,369
  • 6
  • 38
  • 57
  • Thanks, but I believe x is (at least implicitly) copied before obtaining updated object in f. This is because of the same reason that is mentioned by @immibis in the comment section. – Light Yagmi May 20 '15 at 10:48
  • Well, what you believe might not be true. Address of `x` is copied, not `x` itself. When you call `f(m)` where m is `MassiveObject` in Java, address of `m` is copied, not `m` itself. That's the whole point of immutability. The class must be immutable because the object is passed around in the program. It should not allow any mutations after construction. – mtyurt May 20 '15 at 11:51
  • thanks, but still I think my assumption is right. In Java, just a reference is passed. That's why I believe copying the argument is necessary before getting the updated object and it leads the overhead. However, in FP, that's kind of overhead doesn't seems to be a problem. So, my question is how the overhead is avoided inFP, and how can I realize that in Java. – Light Yagmi May 20 '15 at 14:29
  • @rkjt50r983 I believe you're not understanding each other because you forgot, in your original question #1, to say what `f()` *does* and *returns*. Does it do something like your `convert()` method in question #2? – Dan Getz May 20 '15 at 23:38
  • @rkjt50r983 You're wrong. In pure FP, data are immutable, and thus are not updated. Hence, no need to copy them. – Ingo May 21 '15 at 18:22