Currying5:19 with Kenneth Love
Currying isn't making a delicious meal, it's creating new versions of a function until all of the conditions are satisfied.
Currying is a technique you don't come across in Python very often. But, thanks to lambdas, we can implement it pretty easily. The example I used comes from a site that is sadly no longer available.
So now, let's talk about currying. 0:00 So currying is a little bit weird. 0:03 And I have to admit, I could not come up with a good currying example on my own. 0:06 So I borrowed this one off the Internet. 0:09 I'm gonna link to the original doc where I found it. 0:11 But the idea of currying is that you can call a function and that function, if it 0:14 doesn't have enough arguments, will fill in as many arguments as it can to itself. 0:18 And send you back a new version of itself with those arguments already filled in. 0:23 And then you can put in new ones after that. 0:26 So let's write the function real quick, we're gonna call this curried_f. 0:30 And it's gonna take x, y can be None and z can be None. 0:34 And then inside here we're gonna have a function called f that takes x, y and 0:39 z or zed. 0:43 And returns x cubed plus y squared plus z, just as it normally is. 0:45 Okay, so f is our real function. 0:53 Curried_f is the curried version of f, okay. 0:57 If y is not None, so somebody gave us y and z is not None. 1:00 Now they have to give us x. 1:06 Then we're gonna return f(x, y, z), okay. 1:08 So we're gonna actually return, just the function like normal. 1:12 If y is not None which means that z is, 1:16 then we're gonna return a lambda where we're looking for an argument named z. 1:20 And then we're gonna call f(x, y, z). 1:26 Okay, so we're getting back a lambda which is a function. 1:30 And that lambda is looking for one argument which we're gonna call z or zed. 1:33 And then we're gonna pass that to f and call f like normal. 1:36 And if neither of those ifs happen, then we're gonna make a new lambda 1:41 that's looking for y and where z is already equal to None. 1:46 And then, hey look here's how you make a lambda on more than one line. 1:51 We're going to return f(x, y, z), 1:55 if y is not None and z is not None. 2:00 So, when this lambda gets called, if y and 2:05 z are filled in, return the function like normal. 2:08 Otherwise, we're gonna return the lambda where we're looking for 2:12 z and we need f(x, y, and z). 2:17 And then we're gonna close our lambda there. 2:19 Now I will admit that this is not the most readable or understandable code, 2:23 but it does what it says and it's not that over the top. 2:29 So we're gonna print out curried_f, we're gonna call it with 2, 3 and 2:34 4, so that should give us back a number. 2:38 And that number will be 2 cubed, which is 8 plus 3 squared, which is 9. 2:42 So 17 plus 4, so it should give us back 21. 2:48 So let's try that and we need to save, there we go. 2:51 Okay, 21 cool. 2:58 But now, let's see what happens if we don't give it enough arguments. 3:00 So we'll say g = curried_f, and 3:04 we're only gonna give it the number 2 and then let's print g and see what we get. 3:07 And I bet we get a lambda. 3:15 Yeah, we did. 3:17 Cool. 3:18 So we get 21 here from this line. 3:18 But then we got function. 3:20 And we can see that it's from curried_f and then locals. 3:22 So the variable is defined inside curried_f. 3:25 And then a lambda. 3:28 So cool, we have this lambda from somewhere. 3:30 So g is a lambda. 3:32 All right, let's do another one here, 3:34 let's do h = g(3) and let's print(h). 3:38 All right and so we can see it's getting deeper here. 3:43 So now we have curried_f, locals, lambda, locals, lambda, cuz we got into here. 3:47 Where we got a y, but we didn't get a z, all right. 3:53 And then so finally we'll do i = g(4), print(i) and this should give us 21 again. 3:56 Nope, we get another function. 4:07 Oh, cuz I did g, I need to do h. 4:12 There we go, there's 21 again. 4:17 Cool. So now, 4:19 we can kinda see how currying works with giving us multiple versions of the same 4:20 function, we can build up functions. 4:25 Now, why would you wanna do this? 4:27 This is handy if you have something that has to wait on future input. 4:30 If you think about like, say a calculator. 4:33 You enter in a number, 2. 4:35 And then it waits, and you enter in a plus sign, the operator. 4:38 And it waits and its like okay, I have 2 and I have plus, but 4:43 I don't have anything actionable yet, what do I do? 4:45 And then you put in another 2 and it's like all right, I got some things here, 4:47 I can work with this, but you haven't told me what you want me to do yet. 4:52 And then you hit the equals button and 4:56 it finally goes, oh okay you want me to add those up and give you back the answer. 4:58 Cool I can do that, because now it has enough information. 5:02 Currying isn't something that you're going to encounter a lot in the Python world, so 5:05 I wouldn't worry about memorizing how to accomplish it. 5:10 It can be handy in certain situations though, so it's good to know that exists. 5:13 I'll put a link or two in the teacher's notes. 5:16
You need to sign up for Treehouse in order to download course files.Sign up