Software engineering is full of jargon. Occasionally, to grasp the true meaning of the seemingly simplest of words, one must waddle through many mu...
For further actions, you may consider blocking this person and/or reporting abuse
There are a few use cases where an implementation of a function could be considered 'not pure' while, from the outside, it is pure.
Yah I agree that Impure Function is also super useful in certain contexts. Sometimes, we use in-place solution to reduce the space complexity instead of creating copies with Pure Function
Nice article!
I think that it is worth pointing out could be that spread syntax only is pure for primary types. For objects and that sort of stuff it is only the reference that's being copied so any mutations would also mutate the orignal object.
.sort() is also impure, sorting the input in-place.
Thanks for the suggestion, I've updated the list!
I'd like a way to flag pure functions like an annotation or similar. Last time I looked this up I didn't find any so I'm throwing this out - anyone doing this, and how?
example, jsdoc style
I would love that too.
Great article! I would like to add that writing a pure function is more about how much control/ confidence you have on that piece of code, rather than following a bunch of rules and assuming it as a pure function. By confidence/ control, I mean how sure are you that if a certain input is any given later in time, that piece of code will give the same output.
That's how I see pure functions.
And in particular, you would need to assign the result of using the rest operator to a new variable get the sorted array?
For example:
const array = [1,2,6,5,4]
let newArray = [...array].sort((a,b) => a-b )
console.log(newArray) // [1, 2, 4, 5, 6]
console.log(array) // [1,2,6,5,4]
Great, I want to be exact in the language I use so thank you for commenting :) I edited “considered pure” to “typically associated with pure functions”.
As for the definition, I found it elsewhere. Before I update it, let me try and see if I’m parsing what you’re saying correctly: you’re saying that while my definition describes some side-effects, it doesn’t describe ALL side-effects, because there are some which are indeed related to computing the final output. Is that right?
Great write up, educational and entertaining! Definitley going to be using the Slytherin reference re function purity now with my teammates lol
Great lunchtime read! Thanks for the well-researched article!
Thanks
So cool man! Thanks. Best article on web.
Ansible refers pure functions as Idempotent Playbooks which are useful for retries
Nice article