the beauty and joy of computing lecture #19 higher order functions ii instructor: sean morris...

16
The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not www.theatlanticcities.com/neighborhoods/2013/07/how-twitter-can-predict-your-communitys- wellbeing/6270 "training," "gym," "waves," "mountains," "camping", "ideas," "suggestions," "advice," "meetings," "conferences," "support," "donate"

Upload: monique-botts

Post on 29-Mar-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

The Beauty and Joy of Computing

Lecture #19Higher Order Functions II

Instructor:Sean Morris

Twitter Can Tell Whether Your Community Is Happy or Not

www.theatlanticcities.com/neighborhoods/2013/07/how-twitter-can-predict-your-communitys-wellbeing/6270

"training," "gym," "waves," "mountains," "camping", "ideas," "suggestions," "advice," "meetings," "conferences," "support," "donate"

Page 2: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (2)

Garcia

Review useful HOFs map Reporter over List

Report a new list, every element E of List becoming Reporter(E)

keep items such that Predicate from List Report a new list, keeping only elements E of List if Predicate(E)

combine with Reporter over List Combine all the elements of List with Reporter(E)

This is also known as “reduce”

Acronym example keep map combine

The script, run and launch For each Compose example

Today

Page 3: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (3)

Garcia

Pair this out …

Composing Higher-Order Functions, report this…

Page 4: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (4)

Garcia

What would this report:

a) UCB, as beforeb)UoCaBc) UCaBd)UoCBe) Something else

Peer Instruction

Page 5: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (5)

Garcia

Pair this out …

What does the Sprite do?a) Say : “ “, “ “ , “ “b) Say : “Tweet 1”, “Tweet 1”, “Tweet 1”c) Say : “Tweet 3”, “Tweet 3”, “Tweet 3”d) Say : “Tweet 1”, “Tweet 2”, “Tweet 3”

Page 6: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (6)

Garcia

Go Deep …

What makes “for each” a High-Order Function?

Page 7: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (7)

Garcia

Go Deeper …

Explain the difference between the Higher-Order function, “map”, and the Higher-Order function “for each”.

Page 8: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (8)

Garcia

Higher-Order Functions so far…

Consumers of functions …

Page 9: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (9)

Garcia

Cascade: Pair this out

What is reported?a) 2b) 16c) 60d) 80

Page 10: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (10)

Garcia

Cascade: Check it out

Page 11: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (11)

Garcia

Compose: Pair this out

What does the Sprite say?a) Nothingb) Build Your Own Blocksc) Buildd) Youre) Ownf) Blocks

Page 12: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (12)

Garcia

Compose : Returning Functions

Returns a function f(g(x))

Page 13: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (13)

Garcia

Compose : Another Example

Page 14: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (14)

Garcia

Compose : Try this …

A screensaver? A circle starts with a radius of 1 cm and expands as a function of time – doubles its radius every second. Using “compose,” write the function that will calculate the circle’s area at any given time.

What are the two functions?

Page 15: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (15)

Garcia

Write your own : Try this …

Page 16: The Beauty and Joy of Computing Lecture #19 Higher Order Functions II Instructor: Sean Morris Twitter Can Tell Whether Your Community Is Happy or Not

UC Berkeley “The Beauty and Joy of Computing” : Higher-Order Functions II (16)

Garcia

Functions as data

Lambda