1 of 18

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 ComputingPython�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)

2 of 18

Higher Order Functions…�Why? Basics…

Garcia

The Beauty and Joy of Computing : Python IV HOFs (2)

3 of 18

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)

4 of 18

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)

5 of 18

Why Use Higher-Order Functions?

Calling it…

Garcia

The Beauty and Joy of Computing : Python IV HOFs (5)

6 of 18

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)

7 of 18

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)

8 of 18

HOFs you have seen before

  • [Reporter(e) for e in theList if Predicate(e)]
    • map Reporter over List
      • Report a new list, every element E of theList �becoming Reporter(E)
    • keep items Predicate from List
      • Report a new list, keeping only elements E of� theList if Predicate(E)

  • from functools import reduce
  • reduce(Combiner, theList)
    • combine List using Combiner
      • Combine all the elements of List with �Combiner(E1, E2) for elements E1,E2 of theList

Garcia

The Beauty and Joy of Computing : Python IV HOFs (8)

9 of 18

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)

10 of 18

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)

11 of 18

(Cal) Clicker Question

What is reported here?

  1. abcd
  2. acdb
  3. bdac
  4. dcba
  5. (nothing)

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)

12 of 18

Acronym

Garcia

The Beauty and Joy of Computing : Python IV HOFs (12)

13 of 18

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)

14 of 18

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)

15 of 18

(Cal) Clicker Question

What is reported if I swap keep and map in Acronym and call it on (the Beauty and Joy of Computing)?

  1. bjc
  2. BJC
  3. CJB
  4. (nothing)
  5. Error

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)

16 of 18

HOF Tools & Demo

Garcia

The Beauty and Joy of Computing : Python IV HOFs (16)

17 of 18

HOF tools, sharks, pregnant fish, mymap

🗹

lambda

Garcia

The Beauty and Joy of Computing : Python IV HOFs (17)

18 of 18

Frankenstein … can we get laclacc?

Garcia

The Beauty and Joy of Computing : Python IV HOFs (18)