6 ms·
Ok. No. There's a lot of confusion with semantics. Following the most used definition of OOP as defined by JAVA, C++ and C#: Classes are in no way functions in
by leafboi 6y ago
Ok. No. There's a lot of confusion with semantics. Following the most used definition of OOP as defined by JAVA, C++ and C#:
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.
- bitdizzy 6y agoYou're making an argument from definitions that would exclude ML from being a functional language, which is absurd.
- leafboi 6y agoNo I'm using the mathematical definition of a function. ML is not Math nor is it purely functional. 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.
- bitdizzy 6y ago> No I'm using the mathematical definition of a function. 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?
- leafboi 6y agoI'm not having a dogmatic stance please stop voting me down. I'm tired of juniors who think they know what they're talking about. Just stop. https://www.wikiwand.com/en/Function_(mathematics) https://www.wikiwand.com/en/Function_(mathematics) That is the definition of a function. Quite complex. I simplified the explanation by using a Box. You will note in the wikipedia article they also have a picture of a Box with inputs and outputs. That is the essence of functional programming. Now please don't turn this into a immature pedantic argument about some obscure definition of a mathematical function. >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? Uh they do (and it's not a dogmatic stance, the definition of FP is quite blurry, but immutability is universal). The point of functional programming hence the name is for the programming to follow mathematical functions as closely as possible. In the ideal world your program in FP is just one giant pure function that is a composition of other smaller functions. Of course your functions need to have side effects so they come up with all kinds of tricks or bend the rules a bit so that you can have things like IO and side effects but these are just exceptions to the rule. The philosophy of functional programming is to make your program as pure as possible and minimize or segregate side effects away from your program as much as possible. This is essentially what and all that Haskell is. Haskell uses a trick, a VM that takes an IO monadic Value and does side effects with it. Outside of that VM all the user does to the program is write Pure functions and compose them all together into a single function called Main. That's it. That's literally what haskell is, and if you think PL theorists don't agree with my "dogmatic" stance which is to say not "dogmatic" at all then you really don't know what you're talking about because Haskell is basically designed by the best in class functional PL theorists. Let me put it simply for you. If you can put your entire program in a single expression then you are doing functional programming. That is what functional programming is. There are no steps or procedures that happen one after the other. Every value is defined permanently. There's no concept of time or procedures in functions that's why you can't have variables getting assigned or mutated in FP. You can only define things in functional programming as they are right now without the concept of time in FP. Procedural programmers tend to create buckets of empty data variables to be filled in over clock cycles. If you don't get this, you don't understand functional programming. In an expression, you cannot mutate values you cannot have procedures or loops. Why do you think FP is loaded with recursion? If they allowed setters and getters and mutation like the example then we might as well add a jump command while your at it and suddenly we can use loops in functional programming. You just don't get it man. Dogmatic my ass, not even close.