CS/COE 1520 pitt.edu/~ach54/cs1520 Functional programming A - - PowerPoint PPT Presentation

cs coe 1520
SMART_READER_LITE
LIVE PREVIEW

CS/COE 1520 pitt.edu/~ach54/cs1520 Functional programming A - - PowerPoint PPT Presentation

CS/COE 1520 pitt.edu/~ach54/cs1520 Functional programming A challenge: Rewrite the following without using a loop: var data = [1, 2, 3, 4]; for (var i = 0; i < data.length; i++) { alert("loopin! " + data[i]); } 2 A


slide-1
SLIDE 1

CS/COE 1520

pitt.edu/~ach54/cs1520

Functional programming

slide-2
SLIDE 2
  • Rewrite the following without using a loop:

var data = [1, 2, 3, 4]; for (var i = 0; i < data.length; i++) { alert("loopin! " + data[i]); }

A challenge:

2

slide-3
SLIDE 3
  • Functional programming

○ Data stored in the program should be immutable ○ Programs should run in a stateless manner

A different way of programming

3

slide-4
SLIDE 4
  • Building blocks of functional programs
  • Should be idempotent

○ Operate free from their timing relative to other operations

  • Should be free of side-effects

○ Example side effects: ■ Mutate any shared state or mutable arguments

  • Covered by having immutable data...

■ Don’t produce any observable output, e.g.,

  • Thrown exceptions
  • Triggered events
  • I/O to devices
  • I/O to display
  • Writes to a log

■ Note that this means our programs won't be composed entirely of pure functions

Pure functions

4

slide-5
SLIDE 5
  • Your functions should never rely on outside values

Operate only on data passed in as arguments

  • All of your functions must accept at least one argument
  • All of your functions must return data or another function
  • No loops

Guidelines for functional programming

5

slide-6
SLIDE 6

[ { "name": "Crosby", "age": 28, "points": [0, 0, 0, 1, 1, 0] }, { "name": "Malkin", "age": 29, "points": [1, 1, 0, 0, 0, 0] }, { "name": "Letang", "age": 29, "points": [1, 0, 1, 0, 1] } ]

Let's consider getting this data from a server

6

slide-7
SLIDE 7

Imperative approach

  • vs-

Functional approach

Grab each player's average points per game

7

slide-8
SLIDE 8
  • Array.reduce(callback [, initialValue])

○ callback is a reference to a function that will be called for every item in the array being reduced ■ Will be passed 4 arguments:

  • The value previously returned in the last invocation of the

callback, or initialValue, if supplied.

  • The current value of the array being processed
  • The index of the current element being processed
  • The array on which reduce was called

■ What should our callback be if we wanted to use reduce() instead of totalAcrossArray()?

DRYing totalAcrossArray()

8

slide-9
SLIDE 9
  • Array.map(callback [, thisArg])

○ callback is, again, a reference to a function that will be called for every item in the array being reduced ■ However, in this case, the result produced by each call is added into a result array instead of being passed to future calls

  • Hence, is only passed 3 arguments:

○ Current value ○ Current index ○ Array

○ thisArg allows you to set what should be referenced by the this keyword within callback ○ What callback could we use to replace avgAllSubarrays()?

DRYing avgAllSubarrays()

9

slide-10
SLIDE 10
  • How?

DRYing grab()

10

slide-11
SLIDE 11
  • What will this code output?

var ex = []; for (var i = 0; i < 5; i++) { ex[i] = function() { document.write(i); }; } for (var j = 0; j < 5; j++) { ex[j](); }

  • How can we get it to behave as intended?

Another challenge

11

slide-12
SLIDE 12

var ex = []; function funcMaker(i) { return function() { document.write(i); }; } for (var i = 0; i < 5; i++) { ex[i] = funcMaker(i); } for (var j = 0; j < 5; j++) { ex[j](); }

Closure example

12

slide-13
SLIDE 13
  • For a language designed around functional programming:

○ Haskell

This was only a brief intro to functional programming

13