Chaining Functions6:49 with Kenneth Love
Standalone functions are great but when we're writing small functions, we often need to do more work than we can achieve in a single function. Luckily, we can chain functions so long as we write them correctly.
sorted(), and friends all return iterables, we can chain them together. Chained functions resolve, or happen, from the inside out, so the innermost function runs first.
This is another reason why functions usually
return a value at the end. It makes it easier to use them all together.
True if any of the items in the iterable are truthy. Similar is the function
True if all of the items in the iterable are truthy.
Watch our comprehensions workshop if you want more information.
I mentioned a couple of videos ago that map and filter work really well together. 0:00 Let's look at a few ways to combine these two small handy functions for 0:05 some useful results. 0:08 So, I handed this earlier and 0:10 you probably figured it out if you've been playing around. 0:12 But you can combine map and filter, and our other functions. 0:15 So long as your function and in that case I mean map, filter sorted, 0:20 reversed, et cetera, returns an iterable, 0:24 these things can all be chained together because they all work together. 0:28 So let's look at these books. 0:32 And there's a lot of, I mean we have a lot of subjects in all these books. 0:35 But I don't know how many of them all contain the same thing, but 0:40 I know that Roland here is in several of them. 0:43 So let's use Roland. 0:47 And let's look up all the books that have Roland in them, alright? 0:49 So let's write a little function here, has_roland. 0:53 That's gonna take a (book). 0:55 And we're gonna return ["Roland" in subject for 0:58 subject in book.subjects. 1:04 And now this isn't gonna work, because if "Roland" isn't in any of the subjects, 1:10 it's gonna return false. 1:15 So, what we need to do is we're gonna wrapped it in a new function any. 1:16 And any returns true or 1:22 false if any of the things in the iterable that it's passed are true. 1:23 So, if we get nothing but falses, then it comes back to false. 1:29 If we get some trues, then it will come back true. 1:34 Even one true makes it true. 1:37 Alright, and then, if we look at our data here, we see the title "Wizard and 1:40 glass." "g" is lowercase, but on "The Waste Lands," it's all title cased. 1:46 You know we got others here that are, seen this one is like upper and lower. 1:53 So let's make it where it always uppercases the book. 2:00 Okay, so again we want to do copy book, and 2:04 then book.title equals book.title.title. 2:08 Title gives up back the title case version of the title. 2:13 We're going to return book. 2:18 All right, cool. 2:20 So, we have two functions. 2:21 Our first function is going to be a filter function. 2:22 We know that because we have this has, or is, or are or whatever. 2:24 And, it's gonna give us back books that have Roland as a subject. 2:29 And then title case gives us back the title cased titles of all the books. 2:32 So let's print the list version and now we can do the filter or 2:38 the map in either direction that we want. 2:42 We could do the filter first or we could do the map first, but if we do the filter 2:45 first, that means we're only title-casing books that are correctly filtered. 2:49 So that means we have to do the map on the outside cuz these things are weighted 2:56 from the inside out. 3:00 So we're gonna map titleCase to whatever comes out of this filter. 3:01 And the filter's gonna be has_roland. 3:06 And that's gonna be all of our books. 3:09 So we close our filter, close our map, close our list, close our print. 3:10 Fingers crossed that this works. 3:14 Look at that. 3:19 We've got, we should have one, two, three, four, five, six I left out a book! 3:20 I left out book four. 3:27 Oh well, anyway, there's our books that we have that are all about Roland. 3:28 We don't have any copies of the fourth book in the series, so 3:33 if you're trying to read the series and bought them at our store, 3:36 you're gonna get really disappointed in the middle of the series. 3:38 But, there's how we can chain this stuff together. 3:42 Now I want to show you one more and I think this is kind of where the power of 3:47 functional programming first like really hits people is in this chain. 3:51 So here's what we're going to do. 3:55 All right. 3:57 So I think a book is a pretty good deal if it's less than five dollars. 3:58 Right? If I can buy a book for 4:03 two bucks, that's a pretty good deal. 4:05 What I wanna know is what are all the books that are less than 4:07 five dollars when they're on sale. 4:11 Tell me about all those books. 4:14 Okay. And I want them sorted, 4:16 I want to know the cheapest one I can possibly buy. 4:17 So. Here we go. 4:20 We're gonna call this cheap books. 4:21 And we're going to start with sorted. 4:24 Right? Cuz we're going to sort something. 4:26 What are we going to sort? 4:29 Well first we need to write a function. 4:30 So let's write a function. 4:34 We're going to say is good deal. 4:35 All right. A book is a good deal if it's 4:37 less than five bucks. 4:39 So we're going to return book.price less than or equal to 5. 4:40 So, $5 and one penny, not worth it. 4:45 Just not worth it to me. 4:49 But, $5 even or cheaper, that's worth it. 4:51 So, we're gonna filter all these books, is_good_deal. 4:57 And what books we want to filter. 5:03 Well I said I wanted to do sales price books. 5:05 So that means I gotta do my sales price thing before. 5:07 So I'm gonna map sales price and that's gonna go to all the books. 5:10 So we close our map we close our filter. 5:15 And then we need a key. 5:18 Well, I'm gonna go back in iterable of objects. 5:20 And each item in our iterable is an object. 5:24 And each of these objects has attributes. 5:26 So we're gonna use attrgetter. 5:29 And the attribute we wanna get is price. 5:31 Alright, so now, all that work later, Well that's 5:35 print(cheap_books), and let's see what I get. 5:40 Needful things, all right. 5:47 And then, what is needful thing's price? 5:49 How cheap is needful things when it's on sale? 5:51 $2.80, that's not bad. 5:55 I don't mind a book for $2.80. 5:57 But now look, let's look at how much work that we've done here. 5:59 So we turned all of our books into their sales price. 6:02 Their default 20% sales price, we turn all of them into that. 6:06 All right, cool. 6:09 Then we filtered through all of these sale price reduced price books, 6:10 and we said, I only want the ones that are $5 or less, right? 6:16 Only these ones that are super duper cheap. 6:22 And then we sorted all of them by whatever that new updated cheap price was. 6:24 We did all that work in six lines, counting a blank line. 6:32 That's pretty amazing. 6:39 Like peanut butter and 6:40 chocolate, some things just get better the more you add to them. 6:41 Filter and map are both useful on their own, but 6:44 their real power comes out much better when you mix them together. 6:46
You need to sign up for Treehouse in order to download course files.Sign up