Skip to content
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

Add Explanation of Pure vs. Impure Functions #16

Open
nelsonic opened this issue Jan 12, 2016 · 5 comments
Open

Add Explanation of Pure vs. Impure Functions #16

nelsonic opened this issue Jan 12, 2016 · 5 comments

Comments

@nelsonic
Copy link
Member

What is a "Pure" Function?

@nelsonic
Copy link
Member Author

@rjmk care to enlighten us with a practical example of why functional purity is obviously betterer...? 😉

@rjmk
Copy link

rjmk commented Jan 21, 2016

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:

  • Ease of testing
  • Ease of refactoring
  • Ease of reading

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 aVariable might be changing at different points in the program. If the functions are implicitly relying on some external state (via a closure or whatever) I have to know if anything is changing that state to know about the order things should run in. That forEach looks scary! Who knows what's happening as that runs? Certainly it's changing something, so I'm going to need to track down doAThing and hold its actions in my head.

Hope that makes a bit of sense!

@nelsonic
Copy link
Member Author

@rjmk thanks! Agree with this example
I'm already sold on purity...
purity
As a means to eliminate unwanted side-effects and thus dramatically simplify codebase.
I was just wondering if in the course of the last few months of writing functional JS for your projects, you have come across a good beginner example or thunk of one yourself...

The only criteria is that it will take people from "Oh..." to "Ahah!" 😕 >> 🎉

ahah_moment

@rjmk
Copy link

rjmk commented Jan 21, 2016

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.

@yusufyildirim
Copy link

yusufyildirim commented Apr 23, 2017

This is the best explanation I've ever seen. https://www.youtube.com/watch?v=dZ41D6LDSBg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants