Functional JS Flashcards
Immutability
Core concept in functional programming. The idea is the object, list, string, etc. you are dealing with should be treated as immutable in the sense that any update to the value should be a modified new copy of the original value. Never should the original value be modified.
map()
An array/list method that takes in a list, uses the callback function to create a new value for each element in the list, and returns a new list with all of the modified values inside.
NOTE: the original list is never modified
ex. map((elem) => { return elem *2 } );
forEach()
This is basically just an iterator for the list that calls it. It does not return anything and only allows for iteration over a list of elements where you can use their values in some meaningful way.
filter()
Use this to filter out elements that meet a specific criteria. It returns a copy of the original list with the filtered elements removed. Callback needs to evaluate to a boolean.
ex. filter((elem) => { return elem === 1 } );
reduce()
Basically ‘fold’ in OCaml
This function uses an accumulator, and updates the accumulator during the callback fn. So it is very useful for reducing data into a list of character counts, or even as an alternative filtering method.
ex. reduce((head, acc) => acc + head , 0);
find()
This is like filter, except it will return the first element from the list that satisfies the property provided in the callback fn.
ex. find((elem) => { elem > 10 } );