Skip to content

Instantly share code, notes, and snippets.

@bahmutov bahmutov/index.js
Last active Apr 7, 2016

Embed
What would you like to do?
Is it pure?
// A: is this pure function?
function sum(a, b) { return a + b }
// B: is this a pure function?
const k = 2
function addK(a) { return a + k }
// C: is this a pure function?
function sub(a, b) { return sum(a, -b) }
// D: is this a pure function?
function sub(a, b, summer) { return summer(a, b) }
// E: is this a pure function?
var sum = (a, b) => a + b
function sub(a, b) { return sum(a, -b) }
// F: is this a pure function?
const sum = require('./sum')
function sub(a, b) { return sum(a, -b) }
// G: is this a pure function?
const foo = eval('(function sum(a, b) { return a + b })')
// H: is sub a pure function?
const K = 10
function sub(a, b) {
return function () {
return a - b + K
}
}
// I: is sub a pure function?
// module sum.js
module.exports = {
sum: function sum(a, b) { return a + b }
}
// module index.js
const sumThem = require('./sum').sum
function sub(a, b) {
return sumThem(a - b)
}
@bahmutov

This comment has been minimized.

Copy link
Owner Author

commented Mar 28, 2016

How would one determine which one is pure in JavaScript?

Relevant reading

@gunar

This comment has been minimized.

Copy link

commented Mar 28, 2016

According to @ericelliott, a pure function is a function which:

  1. Given the same input, will always return the same output.
  2. Produces no side effects.
  3. Relies on no external state.

So IMHO,

A. Yes.
B. No, breaks 3.
C. If sum is the same function from A (i.e. also pure), then yes.
D. Only if summer is pure
E. Yes.
F. Only if sum is pure
G. Kill me already. (I'd guess yes)

@atmin

This comment has been minimized.

Copy link

commented Mar 28, 2016

B:
addK is pure, it does not rely on external state. k is a constant, than can be replaced with value in addK without side effects. If k was a dictionary and e.g. k.foo was referred, then addK would be impure

G:
Pure in this case, param to eval is a constant. Undecidable in the general case (I can make-up a case, where you pass something awfully coupled with external state to eval, that still happens to return a pure function).

@vkarpov15

This comment has been minimized.

Copy link

commented Mar 29, 2016

C and D are tricky, because calling a function can be considered a side effect, even if the function being called is pure. C is also technically dependent on external state.

@gunar s definition really depends on your definition of side effect, which is a tricky subject in js because even accessing a property can be an observable interaction because of defineproperty and proxies

@bahmutov

This comment has been minimized.

Copy link
Owner Author

commented Mar 30, 2016

Thanks guys for trying, I think everything can be thought of pure if we rewrite top level sum by substituting external variables with their values. This is why I disagree with everyone at least in case of E: it is NOT pure.

// E: is this a pure function?
var sum = (a, b) => a + b
function sub(a, b) { return sum(a, -b) }
sub(10, 1) // 9
sum = (a,b) => 0
sub(10, 1) // 0
@jethrolarson

This comment has been minimized.

Copy link

commented Mar 30, 2016

Function statements also have two states due to hoisting.

@gunar

This comment has been minimized.

Copy link

commented Mar 30, 2016

@bahmutov damn! Nice catch on E.

@bahmutov

This comment has been minimized.

Copy link
Owner Author

commented Mar 30, 2016

@jethrolarson I assume that every function has been hoisted already and we don't override functions with variables with same name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.