Skip to content

Instantly share code, notes, and snippets.

@dnasca
Created September 23, 2016 20:58
Show Gist options
  • Save dnasca/13d1a1f986090fcd0f936b1892ea83f2 to your computer and use it in GitHub Desktop.
Save dnasca/13d1a1f986090fcd0f936b1892ea83f2 to your computer and use it in GitHub Desktop.
Pure Function
// pure function
// evaluates the same result given the same args
// doesn't depend on and doesn't modify the states of variables outside it's scope
// no side effects (mutations, async reqs)
var foods = ['beans', 'rice', 'pizza', 'pineapple']
// slice (shallow modified copy of array) vs splice (modifies original array)
foods.slice(0,3) //returns ['beans', 'rice', 'pizza']
foods.slice(0,3) //returns ['beans', 'rice', 'pizza']
foods.slice(0,3) //returns ['beans', 'rice', 'pizza'] - return predictable
foods.splice(0,3) //returns ['beans', 'rice', 'pizza']
foods.splice(0,3) //returns ['pineapple']
foods.splice(0,3) //returns [] - same args, different results. impure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment