Functional programs
PURE FUNCTIONS
Pure functions are functions that have no side-effects.They don't assign to any outside variables, they don't consume input, they don't produce output, they don't read from or writ to a database, they don't modify the parameters they're passed, etc.
The basic idea is that, if you call a function with the same inputs over and over again, you alwyas get the same result.
You can certainly do things with impure functions(and you must, if your program is going to do amything interesting), but for the most part you'll want to keep most of your funtions pure.
IMMUTABILITY
"Immutable" mean "unchangeable".
Immutability goes hand-in-hand with pure functions. Since pure functions aren't allowed to have side-effects, they aren't allowed
to change outside data structures.They are forced to work with data in an immutable way.
Replace loops with collection-iteration functions
(1)FOREACH
1 | // replace this: |
(2) MAP
Like `forEach`, `map` applies a function to each element of an array.However, unlike `forEach`, map collects the results of aaplying the function into a new array and returns it.
1 | map(x => x * 2, [1,2,3]) // -->[2,4,6] |
(3) FILTER/REJECT
1 | const isEven = x => x%2 ===0 |
filter