1 00:00:00,570 --> 00:00:04,430 One of the more challenging concepts to grasp as you are just picking up 2 00:00:04,430 --> 00:00:08,130 functional programming is dealing with how to create loops. 3 00:00:08,130 --> 00:00:12,020 Almost everything we do in programming deals with a collection of data and 4 00:00:12,020 --> 00:00:16,210 we must process that collection, typically element by element. 5 00:00:16,210 --> 00:00:19,400 Let's take a quick look at the most basic way to do this and 6 00:00:19,400 --> 00:00:21,590 further use it to create our first function. 7 00:00:21,590 --> 00:00:22,760 Sound good? 8 00:00:22,760 --> 00:00:25,468 Okay so we have our collection here already. 9 00:00:25,468 --> 00:00:27,150 So for simplicity's sake, 10 00:00:27,150 --> 00:00:32,150 let's assume that we want to print out each of the ingredients on a single line. 11 00:00:32,150 --> 00:00:35,290 So how do go about doing that with our imperative style? 12 00:00:35,290 --> 00:00:38,295 Well, to start, let's just go ahead and I'll reuse some of this code here. 13 00:00:38,295 --> 00:00:41,761 All right, so we don't need this line anymore. 14 00:00:41,761 --> 00:00:45,553 And I'll just go ahead and comment this. 15 00:00:45,553 --> 00:00:49,701 And we'll just loop through each of the ingredients and we'll print it, right? 16 00:00:51,573 --> 00:00:54,240 We'll print that ingredient that's coming through the list. 17 00:00:54,240 --> 00:00:58,312 Wanna get rid of this too, here, right? 18 00:00:58,312 --> 00:01:00,850 That's how we would do it imperatively. 19 00:01:00,850 --> 00:01:02,060 List them out on each line, right? 20 00:01:05,825 --> 00:01:12,148 Okay, so all classes that implement Iterable, like our list class here, right? 21 00:01:12,148 --> 00:01:15,380 It implements Iterable, that's why we're able to loop through here. 22 00:01:15,380 --> 00:01:19,601 Well they also have a new method named forEach. 23 00:01:19,601 --> 00:01:21,019 So let's explore that a little bit. 24 00:01:21,019 --> 00:01:25,988 So if I come in here and I say ingredients.forEach. 25 00:01:25,988 --> 00:01:29,600 And I'm gonna open and close the parens and then I'm gonna press F1. 26 00:01:31,580 --> 00:01:33,320 Okay, so if we take a look at the bottom here, 27 00:01:33,320 --> 00:01:38,280 it says this has been around since 1.8, which is Java 8, all right? 28 00:01:38,280 --> 00:01:43,370 So what this does is it takes an action type of Consumer. 29 00:01:43,370 --> 00:01:47,050 All right, so it takes an action of type consumer. 30 00:01:47,050 --> 00:01:50,940 So here it says, it performs the given action for each element of the Iterable 31 00:01:50,940 --> 00:01:54,270 until all elements have been processed or the action throws an exception. 32 00:01:54,270 --> 00:01:55,880 Hey, that's exactly what we need, right? 33 00:01:55,880 --> 00:02:00,018 So our action is going to be, print out each ingredient. 34 00:02:00,018 --> 00:02:02,980 So let's take a look at what this consumer says here. 35 00:02:02,980 --> 00:02:07,470 Okay, so it's in the java.util.function package and it's a functional interface. 36 00:02:07,470 --> 00:02:09,433 We'll take a look at these here more in detail. 37 00:02:09,433 --> 00:02:14,350 But for now I just want you to think about a functional interface as an interface 38 00:02:14,350 --> 00:02:17,220 that has exactly one abstract method. 39 00:02:17,220 --> 00:02:20,700 It's a SAM, a single abstract method. 40 00:02:20,700 --> 00:02:23,520 So it represents an operation that accepts a single input argument and 41 00:02:23,520 --> 00:02:25,250 returns no result, cool. 42 00:02:25,250 --> 00:02:27,196 So it's unlike other functional interfaces, 43 00:02:27,196 --> 00:02:29,770 Consumer is expected to operate via side effects. 44 00:02:29,770 --> 00:02:32,910 That is another one of these terms that we are gonna talk about later. 45 00:02:32,910 --> 00:02:34,650 And I'm gonna put it in the parking lot, 46 00:02:34,650 --> 00:02:37,360 so know that we don't need to worry about it. 47 00:02:37,360 --> 00:02:38,920 Don't let these new terms overwhelm you. 48 00:02:38,920 --> 00:02:40,180 So, we'll come over here. 49 00:02:40,180 --> 00:02:40,948 Here's our parking lot. 50 00:02:40,948 --> 00:02:42,930 I already added Pure from before. 51 00:02:42,930 --> 00:02:48,556 So I'm gonna go ahead and I'm gonna add side effects, perfect. 52 00:02:48,556 --> 00:02:51,335 Okay, so we need to add a new consumer, and 53 00:02:51,335 --> 00:02:54,590 I am going to let our IDE give us a little help here. 54 00:02:54,590 --> 00:02:58,711 So I'm gonna say, new Consumer, and I'm going to press Tab. 55 00:02:58,711 --> 00:03:00,725 And it's gonna go ahead and auto-complete that. 56 00:03:00,725 --> 00:03:03,036 And because it has a single abstract field, 57 00:03:03,036 --> 00:03:06,695 it's gonna automatically pop out what we need to override, right? 58 00:03:06,695 --> 00:03:09,100 Cuz it knows that's the only thing we need to do. 59 00:03:09,100 --> 00:03:10,520 And this is pretty standard, right? 60 00:03:10,520 --> 00:03:13,410 This is how you would actually do things before Java 8. 61 00:03:13,410 --> 00:03:14,346 There's nothing new here. 62 00:03:14,346 --> 00:03:18,110 A single abstract method, this is a common concept for these classes. 63 00:03:18,110 --> 00:03:21,410 You would say new and it would tell what it wanted to override. 64 00:03:21,410 --> 00:03:24,720 So inside this method here I think we wanna do just like we did up top, right? 65 00:03:24,720 --> 00:03:26,340 We wanna just print out s. 66 00:03:29,150 --> 00:03:31,992 I wanna put a semicolon here at the bottom. 67 00:03:31,992 --> 00:03:35,575 Okay, so I'm gonna comment out this here and 68 00:03:35,575 --> 00:03:38,890 I'm going to go ahead and give this a run. 69 00:03:44,282 --> 00:03:45,800 Awesome, it works. 70 00:03:47,680 --> 00:03:50,900 So this is the declarative way to do things. 71 00:03:50,900 --> 00:03:56,290 Now I'm not sure if you saw it, but when I came in here and I chose Help. 72 00:03:56,290 --> 00:04:00,500 It says, unless otherwise specified by the implementing class, 73 00:04:00,500 --> 00:04:02,390 it's gonna go ahead and order the iteration. 74 00:04:02,390 --> 00:04:07,640 What's cool about that, is that this is a great example of the declarative when. 75 00:04:07,640 --> 00:04:10,757 By stating what we want, but not how to do it, 76 00:04:10,757 --> 00:04:13,972 we take that problem out of our hands, right? 77 00:04:13,972 --> 00:04:19,210 Before we were saying, we were talking about how to loop through it. 78 00:04:19,210 --> 00:04:22,010 This is putting it in the hands of the iterator. 79 00:04:22,010 --> 00:04:24,630 So that assumes that the Iterable here 80 00:04:24,630 --> 00:04:27,260 knows how best to loop through its own items, right? 81 00:04:27,260 --> 00:04:28,780 It's none of our business. 82 00:04:28,780 --> 00:04:32,770 So once more, the forEach method handles the looping. 83 00:04:32,770 --> 00:04:38,900 And when it has each single item, it calls or invokes this new consumer here, right? 84 00:04:38,900 --> 00:04:46,230 That gets instantiated, and it passes that value into the accept method. 85 00:04:46,230 --> 00:04:51,740 So notice how here it has a specified parametrized type of string? 86 00:04:51,740 --> 00:04:54,740 So you can read this as it's a consumer of strings, 87 00:04:54,740 --> 00:04:56,960 and that's just what we did, right? 88 00:04:56,960 --> 00:05:02,100 We provided a forEach method, a function that consumes strings. 89 00:05:02,100 --> 00:05:06,190 Now if this seems a little clunky to you, you're totally right. 90 00:05:06,190 --> 00:05:10,740 Up until Java 8, this is the way to do things with single abstract methods. 91 00:05:10,740 --> 00:05:16,320 You instantiate a new, temporary, anonymous class, just like this. 92 00:05:16,320 --> 00:05:17,800 It's pretty ugly. 93 00:05:17,800 --> 00:05:21,505 I can think of exactly zero people who thinks it looks pretty. 94 00:05:21,505 --> 00:05:26,280 [LAUGH] So you'll see a ton of this in UI event handlers, like your Android apps or 95 00:05:26,280 --> 00:05:27,760 Java effects. 96 00:05:27,760 --> 00:05:32,020 So you wind up sifting through a bunch of code like this. 97 00:05:32,020 --> 00:05:35,110 Just for a single line, a single method. 98 00:05:36,270 --> 00:05:37,500 Well, here's some great news. 99 00:05:37,500 --> 00:05:42,570 The language has introduced a new syntax to make this much more prettier and 100 00:05:42,570 --> 00:05:43,510 more succinct. 101 00:05:43,510 --> 00:05:45,860 Let's make things pretty right after this quick break.