1 of 53

Week 07 and Week 08

Functional Programming

2 of 53

Functions as Parameters

Array filter - Array map - Array reduce

3 of 53

Functional Programming

Functional Programming /ˈfəNG(k)SH(ə)n(ə)l/ /ˈprōˌɡramiNG/ noun

  • a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data through the use of pure and idempotent functions

Pure Function /pyo͝or/ /ˈfəNG(k)SH(ə)n/ noun

  • a pure function is one where the return value is only determined by its input values, without observable side effects

Idempotent Function /ˌīdemˈpōt(ə)nt/ /ˈfəNG(k)SH(ə)n/ noun

  • an idempotent function is one that has no additional effect if it is called more than once with the same input parameters

4 of 53

Functional Programming

5 of 53

Functional Programming

6 of 53

Functional Programming - Check In

  1. What is a pure function?
  2. What is the primary thing that functional programming seeks to avoid?
  3. Given the code below, is the trimLeft() function pure?
  4. Given the code below, is the trimLeft() function idempotent?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

function trimLeft(str) {

let arr = str.split('')

let i = 0

while (arr[i] === ' ') {

arr.shift()

i++

}

return arr.join('')

}

7 of 53

Array.filter( () => {} )

New Arrays by Filtering Other Arrays

8 of 53

Functional Programming

9 of 53

Functional Programming

10 of 53

Functional Programming

11 of 53

Functional Programming

12 of 53

Filter with Array.filter()

13 of 53

Filter with Array.filter()

14 of 53

Filter with Array.filter()

15 of 53

Functional Programming

16 of 53

Functional Programming

17 of 53

Check In

  • What is a predicate function?
  • What is a higher order function?
  • Given the code below, what will be the value stored in retVal?
  • After the code below runs, what will be the value stored in values?

1

2

3

4

5

6

7

8

9

function isNumeric(n) {

return !isNaN(n)

}

const values = [1, '2', 'a', NaN, null]

const retVal = values.filter(isNumeric)

18 of 53

Array `map`

Mapping Over an Array

19 of 53

Array.map( () => {} )

20 of 53

Array.map( () => {} )

21 of 53

Array.map( () => {} )

22 of 53

Array.map( () => {} )

23 of 53

Array.map( () => {} )

24 of 53

Array.map( () => {} )

25 of 53

Array.map( () => {} )

26 of 53

Array.map( () => {} )

27 of 53

Array.map( () => {} )

28 of 53

Check In

  • What is a higher order function?
  • What is an idempotent function?
  • Given the code below, what will be the value stored in retVal?
  • After the code below runs, what will be the value stored in values?

1

2

3

4

5

6

7

8

9

function halve(n) {

return n / 1

}

const values = [2, 4, 6, 8]

const retVal = values.map(halve)

29 of 53

Hands On

Calculating the subtotal of each item. This exercise will be completed in class.

  • Create a new branch named answer to do your work in
  • Run npm install to have all dependencies installed and then run npm test to see the failing tests
  • Use what you have learned about functional programming to implement the calculateSubTotals() function to make the tests pass
  • Commit your changes, push your branch up to GitHub, and create a pull request
  • Request review from the instructor on your PR

30 of 53

Array.reduce( () => {} )

A Single Value from an Array

31 of 53

Array.reduce( () => {} )

32 of 53

Array.reduce( () => {} )

33 of 53

Array.reduce( () => {} )

34 of 53

Array.reduce( () => {} )

35 of 53

Array.reduce( () => {} )

36 of 53

Array.reduce( () => {} )

37 of 53

Check In

  1. What is a pure function?
  2. What is an idempotent function?
  3. What is a higher order function?
  4. What is the primary thing that functional programming seeks to avoid?

38 of 53

Chaining

Less Array Code with Chaining

39 of 53

Less Code by Chaining

40 of 53

Less Code by Chaining

41 of 53

Less Code by Chaining

42 of 53

Less Code by Chaining

43 of 53

Less Code by Chaining

44 of 53

Less Code by Chaining

45 of 53

Less Code by Chaining

46 of 53

Less Code by Chaining

47 of 53

Less Code by Chaining

48 of 53

Hands On

Calculating the class average by assignment. This exercise will be completed in class.

  • If you are a driver, create a new repo named head-of-the-class in GitHub and make your instructor and your navigator a collaborator
  • Once your instructor has pushed you code, clone your repo locally
  • Create a new branch named answer to do your work in
  • Run npm install to have all dependencies installed and then run npm test to see the failing tests
  • Use what you have learned about functional programming to implement the calculateAverageByAssignment() function to make the tests pass
  • Commit your changes, push your branch up to GitHub, and create a pull request
  • Request review from the instructor on your PR

49 of 53

Homework

Using functional programming create a lineup validation function

  • Create a new repo named perfect-lineup in GitHub and make your instructor a collaborator
  • Once your instructor has pushed you code, clone your repo locally
  • Create a new branch named answer to do your work in
  • Use what you have learned about functional programming to implement the validateLineup() function according to the rules provided
  • Commit your changes, push your branch up to GitHub, and create a pull request
  • Request review from the instructor on your PR

50 of 53

Resources

51 of 53

Vocabulary

  • Functional Programming: a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data through the use of pure and idempotent functions
  • Pure Function: a function where the return value is only determined by its input values, without observable side effects
  • Idempotent Function: a function that has no additional effect if it is called more than once with the same input parameters
  • Higher Order Function: a function that takes another function as a parameter
  • Filter Function: a higher order function that filters an array of values by a predicate function. If the predicate function returns true, the value passes through the filter. If it returns false, the value is filtered out of the array.

52 of 53

Vocabulary

  • Anonymous Function: In Javascript, an anonymous function is one declared without a named function identifier, using the syntax (arguments) => function body
  • Fat arrow: refers to this: => syntax used in anonymous functions
  • Find function: a higher order function that finds a value from an array according to a predicate function. The function returns the first value in the array for which the predicate returns true.
  • Map function: a higher order function that applies the function passed to it to each value in an array, and returns a new array of the transformed values.
  • Function Chaining: calling a function immediately on the return value of a different function

53 of 53

Vocabulary

  • Reduce Function: a higher order function that takes an array, an inner function, and optionally a starting “accumulator” value. The inner function takes the current accumulator value (or undefined) and the next array value (starting with the first item in the array). The return value of this function becomes the next accumulator value.
  • DRY: Don’t Repeat Yourself. A rule of thumb for writing clean and maintainable code