1 00:00:00,320 --> 00:00:03,410 So now, let's talk about currying. 2 00:00:03,410 --> 00:00:06,020 So currying is a little bit weird. 3 00:00:06,020 --> 00:00:09,850 And I have to admit, I could not come up with a good currying example on my own. 4 00:00:09,850 --> 00:00:11,270 So I borrowed this one off the Internet. 5 00:00:11,270 --> 00:00:14,070 I'm gonna link to the original doc where I found it. 6 00:00:14,070 --> 00:00:18,310 But the idea of currying is that you can call a function and that function, if it 7 00:00:18,310 --> 00:00:23,280 doesn't have enough arguments, will fill in as many arguments as it can to itself. 8 00:00:23,280 --> 00:00:26,820 And send you back a new version of itself with those arguments already filled in. 9 00:00:26,820 --> 00:00:29,240 And then you can put in new ones after that. 10 00:00:30,470 --> 00:00:34,453 So let's write the function real quick, we're gonna call this curried_f. 11 00:00:34,453 --> 00:00:38,580 And it's gonna take x, y can be None and z can be None. 12 00:00:39,620 --> 00:00:43,140 And then inside here we're gonna have a function called f that takes x, y and 13 00:00:43,140 --> 00:00:45,420 z or zed. 14 00:00:45,420 --> 00:00:53,430 And returns x cubed plus y squared plus z, just as it normally is. 15 00:00:53,430 --> 00:00:57,210 Okay, so f is our real function. 16 00:00:57,210 --> 00:01:00,032 Curried_f is the curried version of f, okay. 17 00:01:00,032 --> 00:01:06,510 If y is not None, so somebody gave us y and z is not None. 18 00:01:06,510 --> 00:01:08,200 Now they have to give us x. 19 00:01:08,200 --> 00:01:12,420 Then we're gonna return f(x, y, z), okay. 20 00:01:12,420 --> 00:01:15,250 So we're gonna actually return, just the function like normal. 21 00:01:16,480 --> 00:01:20,240 If y is not None which means that z is, 22 00:01:20,240 --> 00:01:24,940 then we're gonna return a lambda where we're looking for an argument named z. 23 00:01:26,030 --> 00:01:28,740 And then we're gonna call f(x, y, z). 24 00:01:30,350 --> 00:01:33,260 Okay, so we're getting back a lambda which is a function. 25 00:01:33,260 --> 00:01:36,960 And that lambda is looking for one argument which we're gonna call z or zed. 26 00:01:36,960 --> 00:01:40,260 And then we're gonna pass that to f and call f like normal. 27 00:01:41,520 --> 00:01:45,160 And if neither of those ifs happen, then we're gonna make a new lambda 28 00:01:46,760 --> 00:01:50,440 that's looking for y and where z is already equal to None. 29 00:01:51,630 --> 00:01:54,430 And then, hey look here's how you make a lambda on more than one line. 30 00:01:55,890 --> 00:02:00,080 We're going to return f(x, y, z), 31 00:02:00,080 --> 00:02:04,030 if y is not None and z is not None. 32 00:02:05,480 --> 00:02:08,960 So, when this lambda gets called, if y and 33 00:02:08,960 --> 00:02:11,090 z are filled in, return the function like normal. 34 00:02:12,090 --> 00:02:17,204 Otherwise, we're gonna return the lambda where we're looking for 35 00:02:17,204 --> 00:02:19,597 z and we need f(x, y, and z). 36 00:02:19,597 --> 00:02:23,380 And then we're gonna close our lambda there. 37 00:02:23,380 --> 00:02:29,539 Now I will admit that this is not the most readable or understandable code, 38 00:02:29,539 --> 00:02:34,072 but it does what it says and it's not that over the top. 39 00:02:34,072 --> 00:02:38,867 So we're gonna print out curried_f, we're gonna call it with 2, 3 and 40 00:02:38,867 --> 00:02:42,190 4, so that should give us back a number. 41 00:02:42,190 --> 00:02:48,420 And that number will be 2 cubed, which is 8 plus 3 squared, which is 9. 42 00:02:48,420 --> 00:02:51,870 So 17 plus 4, so it should give us back 21. 43 00:02:51,870 --> 00:02:58,909 So let's try that and we need to save, there we go. 44 00:02:58,909 --> 00:03:00,590 Okay, 21 cool. 45 00:03:00,590 --> 00:03:04,810 But now, let's see what happens if we don't give it enough arguments. 46 00:03:04,810 --> 00:03:07,360 So we'll say g = curried_f, and 47 00:03:07,360 --> 00:03:13,290 we're only gonna give it the number 2 and then let's print g and see what we get. 48 00:03:15,680 --> 00:03:16,420 And I bet we get a lambda. 49 00:03:17,610 --> 00:03:18,180 Yeah, we did. 50 00:03:18,180 --> 00:03:18,820 Cool. 51 00:03:18,820 --> 00:03:20,840 So we get 21 here from this line. 52 00:03:20,840 --> 00:03:22,800 But then we got function. 53 00:03:22,800 --> 00:03:25,940 And we can see that it's from curried_f and then locals. 54 00:03:25,940 --> 00:03:28,740 So the variable is defined inside curried_f. 55 00:03:28,740 --> 00:03:30,270 And then a lambda. 56 00:03:30,270 --> 00:03:32,990 So cool, we have this lambda from somewhere. 57 00:03:32,990 --> 00:03:34,130 So g is a lambda. 58 00:03:34,130 --> 00:03:38,686 All right, let's do another one here, 59 00:03:38,686 --> 00:03:43,923 let's do h = g(3) and let's print(h). 60 00:03:43,923 --> 00:03:47,370 All right and so we can see it's getting deeper here. 61 00:03:47,370 --> 00:03:53,590 So now we have curried_f, locals, lambda, locals, lambda, cuz we got into here. 62 00:03:53,590 --> 00:03:56,562 Where we got a y, but we didn't get a z, all right. 63 00:03:56,562 --> 00:04:04,150 And then so finally we'll do i = g(4), print(i) and this should give us 21 again. 64 00:04:07,200 --> 00:04:09,300 Nope, we get another function. 65 00:04:12,560 --> 00:04:14,290 Oh, cuz I did g, I need to do h. 66 00:04:17,630 --> 00:04:19,470 There we go, there's 21 again. 67 00:04:19,470 --> 00:04:20,342 Cool. So now, 68 00:04:20,342 --> 00:04:25,179 we can kinda see how currying works with giving us multiple versions of the same 69 00:04:25,179 --> 00:04:27,610 function, we can build up functions. 70 00:04:27,610 --> 00:04:30,060 Now, why would you wanna do this? 71 00:04:30,060 --> 00:04:33,300 This is handy if you have something that has to wait on future input. 72 00:04:33,300 --> 00:04:35,930 If you think about like, say a calculator. 73 00:04:35,930 --> 00:04:38,810 You enter in a number, 2. 74 00:04:38,810 --> 00:04:43,480 And then it waits, and you enter in a plus sign, the operator. 75 00:04:43,480 --> 00:04:45,770 And it waits and its like okay, I have 2 and I have plus, but 76 00:04:45,770 --> 00:04:47,620 I don't have anything actionable yet, what do I do? 77 00:04:47,620 --> 00:04:52,940 And then you put in another 2 and it's like all right, I got some things here, 78 00:04:52,940 --> 00:04:56,230 I can work with this, but you haven't told me what you want me to do yet. 79 00:04:56,230 --> 00:04:58,730 And then you hit the equals button and 80 00:04:58,730 --> 00:05:02,920 it finally goes, oh okay you want me to add those up and give you back the answer. 81 00:05:02,920 --> 00:05:05,361 Cool I can do that, because now it has enough information. 82 00:05:05,361 --> 00:05:10,170 Currying isn't something that you're going to encounter a lot in the Python world, so 83 00:05:10,170 --> 00:05:13,180 I wouldn't worry about memorizing how to accomplish it. 84 00:05:13,180 --> 00:05:16,900 It can be handy in certain situations though, so it's good to know that exists. 85 00:05:16,900 --> 00:05:18,480 I'll put a link or two in the teacher's notes.