1 00:00:00,330 --> 00:00:05,400 Anonymous functions are functions that don't have a name, hence being anonymous. 2 00:00:05,400 --> 00:00:07,423 I say that, but it's not a 100% true. 3 00:00:07,423 --> 00:00:11,080 You can give anonymous functions a name in Python, but you don't have to and 4 00:00:11,080 --> 00:00:12,660 usually won't. 5 00:00:12,660 --> 00:00:17,650 We call anonymous functions lambda's, borrowed from mathematics lambda calculus. 6 00:00:17,650 --> 00:00:20,280 Remember that whole, programming is done with expressions bit? 7 00:00:20,280 --> 00:00:21,090 We've come full circle. 8 00:00:22,240 --> 00:00:25,040 Let's jump over to Workspaces and see how to use lambdas and 9 00:00:25,040 --> 00:00:27,200 how they can be applied to our existing functionality. 10 00:00:28,600 --> 00:00:32,410 So lambdas are anonymous functions in Python. 11 00:00:32,410 --> 00:00:35,770 They let us just write a function somewhere that we need one, and 12 00:00:35,770 --> 00:00:37,700 we don't wanna reference that function again. 13 00:00:37,700 --> 00:00:41,180 We can give lambdas names, so they're not anonymous, but that's a little weird. 14 00:00:41,180 --> 00:00:42,520 Most people don't do that. 15 00:00:42,520 --> 00:00:45,930 And lambdas are really only meant to be one line long, and 16 00:00:45,930 --> 00:00:50,250 lambdas can't contain assignments, so we can't do a equals five. 17 00:00:50,250 --> 00:00:50,860 You might be thinking, 18 00:00:50,860 --> 00:00:55,390 well that's an awful lot of rules, why do we even wanna have lambdas then? 19 00:00:55,390 --> 00:00:58,000 It's because they're really useful for little one off problems. 20 00:00:58,000 --> 00:01:00,190 They're very handy for, I don't wanna write a function, 21 00:01:00,190 --> 00:01:05,110 I just need to throw this thing in there, so let me do that. 22 00:01:05,110 --> 00:01:07,770 For instance, all these places that we've been writing functions up here, 23 00:01:07,770 --> 00:01:09,340 with the exception of the maps. 24 00:01:09,340 --> 00:01:15,510 So, this map here, the title case, and this map here, the sales price. 25 00:01:15,510 --> 00:01:18,250 We have to do assignment in both of these. 26 00:01:18,250 --> 00:01:20,750 So we can't do these as lambdas. 27 00:01:20,750 --> 00:01:23,370 But we can do our functions and our reduces and stuff as lambdas. 28 00:01:23,370 --> 00:01:24,280 So let's see about that. 29 00:01:24,280 --> 00:01:25,120 Let's look at this one. 30 00:01:25,120 --> 00:01:26,760 We just did this total one, right? 31 00:01:26,760 --> 00:01:30,590 Total equals reduce add_book_prices, so let's repeat that. 32 00:01:30,590 --> 00:01:34,643 So let's do total, and it's gonna be reduced, and 33 00:01:34,643 --> 00:01:41,250 now I haven't written a function, but my function up there just did a + b, right? 34 00:01:41,250 --> 00:01:42,620 So, we're going to do lambda, and 35 00:01:42,620 --> 00:01:47,140 we're going to take two numbers, x and y, just like in algebra class. 36 00:01:47,140 --> 00:01:48,760 And we're gonna add x and y. 37 00:01:48,760 --> 00:01:54,023 And lambda's automatically return the last value that they calculate. 38 00:01:54,023 --> 00:01:55,775 They have an implicit return, so 39 00:01:55,775 --> 00:01:59,101 you can pretend that there's the return keyword right there. 40 00:01:59,101 --> 00:02:00,030 But you don't wanna put that in. 41 00:02:01,070 --> 00:02:02,550 Okay, so this is our lambda. 42 00:02:04,220 --> 00:02:05,580 So we have lambda. 43 00:02:05,580 --> 00:02:07,760 These are the arguments that the lambda takes. 44 00:02:07,760 --> 00:02:11,158 In this case our lambda takes two arguments, x and y. 45 00:02:11,158 --> 00:02:14,990 A colon cuz we're starting a block, but we're not indenting the block. 46 00:02:14,990 --> 00:02:16,945 Lambdas are a little bit strange, huh? 47 00:02:16,945 --> 00:02:19,825 And then this is the body of our function, just x plus y. 48 00:02:19,825 --> 00:02:21,920 And that gets returned automatically. 49 00:02:21,920 --> 00:02:24,220 So now it's a reduce so we have our function and 50 00:02:24,220 --> 00:02:27,170 then we have our list of values right? 51 00:02:27,170 --> 00:02:34,478 So let's say b.price for b in BOOKS So 52 00:02:34,478 --> 00:02:40,100 when we run this we should get 225, so we did. 53 00:02:40,100 --> 00:02:41,529 So, we got the same amount of work but 54 00:02:41,529 --> 00:02:44,173 we didn't have to write a function just to add two things together. 55 00:02:44,173 --> 00:02:45,640 So, that's pretty cool. 56 00:02:45,640 --> 00:02:47,370 Let's do our long_books again. 57 00:02:47,370 --> 00:02:51,047 So, it's a filter and lambda is x. 58 00:02:51,047 --> 00:02:53,110 Now lambda can be anything, we're gonna say book. 59 00:02:53,110 --> 00:02:54,195 Lambda book. 60 00:02:54,195 --> 00:02:55,813 Cuz we're gonna get a book, right? 61 00:02:55,813 --> 00:02:59,280 So book.number_of_pages. 62 00:02:59,280 --> 00:03:02,710 [INAUDIBLE] equal to 600 and we're gonna do this out of BOOKS. 63 00:03:05,180 --> 00:03:09,847 All right, so now since I want to be able to print, 64 00:03:09,847 --> 00:03:13,860 let's do len(list(long_books)). 65 00:03:13,860 --> 00:03:16,215 So how many long books do we have? 66 00:03:16,215 --> 00:03:17,235 We have 12 long books. 67 00:03:17,235 --> 00:03:19,755 If I remember correctly, that was the number before. 68 00:03:19,755 --> 00:03:24,067 So lambdas are really handy for being able to stick in places that we 69 00:03:24,067 --> 00:03:28,920 don't wanna have to write a function that's only gonna get used one time. 70 00:03:30,250 --> 00:03:35,872 We could do, good_deals = filter (lambda book: 71 00:03:35,872 --> 00:03:41,494 book.price <= let's say 5.99, all right, 72 00:03:41,494 --> 00:03:47,133 we'll do 6, so if the book's than \$6 this time. 73 00:03:47,133 --> 00:03:52,215 Out of books and we're gonna print len(listgood_deals)) and 74 00:03:52,215 --> 00:03:55,750 let's see if there are any good cheap books. 75 00:03:55,750 --> 00:03:57,430 There's 6, cool. 76 00:03:57,430 --> 00:03:59,120 So 6 books that are nice and cheap. 77 00:04:00,120 --> 00:04:04,220 So again lambdas are just something that you use instead of having to 78 00:04:04,220 --> 00:04:07,520 write a function for something that you're only gonna do once. 79 00:04:07,520 --> 00:04:09,780 That's where lambdas come in really really handy. 80 00:04:11,718 --> 00:04:13,940 You might have heard the expression, when all you have is a hammer, 81 00:04:13,940 --> 00:04:15,410 everything looks like a nail. 82 00:04:15,410 --> 00:04:18,890 Well, lambdas are often a big hammer in the Python world. 83 00:04:18,890 --> 00:04:21,660 You learn about them, and then you want to use them in all sorts of places and 84 00:04:21,660 --> 00:04:23,020 situations. 85 00:04:23,020 --> 00:04:27,099 Most of the time, a lambda is the wrong choice unless you're sure that you're only 86 00:04:27,099 --> 00:04:29,840 going to need a particular bit of functionality once and 87 00:04:29,840 --> 00:04:30,965 only in one location. 88 00:04:30,965 --> 00:04:34,320 Writing our full functions might be a bit more trouble and take up more room, but 89 00:04:34,320 --> 00:04:36,390 it will usually serve you better in the long run.