Skip to content

Instantly share code, notes, and snippets.

@bahmutov
Last active April 7, 2016 14:08
Show Gist options
  • Save bahmutov/1ec7a573ce5771caa9c7 to your computer and use it in GitHub Desktop.
Save bahmutov/1ec7a573ce5771caa9c7 to your computer and use it in GitHub Desktop.
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)
}
@jethrolarson
Copy link

Function statements also have two states due to hoisting.

@gunar
Copy link

gunar commented Mar 30, 2016

@bahmutov damn! Nice catch on E.

@bahmutov
Copy link
Author

@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