Functional Programming Concepts
- What is functional programming?
- from https://medium.com/the-renaissance-developer/concepts-of-functional-programming-in-javascript-6bc84220d2aa: "a style of building the structure and elements of computer programs — that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data"
- What is a pure function and how do we know if something is a pure function?
- The function return values that are the same for identical arguments.
- Impure function: eg a function that uses a global object in its calculations that is not passed in as a parameter of the function
- The function does not cause any side-effects
- What are the benefits of a pure function?
- testing code is easier
- can do unit testing on pure functions in different contexts
- What is immutability?
- inability to be changed
- the function shouldnt alter the original value of the argument passed in.
- eg: value + 2, if value is passed in as an argument and needs to be incremented
- What is Referential transparency?
- a function that consistently returns the same result given the same input as arguments
- requires a pure function and immutibility to be referentially transparent
Node JS Tutorial for Beginners #6 - Modules and require()
- What is a module?
- a logical section of code that can be called on when it is needed to execute code
- What does the word ‘require’ do?
- allows you to import/read JS modules that exist in separate files
- it reads the file, executes the files contents, and then returns the exports object
- How do we bring another module into the file the we are working in?
- with require('file path')
- dont have to put .js on the end of the file
- eg
let functionName = require(filePath);
- What do we have to do to make a module available?
- export the file to make it available to other files with the 'export' keyword to make it available for require/import
module.exports = <function you want to be available outside fof the module>