dwyl / learn-redux

:boom: Comprehensive Notes for Learning (how to use) Redux to manage state in your Web/Mobile (React.js) Apps.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add Explanation of Pure vs. Impure Functions

nelsonic opened this issue · comments

What is a "Pure" Function?

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

commented

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!

@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

commented

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