Functional programming is a paradigm which has its roots in mathematics, primarily stemming from lambda calculus. Functional programming aims to be...
For further actions, you may consider blocking this person and/or reporting abuse
Note / opinion: while use of copies serves the example (I guess), in real world one should definitely fix
logElementsnot to modify the input parameter. Please don't leave bombs like this to your colleagues -logElementsname does not suggest it would modify the elements array.I would call the suggested "fix" a "workaround", which should only be made if you can't modify the
logElementsfunction.I agree that this isn’t good code or practice to use in a production environment and if seen in the wild it should be refactored but that was the point of the example.
Furthermore the example isn’t my own and instead comes from the linked source who uses this example to showcase a similar point.
The other point of consideration is that in some environments it is unavoidable to have mutations to the inputs and so clones and copies become the default solution to avoid mutation to the original values which this example showcases.
Really doesn't matter. I was commenting about the example, not about you :)
The aim is to let potentially inexperienced readers know that this is typically not OK.
Unfortunately, yes.
I would also like the readers to understand that it's more of a workaround than a solution.
“I would also like the readers to understand that it's more of a workaround than a solution.” - only if it’s avoidable since in the environments where we can’t have proper immutability this is the solution.
Fair enough, it’s a point though. Thanks for the comments!
Mmm.. interesting philosophical topic. I really need to think about it. Thinking here now. I would say that it's a workaround, which is the appropriate solution for the situation (as opposed to refactoring the original function, which is not possible).
Keeping it in mind and in comments as "workaround" should potentially push more towards the "real" solution when it becomes possible.
I guess "workaround" is specific type of solution in this situation. Between two words when one is more specific, I choose the specific one unless I specifically want to generalize.
Hi, James.
While talking about "pure functions", you mentioned that the function getSquare( items ) is an impure one. Just because "(...) any function that changes its inputs or the value of some external variable is an impure function.". So, since we are receiving a container, changing its content and returning it already modified, leads to be working with an impure function.
What about if I perform an internal copy of the "items" parameter, like "items1", and then do exactly the same process your function does but over the "items1" container and finally I return "items1". Should now this be a pure function? If the answer is "no", which would be the reason? Another point here might be "because we have a loop inside it, which is prone to side effects", correct? But, if this is the case, we have another reason different from: "any function that changes its inputs or the value of some external variable is an impure function."
I have asked this same question (based on your post) to a well-known Developer, Teacher and Writer and he gave another definition to me of a "pure function": "A function should always give the same result when given the same arguments."
This definition is clearly different than yours.
Could you please clarify and explain this topic better?
Thanks in advance!
As I stated in the article:
So long as our inputs are unaffected and the output is consistent, the function is pure but if you alter variables you then break the rules of immutability but that's a different point.
If acting on an internal variable that is thrown away after execution, that is fine so long as the internal variable does not break the rules of pureness itself, for example mutating another internal variable such as:
But it does break the rules of immutability which is why it should be avoided to do something like this. We never want to mutate variables but to create immutable deep copies or new values:
This is a silly example since returning
a + bwould suffice but if you do need variables then immutable constants are the way to go.Loops aren't inherintly bad constructs, they have uses but generally speaking whatever a loop can do, so can mappers, reducers, recursion and so on. For example in the article on the pure functions section I gave the example of calculating the squares of numbers, a pure way to do that without loops would be like so:
No loops required.
As I stated in the pure functions section:
The first point is the same as the definition the "well-known Developer, Teacher and Writer" stated.
If you need something else clarified though, feel free to ask!