0:14

[BLANK_AUDIO]

Â In this video, I'm going to talk about higher-order functions.

Â So what is a higher-order function?

Â Well, it's a function that can take other

Â functions as arguments or can even return functions.

Â Huh.

Â Right, that's interesting, okay?

Â What does that mean exactly?

Â Well, the higher-order function, now, it gets a function

Â as an argument and then it can call that function.

Â This allows it to implement much more sophisticated behaviors.

Â So this is a very powerful concept that

Â allows me to write much more interesting programs.

Â Okay, and instead of repeating a bunch of similar

Â behaviors where you do slightly different things, instead you

Â can encapsulate this into one higher-order function, and then

Â modify its behavior by passing it in different functions.

Â 1:07

We know how to build functions, so let's build some simple functions.

Â I'm going to build a function double.

Â Okay, and that returns double of its input.

Â And, I want to return, I want to write a function

Â called square, that squares its input, okay?

Â And we know how these work, so let's say print double.

Â I gotta type print correctly.

Â [LAUGH] Double three, print square three.

Â Alright, let's run this.

Â Nothing surprising here.

Â Okay, I doubled three.

Â I squared three.

Â Woo hoo.

Â Great, I know how to write a function.

Â Now, let's write a higher order function.

Â Twice, func, val, and what

Â I want this thing to do is to

Â return func of func of val.

Â All right.

Â Why is this a higher-order function?

Â Well, a higher-order function either takes a

Â function as an argument, or returns a function.

Â 2:14

This function here is taking a function as an argument.

Â That first argument, func, I am expecting you to pass me a function.

Â Alright?

Â And you can see what's going to happen.

Â If I pass you a function, I'm actually going to call it twice.

Â I'm going to call func on val.

Â I'm going to take the result, and I'm going to call func on the result.

Â So let's actually try this.

Â And let's see what happens.

Â Print twice.

Â And what do I want to do twice?

Â I want to double something twice.

Â And lets double the number three twice, okay?

Â Think about what you, what you expect to happen here.

Â I'm going to call the function double on three and

Â then I'm going to call the function double on its result.

Â 3:00

Okay.

Â Do not just watch by and say, oh yeah, that makes sense.

Â No problem.

Â No, no, no.

Â Okay?

Â I passed a function, double, as an argument, okay?

Â It now has a new name.

Â Its now called func, not double and I

Â actually called it twice inside of this function.

Â So why did I define twice?

Â That's kind of stupid, I could have just,

Â you know, called double twice instead of doing this.

Â But look what I can do now.

Â 3:25

Let's square three.

Â I now have the ability where I can call any function twice, right?

Â So did that work?

Â Square three is nine, square of that is 81.

Â Yes it does.

Â So I now have a new function twice, that takes any function you want and

Â calls it, and then calls it again on the result of the first call, okay?

Â Of course, this function has to take a single argument, but

Â other than that, you know, we've got an interesting situation here.

Â Now.

Â Right, now that we understand that we can use functions as arguments,

Â let's think of, look at some other things that we can do.

Â Let's imagine I have a list of data, 1, 3, 6, 9, 18.

Â Okay, and let's imagine that I want to double everything in there.

Â Well, I know one way of doing that.

Â I can say newdata equals, let's use a

Â list comprehension, double item for item in data.

Â 4:22

Print newdata.

Â Let's see what happens there.

Â Okay, when I run that, all right, I get a list with 2, 6, 12, 18, 36.

Â No surprise.

Â Okay, I know how to do that.

Â And now I have a list with everything doubled in it.

Â Well, this is such a common idiom, there's actually a function in

Â Python and many other languages, too, that allows me to do this.

Â I'll say newdata2 equals map, alright?

Â That's the name of the function, and it

Â takes a function as an argument and a list.

Â Alright, so what's going to happen is map

Â takes this, this first argument, this function, and it

Â applies it to every element in the list and

Â creates a new list with all the results, right?

Â Just like that list comprehension did, and let's print newdata2 and let's look at it.

Â Alright.

Â I got 2, 6, 12, 18, 36, as I would expect.

Â I can pass different functions in here to map.

Â Let's pass square, and see what happens.

Â 5:15

Alright?

Â Now I have 1, 9, 36, 81, 324.

Â Alright, that looks like it worked.

Â Okay, so map is a powerful function that allows you to manipulate lists, right?

Â By applying a particular function to every element

Â to the list to create a new list, right?

Â This is very similar to what I could do with a list comprehension, alright?

Â Except that I need to use a function, whereas

Â in list comprehension I could just use an expression.

Â 5:39

Alright, there's another function like this called filter.

Â Alright, let's define a new function, even, if val percent

Â two, then return false because it's odd, else return true.

Â Now I have a function that returns true if something's even, false otherwise.

Â Let's look at newdata3 equals filter, even, data, and see what happens.

Â Print newdata3.

Â 6:17

Okay, I do that.

Â I'm going to run it.

Â I want you to think about what you think might happen.

Â I'm taking a function in a list.

Â Map maps a function onto every element of the list.

Â Filter is going to filter somehow.

Â Alright, what does that mean?

Â Alright, it only keeps the things that were true.

Â Right?

Â So newdata3 is all of the elements of

Â data that, for which the function even returned true.

Â Okay, so between map and filter, I can filter

Â a list by only getting things that satisfy some predicate.

Â And by map, I can, you know, manipulate every element of the list.

Â 6:54

Okay, so you've seen how map and filter are

Â actually built-in functions in Python that are higher-order functions.

Â They allow you to do some interesting things to lists.

Â But that's not all I can do with higher-order functions.

Â I can pretty much do anything I want.

Â So let's look at this function called area.

Â I want to write a function that computes the area under

Â a curve between two boundaries, a low and a high boundary.

Â Alright, so I wrote a function called area.

Â It takes four arguments.

Â The first is the function that I'm trying to compute the area of, then a lower

Â bound and upper bounds, low and high, and

Â a step size because I'm going to actually approximate this.

Â Okay, what this function does is it starts at the lower bound and it increments the

Â current location by the step size again and

Â again until you hit the upper bound, or high.

Â Alright, and what I do is I evaluate the function, and I

Â approximate the area under that curve as a rectangle of width step size.

Â Okay, so I evaluate the function, multiply it by step size,

Â take that rectangular area, I add it in to the total.

Â I keep doing this, and when I've hit the upper

Â bound, or high, I am done, and I return the total.

Â All right?

Â Let's try this out.

Â Okay, let's define some functions.

Â Let's define f of x, that just returns x.

Â So that's basically f of x equals x, so my g of x is return x squared.

Â So that's g of x equals x squared.

Â And let's, let's see if we can print out the area.

Â The area of f from zero to ten, with a step size of 0.01.

Â 8:24

Let's run that.

Â Returns 50.

Â All right.

Â For those of you who know, who know calculus, you should be

Â able to tell me immediately what the area under that curve is.

Â Even if you don't know calculus, you should still be able

Â to tell me immediately what the area under that curve is.

Â Right?

Â It's a triangle.

Â f of x equals x from zero to ten, all right, is a triangle with side ten.

Â You know?

Â The base length is ten.

Â The height is ten.

Â And so it's one-half times base times height, 50.

Â Alright, well, I got a reasonable approximation there.

Â It's a little bit off, because, well, I'm approximating.

Â Right?

Â Let's print the area of g from zero to ten.

Â Same step size.

Â Okay.

Â And 333.

Â Again, those of you who know calculus, you should be able to calculate that out

Â fairly easily, and figure out that 333 and a third is pretty much the answer here.

Â And again, there's some error, because this is an approximate way.

Â And I can reduce the error by changing the step size.

Â 9:20

Now, I don't have to have nice functions here.

Â Okay, let's define h of x.

Â All right, this function, if x is less than 3, let's

Â return x, elif x is less than seven, let's return x

Â squared, else, let's return seven times x minus four.

Â 9:48

This function, from zero to ten, with, hm, let's use that step size.

Â Okay, and oops.

Â I did not type this right.

Â The area from zero to ten with that step size, lets run.

Â And I get 276.3.

Â Is that correct?

Â I have no idea.

Â I leave it to you to calculate it and see if it actually did the right thing.

Â Okay?

Â So I can create functions like this

Â that take other functions and manipulate them or

Â evaluate them at certain locations to do interesting

Â things like compute the area under that function.

Â 10:29

Hopefully after watching this video, you have

Â an appreciation of higher ardor, order functions.

Â We can write functions that use other functions, right?

Â This is how I can write that area function where

Â I can take the area under an arbitrary curve, right?

Â All I have to do is pass it a function.

Â This is such a powerful concept.

Â There are many built-in functions in Python

Â that do this, right, like map and filter.

Â They use these functions in a very

Â well-defined way to accomplish specific behaviors, right?

Â But now you also know how you can write your own higher-order functions.

Â So, this is a new tool in your toolbox

Â that will allow you to write much more sophisticated programs.

Â