My question is a more specific instantiation of this question: Functional programming: state vs. reassignment
I'm a newbee to FP and trying to understand it through Java.
I have the following class whose object is shared between multiple threads:
public class Bank
{
private double[] accounts = new double[1000];
public synchronized void transfer(int from, int to, double amount)
{
account[from] -= amount;
account[to] += amount;
}
}
(This is a very simplified example, hence other details such as validation and condition waiting are omitted).
Because 'transfer' method is synchronized, Bank object's mutable state won't be corrupted even if it's shared with multiple threads. If I want to achieve the same thing through FP in Java, how would I write that code? (I would like to see an actual code example).
EDIT: my interest in FP stems from its potential for writing thread-safe, concurrent applications. Following is a link to an article that claims it: http://www.defmacro.org/ramblings/fp.html
EDIT2: just found out about an STM for Java. Not sure about its performance, but it seems to provide what I wanted. http://multiverse.codehaus.org/60second.html