Functional JS Flashcards

1
Q

Immutability

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

map()

A

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 } );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

forEach()

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

filter()

A

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 } );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

reduce()

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

find()

A

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 } );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly