Lambdas5:36 with Craig Dennis
Lambdas are syntactical sugar for Single Abstract Methods.
Okay, so we did, 0:00 in fact, make a new Anonymous function and passed it into a forEach method. 0:01 It works, but it's pretty ugly, right? 0:06 I mean, if I didn't have that autocomplete feature in my IDE, 0:10 I don't know if I'd wanna type that all the time. 0:13 I mean, sure we've created our first-class function and 0:15 now we can pass it around, but yuck, I mean, right? 0:19 This is just, [SOUND]. 0:22 Well, creating Anonymous functions is pretty much at the heart 0:25 of functional programming. 0:28 Now since we're gonna be doing this a lot, there is a better way. 0:30 In fact, our IDE is even suggesting that we do that right now, right? 0:33 Look, if I go over here, 0:37 it says, Anonymous new can be replaced with a lambda. 0:38 So let's take a quick look at a better way to do things. 0:41 Now we can express this in what is known as a lambda or Anonymous function. 0:44 So the Consumer, right, the Consumer that we passed in here. 0:50 Remember, that was a functional interface. 0:54 And like we said, that means that it has a single abstract method 0:56 which is overridden here, this method here named accept. 1:00 This method is really the only thing that forEach is looking for. 1:04 Side note here, a method that accepts a function as a parameter 1:08 is also referred to as a higher-order function. 1:13 Now I'm gonna go ahead an park that term, but I think you might see it. 1:17 But I'm gonna put it here so you know we're gonna come back to it. 1:20 So we've got Pure, Side Effects, and Higher Order Functions. 1:23 So when you want to write an Anonymous function or 1:29 lambda, what you do is you look at the type that it is expecting. 1:32 So look here, its expecting a String, right. 1:37 We're trying to rewrite this accept method and its expecting a String. 1:40 I'm gonna go ahead, and I'm gonna cut this. 1:42 Let's comment this out, and we'll keep it around so 1:45 that we can take a look at what we were working with. 1:48 So if I say ingredients.forEach, 1:51 we know that it's looking for a String. 1:56 Before we get started with lambdas, 2:00 you should know that we have a couple of forms. 2:02 So I wanna start out on the long way and 2:05 then we'll work our way back to the more succinct version. 2:07 So the way to specify what you're attempting to pass into the function 2:10 is with parentheses, okay? 2:15 So we'll put those here. 2:17 So we'll put a parenthesis, and we said that we were trying to accept a String. 2:19 Okay, so we'd like to see a String of ingredients. 2:24 So that is the parameters that our accept method is looking for here. 2:27 It called it s, we'll call it ingredient, name it a little bit better. 2:31 So that matches the expected accept method declaration, right? 2:34 Okay, and now we need to specify the body of our function. 2:41 What happens when the functions ran? 2:45 So to specify that, you draw an arrow. 2:47 Okay, so it's a dash and then a greater than sign. 2:50 And now we can open up our code block, just like always. 2:54 So we can open up with a opening brace and a closing brace. 2:56 And I'm gonna go ahead and I'm gonna put a semicolon at the end here. 3:01 So, just like before, the body of the method is going to be there. 3:04 It's gonna be, we're gonna print out ingredient. 3:09 Wow, that feels a lot better already, doesn't it? 3:12 Now I just wanna point out, this is just shorthand for 3:16 what we did right here with the creation of our Anonymous function, right? 3:20 This is often referred to as syntactic sugar, 3:25 because it makes writing these sweeter. 3:28 But it really is the exact same thing. 3:30 Well, let's pour some more sugar on it. 3:33 Since we know what type is expected, 3:36 the lambda expression syntax will actually infer the type. 3:39 So I can lose the String here. 3:43 It knows that it is supposed to be a Consumer of String, it knows that. 3:47 Because ingredients is a list of Strings. 3:51 Okay, It gets better, and 3:54 I hope you're not a diabetic cuz we're adding a lot of sugar here. 3:55 So we can actually have a one liner, right? 3:59 Because these opening and closing curlies, they aren't really needed. 4:03 So we can go like this And actually, 4:07 when there's only one parameter, you can get rid of these parenths. 4:12 Take a gander at that beauty. 4:19 That is Java. 4:20 Can you believe how succinct that is? 4:23 Now, we'll get tons of practice writing these throughout the course. 4:25 Let's go ahead and run it, and make sure that things are working, perfect. 4:29 Now I just wanna show off that this syntax is 4:33 really just creating one of those Anonymous Consumer functions. 4:36 So to show that off, let's go ahead. 4:40 You can actually create one. 4:42 Let's assume that you wanted to reuse this function multiple times. 4:44 You can actually just create one like this, so we'll say it's a Consumer 4:47 of Strings, and we're gonna name that, let's just call it printer, for now. 4:51 And it's gonna take an ingredient and 4:57 it's going to then print out that ingredient. 5:00 See how it's creating one, just like that. 5:05 And then I can use it in this forEach, I can just pass that in, right? 5:08 Because they're first-class values. 5:12 And I'm gonna run that again, boom. 5:15 Now believe it or not, we can make this even better. 5:18 So you'll notice that there's an IDE suggestion here that says, 5:23 Can be replaced with a method reference. 5:26 Method references are another new feature added to Java 8. 5:29 Let's take a look at those right after this quick break 5:33
You need to sign up for Treehouse in order to download course files.Sign up