Skip to content

Latest commit

 

History

History
41 lines (33 loc) · 2.08 KB

File metadata and controls

41 lines (33 loc) · 2.08 KB

Functional Programming

Section Source

Definition: Functional programming is a programming model that builds the structure and elements of programs where computation is the evaluation of mathematical functions while avoiding changing-state and mutable data.

Pure Functions:

  • Purity:
    • returns the same result given the same arguement (aka deterministic)
    • does not cause obeservable side effects
      • exmaple of observable side effects is increasing the count on a global variable by incrementing the variable inside of the function.
    • passes global objects in as parameters instead of calling them directly inside of the function
    • doesn't read external files
    • doesn't rely on random number generation
  • Benefits:
    • code is easier to test
    • doesn't need to mock anything
    • can test with different contexts

Immutability:

  • immutable data cannot change its state after it is created
    • you need to create a new object and assign a new value
  • recursion can be used to keep our variables immutable

Referential Transparency:

  • if a function consistently yields the same result for the same input it is referentially transparent
  • this allows for memoizing the function (optimization)
    • replacing the entire expression with a number constant is memoizing

Node.js Modules and require()

Section Source

Module - spliting node.js code into separate js files and calling on those files when needed.

Require

  • is on the global object in node.js so it can be accessed from anywhere
  • allows functions from other files to be accessed
  • const variableName = require('./filepath'); don't need .js on the end, it will account for it automatically
  • inorder for these file to be accessed in addition to require, they need to be exported module.exports = functionName similarly to React components

Things I want to know more about:

  • writing pure functions, and memonizing -- Hard to understand the benefit with the simple example they displayed.