I learned to code when ‘algorithms + data structure = programs’. I haven’t ever used OO in anger but I think I understand the basics. My mental model involves the manipulation of state in the form of values, stored in variables.
I’ve used functions and recursion successfully in VAX/VMS Pascal so I understand how functional languages use recursion to replace loops BUT I’m really stumped by immutability. I don’t know how to get some stuff done without variables.
I understand that pure functional languages can’t have side effects, so can’t directly mutate state (but Clojure isn’t pure.) I’ve worked through a few examples that look like a pipe-line of functions transforming one data structure into another but I can’t see how to do some really simple things.
Imagine a shop that sells 1 type of widget and when stock falls below 40% of maximum, re-orders. In an imperative language, I could have a constant for target stock and the percentage and a variable to keep count of how many were left. Assuming a multi-user system, I’d expect to have some sort of transactional locking system on my 1 variable. How on earth do I do it in magic function land? Without parallelism, I can see how a function might recurse and maintain a value within that single function but my function could be called on multiple cores at once. Can anyone help me see what I’m missing?
Now I’ve written all that: is my assumption that multiple calls of the same function are completely independent valid? If the interpreter queues calls to each function, there would be a blocking mechanism but that would be a limit on parallel scalability, so seems unlikely.