PYPL Index Shows Top 3: Python, Java, JavaScript
“The PYPL PopularitY of Programming Language Index is created by analyzing how often language tutorials are searched on Google”
The Beauty and Joy of Computing��Python�Higher-Order Functions�
https://pypl.github.io/PYPL.html
CS10 TA
Marius Castro
(Baby version)
Garcia
The Beauty and Joy of Computing : Python IV HOFs (1)
Higher Order Functions…�Why? Basics…
Garcia
The Beauty and Joy of Computing : Python IV HOFs (2)
Why Use Functions? (review)
Calling it…
Garcia
The Beauty and Joy of Computing : Python IV HOFs (3)
Garcia
The Beauty and Joy of Computing : Python IV HOFs (3)
Why Use Functions? (review, in Python)
Calling it…
pd()
for _ in range(4):
fd(25)
rt(90)
pu()
pd()
for _ in range(4):
fd(100)
rt(90)
pu()
pd()
for _ in range(4):
fd(200)
rt(90)
pu()
def draw_square(length): � pd()
for _ in range(4):
fd(length)
rt(90)
pu()
draw_square(100)
from turtle import *
Garcia
The Beauty and Joy of Computing : Python IV HOFs (4)
Why Use Higher-Order Functions?
Calling it…
Garcia
The Beauty and Joy of Computing : Python IV HOFs (5)
Why Use Higher-Order Functions? (Python)
for _ in range(4):
draw_line(100)
rt(90)
for _ in range(4):
draw_dashed_line(100)
rt(90)
for _ in range(4):
draw_wiggly_line(100)
rt(90)
def draw_100_square(line_drawer): � pd()
for _ in range(4):
line_drawer(100)
rt(90)
pu()
Calling it…
draw_100_square(draw_wiggly_line)
from turtle import *
Garcia
The Beauty and Joy of Computing : Python IV HOFs (6)
Why HOFs are like Pregnant Fish, Sharks
Source: Brian Harvey, Wikipedia (Fbattail, Aka, Evdaimon)
Data (e.g,. Sentences, Words, Booleans, Lists)
Data (e.g,. Sentences, Words, Booleans, Lists)
Data (e.g,. Sentences, Words, Booleans, Lists)
Normal �Fish
Data (e.g,. Sentences, Words, Booleans, Lists)
Functions
Pregnant�Fish
Sharks
Functions
Pregnant
Sharks
Functions
Functions
Garcia
The Beauty and Joy of Computing : Python IV HOFs (7)
HOFs you have seen before
Garcia
The Beauty and Joy of Computing : Python IV HOFs (8)
How combine works:
a
b
c
d
f(_,_) [‘a’,’b’,’c’,’d’]
reduce(Combiner, theList)
a b
f(a,b) c
f(f(a,b),c) d
f(f(f(a,b),c),d)
Garcia
The Beauty and Joy of Computing : Python IV HOFs (9)
combine above the abstraction line
Your f should be associative, and work if it’s
a f b f c f d
combine using f
((a f b) f (c f d))
or…
(((a f b) f c) f d)
etc…
(a b c d)
🗹
Garcia
The Beauty and Joy of Computing : Python IV HOFs (10)
(Cal) Clicker Question
What is reported here?
def joinswap(L, R):
return R + L
reduce(joinswap, "abcd")
Garcia
The Beauty and Joy of Computing : Python IV HOFs (11)
Garcia
The Beauty and Joy of Computing : Python IV HOFs (11)
Acronym
Garcia
The Beauty and Joy of Computing : Python IV HOFs (12)
Acronym Algorithm
(the Beauty and Joy of Computing)
keep only �uppercase words
(Beauty Joy Computing)
map first letter only
(B J C)
combine into one�word (join)
BJC
Garcia
The Beauty and Joy of Computing : Python IV HOFs (13)
Garcia
The Beauty and Joy of Computing : Python IV HOFs (13)
Acronym (uses map, keep, combine)
🗹
def Acronym(phrase):
return ''.join([w[0] for w in phrase.split() if w < 'a'])
Garcia
The Beauty and Joy of Computing : Python IV HOFs (14)
Garcia
The Beauty and Joy of Computing : Python IV HOFs (14)
(Cal) Clicker Question
What is reported if I swap keep and map in Acronym and call it on (the Beauty and Joy of Computing)?
def Acronym(phrase):
return ''.join([l for l in [w[0] for w in phrase.split()] if l < 'a'])
Garcia
The Beauty and Joy of Computing : Python IV HOFs (15)
Garcia
The Beauty and Joy of Computing : Python IV HOFs (15)
HOF Tools & Demo
Garcia
The Beauty and Joy of Computing : Python IV HOFs (16)
HOF tools, sharks, pregnant fish, mymap
🗹
lambda
Garcia
The Beauty and Joy of Computing : Python IV HOFs (17)
Frankenstein … can we get laclacc?
Garcia
The Beauty and Joy of Computing : Python IV HOFs (18)