by leafboi · 6y ago · 0 comments
Classes are in no way functions in the FP sense because classes mutate state.
Classes are in no way functions because calling methods requires the instantiation of unrelated state. Example:
class A
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
self.banana = create_banana(create_gorilla(create_jungle()))
def process_a(self):
return process(self.a)
process_a cannot be used unless a, b, c and the banana plus the gorilla holding the banana and the entire jungle the gorilla lives in is instantiated.Let's talk about terminology. There are three that often get mixed up.
Procedure = a set of instructions that could encompass mutating and changing state.
Function = a box that when given input produces an output. The box cannot modify the universe. It can only produce something new when given an input.
Object = A grouping of a set of procedures and state that can mutate.
The javascript programmer OP is mixing up function and procedure. He defines a procedure and says that it can be a class. A procedure can be a class as it's just a set of instructions which can encompass defining scope, state and more procedures restricted to scope and state (aka an object).This thing right here, written by the OP:
function setFirstName(firstName) {
fName = firstName
}
Is not a function... it is a method or a procedure that mutates state.A function can do none of the above.
That's it.
All functional languages need to bend the rules a bit in order to have side effects but in general the person is not writing functions and therefore is not getting the benefits of using functions.
FP is about reducing and restricting these functions that break the rules. Defining a class in terms of mutations and side effects is going against the philosophy of FP.
Which mathematical definition? A "box" is not a mathematical definition. There are multiple mathematically precise definitions of "function" possible. Yours is none of them. Some of them accommodate objects that model mutable state.
I don't think many (any?) PL theorists or functional programming practitioners would agree with your dogmatic stance. Where are you coming from with all this?