-
Notifications
You must be signed in to change notification settings - Fork 42
Add Explanation of Pure vs. Impure Functions #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@rjmk care to enlighten us with a practical example of why functional purity is obviously betterer...? 😉 |
Hm. Purity's greatest benefits are observed in large applications where various bugs disappear and the parts of the application that you have to hold in your head at any point become greatly diminished. Before I try and come up with an example, here are some of the benefits of pure functions:
I think if you look at these benefits, it's kind of clear why it's difficult to come with an example. Toy examples are always easy to refactor, read and test, because they can't have any context around them. But I'll give it a go! Let's say I have an event listener which is getting kind of long ... someEmitter.on('someEvent', function (e) {
var aVariable = computeSomething(e)
var someThings = thingsFromEvent(e)
e.forEach(doAThing)
while (somePredicate(aVariable)) {
...
}
...
}) Ideally, I would factor this out into separate functions and register them separately on the event. If ordering of events is important, ideally they'll be in the same function or at least will be put into smaller functions that are run in sequence by a coordinating function. To do this successfully, I need to know how Hope that makes a bit of sense! |
@rjmk thanks! Agree with this example The only criteria is that it will take people from "Oh..." to "Ahah!" 😕 >> 🎉 |
Surely the greatest resource on functional programming in javascript is Professor Frisby's Mostly Adequate Guide to Functional Programming. However, it is functional programming of a more Haskell-y flavour and less of a Lisp-y flavour; which is to say, it focusses a lot on types and the standard abstractions of category theory and is less heavy on nifty map / reduce / filter combos. For that, I like Functional Programming in Javascript, though note that it has a non-standard definition of reduce such that reduce wraps its return value in an array. |
This is the best explanation I've ever seen. https://www.youtube.com/watch?v=dZ41D6LDSBg |
What is a "Pure" Function?
The text was updated successfully, but these errors were encountered: