Skip to content

Instantly share code, notes, and snippets.

@simeg

simeg/fp_js_1.js Secret

Last active May 23, 2022 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simeg/22d08498123864fd789dfea64a6ab8c5 to your computer and use it in GitHub Desktop.
Save simeg/22d08498123864fd789dfea64a6ab8c5 to your computer and use it in GitHub Desktop.
FP in JS 1
// This function is pure because it's determinisic.
// It has no side effects.
// Nothing outside the function can influence the output.
// addTen(5) will _always_ be 15
const addTen = input => {
return input + 10;
};
// Fetch the number from the database
const numberFromDb = DB.getNumber();
// This function is not pure because it's not deterministic.
// It has a side effect (the numberFromDb value).
// We cannot know for sure that the outcome will be the same
// every time we call it, and that's why it's not pure
const addWithNumberFromDb = input => {
return input + numberFromDb;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment