1 00:00:00,650 --> 00:00:05,130 I mentioned a couple of videos ago that map and filter work really well together. 2 00:00:05,130 --> 00:00:08,530 Let's look at a few ways to combine these two small handy functions for 3 00:00:08,530 --> 00:00:10,150 some useful results. 4 00:00:10,150 --> 00:00:12,910 So, I handed this earlier and 5 00:00:12,910 --> 00:00:15,730 you probably figured it out if you've been playing around. 6 00:00:15,730 --> 00:00:20,630 But you can combine map and filter, and our other functions. 7 00:00:20,630 --> 00:00:24,970 So long as your function and in that case I mean map, filter sorted, 8 00:00:24,970 --> 00:00:28,630 reversed, et cetera, returns an iterable, 9 00:00:28,630 --> 00:00:32,250 these things can all be chained together because they all work together. 10 00:00:32,250 --> 00:00:34,240 So let's look at these books. 11 00:00:35,900 --> 00:00:39,740 And there's a lot of, I mean we have a lot of subjects in all these books. 12 00:00:40,790 --> 00:00:43,450 But I don't know how many of them all contain the same thing, but 13 00:00:43,450 --> 00:00:47,200 I know that Roland here is in several of them. 14 00:00:47,200 --> 00:00:49,150 So let's use Roland. 15 00:00:49,150 --> 00:00:53,580 And let's look up all the books that have Roland in them, alright? 16 00:00:53,580 --> 00:00:55,990 So let's write a little function here, has_roland. 17 00:00:55,990 --> 00:00:58,500 That's gonna take a (book). 18 00:00:58,500 --> 00:01:04,310 And we're gonna return ["Roland" in subject for 19 00:01:04,310 --> 00:01:10,430 subject in book.subjects. 20 00:01:10,430 --> 00:01:15,310 And now this isn't gonna work, because if "Roland" isn't in any of the subjects, 21 00:01:15,310 --> 00:01:16,750 it's gonna return false. 22 00:01:16,750 --> 00:01:20,560 So, what we need to do is we're gonna wrapped it in a new function any. 23 00:01:22,170 --> 00:01:23,940 And any returns true or 24 00:01:23,940 --> 00:01:29,640 false if any of the things in the iterable that it's passed are true. 25 00:01:29,640 --> 00:01:34,430 So, if we get nothing but falses, then it comes back to false. 26 00:01:34,430 --> 00:01:37,930 If we get some trues, then it will come back true. 27 00:01:37,930 --> 00:01:39,210 Even one true makes it true. 28 00:01:40,330 --> 00:01:46,920 Alright, and then, if we look at our data here, we see the title "Wizard and 29 00:01:46,920 --> 00:01:51,830 glass." "g" is lowercase, but on "The Waste Lands," it's all title cased. 30 00:01:53,110 --> 00:02:00,330 You know we got others here that are, seen this one is like upper and lower. 31 00:02:00,330 --> 00:02:04,900 So let's make it where it always uppercases the book. 32 00:02:04,900 --> 00:02:08,901 Okay, so again we want to do copy book, and 33 00:02:08,901 --> 00:02:13,471 then book.title equals book.title.title. 34 00:02:13,471 --> 00:02:17,300 Title gives up back the title case version of the title. 35 00:02:18,680 --> 00:02:19,520 We're going to return book. 36 00:02:20,710 --> 00:02:21,410 All right, cool. 37 00:02:21,410 --> 00:02:22,590 So, we have two functions. 38 00:02:22,590 --> 00:02:24,490 Our first function is going to be a filter function. 39 00:02:24,490 --> 00:02:29,020 We know that because we have this has, or is, or are or whatever. 40 00:02:29,020 --> 00:02:32,820 And, it's gonna give us back books that have Roland as a subject. 41 00:02:32,820 --> 00:02:37,310 And then title case gives us back the title cased titles of all the books. 42 00:02:38,330 --> 00:02:42,710 So let's print the list version and now we can do the filter or 43 00:02:42,710 --> 00:02:45,530 the map in either direction that we want. 44 00:02:45,530 --> 00:02:49,410 We could do the filter first or we could do the map first, but if we do the filter 45 00:02:49,410 --> 00:02:56,030 first, that means we're only title-casing books that are correctly filtered. 46 00:02:56,030 --> 00:03:00,100 So that means we have to do the map on the outside cuz these things are weighted 47 00:03:00,100 --> 00:03:01,120 from the inside out. 48 00:03:01,120 --> 00:03:06,130 So we're gonna map titleCase to whatever comes out of this filter. 49 00:03:06,130 --> 00:03:09,190 And the filter's gonna be has_roland. 50 00:03:09,190 --> 00:03:10,370 And that's gonna be all of our books. 51 00:03:10,370 --> 00:03:13,240 So we close our filter, close our map, close our list, close our print. 52 00:03:14,410 --> 00:03:16,673 Fingers crossed that this works. 53 00:03:19,494 --> 00:03:20,353 Look at that. 54 00:03:20,353 --> 00:03:27,950 We've got, we should have one, two, three, four, five, six I left out a book! 55 00:03:27,950 --> 00:03:28,818 I left out book four. 56 00:03:28,818 --> 00:03:33,740 Oh well, anyway, there's our books that we have that are all about Roland. 57 00:03:33,740 --> 00:03:36,390 We don't have any copies of the fourth book in the series, so 58 00:03:36,390 --> 00:03:38,860 if you're trying to read the series and bought them at our store, 59 00:03:38,860 --> 00:03:42,950 you're gonna get really disappointed in the middle of the series. 60 00:03:42,950 --> 00:03:47,360 But, there's how we can chain this stuff together. 61 00:03:47,360 --> 00:03:51,400 Now I want to show you one more and I think this is kind of where the power of 62 00:03:51,400 --> 00:03:55,540 functional programming first like really hits people is in this chain. 63 00:03:55,540 --> 00:03:57,160 So here's what we're going to do. 64 00:03:57,160 --> 00:03:58,000 All right. 65 00:03:58,000 --> 00:04:03,800 So I think a book is a pretty good deal if it's less than five dollars. 66 00:04:03,800 --> 00:04:05,030 Right? If I can buy a book for 67 00:04:05,030 --> 00:04:07,170 two bucks, that's a pretty good deal. 68 00:04:07,170 --> 00:04:11,090 What I wanna know is what are all the books that are less than 69 00:04:11,090 --> 00:04:14,060 five dollars when they're on sale. 70 00:04:14,060 --> 00:04:15,250 Tell me about all those books. 71 00:04:16,360 --> 00:04:17,710 Okay. And I want them sorted, 72 00:04:17,710 --> 00:04:20,850 I want to know the cheapest one I can possibly buy. 73 00:04:20,850 --> 00:04:21,780 So. Here we go. 74 00:04:21,780 --> 00:04:23,600 We're gonna call this cheap books. 75 00:04:24,790 --> 00:04:26,420 And we're going to start with sorted. 76 00:04:26,420 --> 00:04:29,110 Right? Cuz we're going to sort something. 77 00:04:29,110 --> 00:04:30,980 What are we going to sort? 78 00:04:30,980 --> 00:04:33,130 Well first we need to write a function. 79 00:04:34,140 --> 00:04:35,160 So let's write a function. 80 00:04:35,160 --> 00:04:36,770 We're going to say is good deal. 81 00:04:37,920 --> 00:04:39,710 All right. A book is a good deal if it's 82 00:04:39,710 --> 00:04:40,770 less than five bucks. 83 00:04:40,770 --> 00:04:45,282 So we're going to return book.price less than or equal to 5. 84 00:04:45,282 --> 00:04:49,005 So, \$5 and one penny, not worth it. 85 00:04:49,005 --> 00:04:51,746 Just not worth it to me. 86 00:04:51,746 --> 00:04:57,800 But, \$5 even or cheaper, that's worth it. 87 00:04:57,800 --> 00:05:03,798 So, we're gonna filter all these books, is_good_deal. 88 00:05:03,798 --> 00:05:05,540 And what books we want to filter. 89 00:05:05,540 --> 00:05:07,910 Well I said I wanted to do sales price books. 90 00:05:07,910 --> 00:05:10,560 So that means I gotta do my sales price thing before. 91 00:05:10,560 --> 00:05:15,430 So I'm gonna map sales price and that's gonna go to all the books. 92 00:05:15,430 --> 00:05:17,110 So we close our map we close our filter. 93 00:05:18,345 --> 00:05:19,425 And then we need a key. 94 00:05:20,505 --> 00:05:24,105 Well, I'm gonna go back in iterable of objects. 95 00:05:24,105 --> 00:05:26,245 And each item in our iterable is an object. 96 00:05:26,245 --> 00:05:29,025 And each of these objects has attributes. 97 00:05:29,025 --> 00:05:30,565 So we're gonna use attrgetter. 98 00:05:31,605 --> 00:05:33,505 And the attribute we wanna get is price. 99 00:05:35,045 --> 00:05:40,023 Alright, so now, all that work later, Well that's 100 00:05:40,023 --> 00:05:46,060 print(cheap_books[0]), and let's see what I get. 101 00:05:47,380 --> 00:05:49,230 Needful things, all right. 102 00:05:49,230 --> 00:05:51,628 And then, what is needful thing's price? 103 00:05:51,628 --> 00:05:55,838 How cheap is needful things when it's on sale? 104 00:05:55,838 --> 00:05:57,690 \$2.80, that's not bad. 105 00:05:57,690 --> 00:05:59,120 I don't mind a book for \$2.80. 106 00:05:59,120 --> 00:06:02,470 But now look, let's look at how much work that we've done here. 107 00:06:02,470 --> 00:06:06,050 So we turned all of our books into their sales price. 108 00:06:06,050 --> 00:06:09,620 Their default 20% sales price, we turn all of them into that. 109 00:06:09,620 --> 00:06:10,750 All right, cool. 110 00:06:10,750 --> 00:06:16,910 Then we filtered through all of these sale price reduced price books, 111 00:06:16,910 --> 00:06:20,230 and we said, I only want the ones that are \$5 or less, right? 112 00:06:22,140 --> 00:06:23,810 Only these ones that are super duper cheap. 113 00:06:24,990 --> 00:06:32,740 And then we sorted all of them by whatever that new updated cheap price was. 114 00:06:32,740 --> 00:06:39,010 We did all that work in six lines, counting a blank line. 115 00:06:39,010 --> 00:06:40,530 That's pretty amazing. 116 00:06:40,530 --> 00:06:41,260 Like peanut butter and 117 00:06:41,260 --> 00:06:44,620 chocolate, some things just get better the more you add to them. 118 00:06:44,620 --> 00:06:46,990 Filter and map are both useful on their own, but 119 00:06:46,990 --> 00:06:49,570 their real power comes out much better when you mix them together.