1 00:00:00,670 --> 00:00:04,680 Okay, so we've got to look at consumer and supplier. 2 00:00:04,680 --> 00:00:08,210 So next up in our list of important function shapes 3 00:00:08,210 --> 00:00:11,420 is the root of all of these, function. 4 00:00:11,420 --> 00:00:14,610 Now, it gets a little bit weird when we start looking at this. 5 00:00:14,610 --> 00:00:18,700 So this represents a function that accepts a one argument and produces a result. 6 00:00:18,700 --> 00:00:24,470 So we're gonna click into here and this is a function functional interface. 7 00:00:24,470 --> 00:00:25,630 It gets a little weird talking about it. 8 00:00:25,630 --> 00:00:27,900 Don't worry, I didn't pick up a stutter. 9 00:00:27,900 --> 00:00:33,530 So, a function takes a single argument and produces a result. 10 00:00:33,530 --> 00:00:38,190 And both the parameter type and the return value are specified, right? 11 00:00:38,190 --> 00:00:40,047 That's what the T and the R are. 12 00:00:40,047 --> 00:00:44,912 So T is the type that's passed in and R is the result type. 13 00:00:44,912 --> 00:00:46,353 So let's swing down. 14 00:00:46,353 --> 00:00:50,250 Let's take a look at the single abstract method and it's called apply. 15 00:00:50,250 --> 00:00:54,570 So again, it accepts a T and it returns an R. 16 00:00:54,570 --> 00:00:59,350 So, when you define the function, you specify what type it expects and 17 00:00:59,350 --> 00:01:01,510 then the type of the return value. 18 00:01:01,510 --> 00:01:03,980 So as you can imagine, these are great for 19 00:01:03,980 --> 00:01:08,620 transforming values from one to another and going between types. 20 00:01:08,620 --> 00:01:12,570 They both can be strings but you can switch between types. 21 00:01:12,570 --> 00:01:15,640 We'll get our feet wet with these here in a bit when we explore streams. 22 00:01:15,640 --> 00:01:19,580 Another thing that I wanted to point out here in the docs, while we're here. 23 00:01:19,580 --> 00:01:24,320 There are these things called default methods which are new to java eight 24 00:01:24,320 --> 00:01:27,630 interfaces and they allow you to change several functions together. 25 00:01:27,630 --> 00:01:31,560 So if we click here, if we click this default methods, we'll see that you can go 26 00:01:31,560 --> 00:01:34,480 on every function that has this method that says andThen and 27 00:01:34,480 --> 00:01:36,780 you can pass in a function okay. 28 00:01:36,780 --> 00:01:40,440 So you can kind of chain several of these functions together. 29 00:01:40,440 --> 00:01:43,120 You call your function and then call another and then another and 30 00:01:43,120 --> 00:01:45,300 it just sort of goes flowing between them. 31 00:01:45,300 --> 00:01:50,350 This is called functional composition and as you guessed, I'm gonna go ahead and 32 00:01:50,350 --> 00:01:51,830 park that right now. 33 00:01:51,830 --> 00:01:54,610 Let's flip over and let's write that here. 34 00:01:54,610 --> 00:01:57,080 We'll write functional composition. 35 00:01:57,080 --> 00:02:01,479 We are getting a pretty healthy looking list for the parking lot there. 36 00:02:01,479 --> 00:02:06,145 Can't wait to go over this stuff, but we'll get to it in a bit, don't worry. 37 00:02:06,145 --> 00:02:07,665 So about these default methods, 38 00:02:07,665 --> 00:02:11,042 check the teacher's notes if it needs some more information about that. 39 00:02:11,042 --> 00:02:12,437 Other super powerful and 40 00:02:12,437 --> 00:02:18,070 there's a couple of these interfaces that have some pretty handy default methods. 41 00:02:18,070 --> 00:02:23,930 Okay and last but definitely not least, let's check out Predicate. 42 00:02:25,950 --> 00:02:27,830 Predicate is our shape here, 43 00:02:27,830 --> 00:02:32,580 and it represents a Predicate, thank you, boolean-valued function of one argument. 44 00:02:34,130 --> 00:02:39,190 So what happens here is if we look at this abstract method, it has an abstract 45 00:02:39,190 --> 00:02:44,020 method of name test and it will take a type t and always return true or 46 00:02:44,020 --> 00:02:49,030 false so it's really kind of a specific type of function right, of the function, 47 00:02:49,030 --> 00:02:55,040 functional interface so it will take a type and return a true or false. 48 00:02:55,040 --> 00:02:55,780 Pretty nice, right? 49 00:02:55,780 --> 00:02:57,490 It's very specific. 50 00:02:57,490 --> 00:03:00,740 So, just like functions, it has a default method and 51 00:03:00,740 --> 00:03:02,080 you can kind of chain these together. 52 00:03:02,080 --> 00:03:08,410 So you can say, this and this or not this, so you can negate it or do an or. 53 00:03:08,410 --> 00:03:12,120 We'll get our hands wet with these real soon as we use these 54 00:03:12,120 --> 00:03:14,980 often when trying to filter values. 55 00:03:14,980 --> 00:03:19,150 The java.util.function package has some wonderful functional interfaces 56 00:03:19,150 --> 00:03:22,320 that we can lean on when diving deeper into functional programming. 57 00:03:22,320 --> 00:03:25,360 I'm glad you got a chance to see how to explore the out of the box options. 58 00:03:26,480 --> 00:03:29,250 Let's review the four basic shapes that I'd like you to be 59 00:03:29,250 --> 00:03:30,500 familiar with real quick. 60 00:03:30,500 --> 00:03:35,250 Consumer, a function that accepts something and returns nothing. 61 00:03:35,250 --> 00:03:38,350 These are useful when you need to be provided a value in context. 62 00:03:39,590 --> 00:03:43,420 Supplier, this accepts nothing but returns a value. 63 00:03:43,420 --> 00:03:47,740 This is great when you need something initially, like we did in our yell method. 64 00:03:47,740 --> 00:03:51,690 Function, this takes a single argument, and returns a value. 65 00:03:51,690 --> 00:03:54,200 This is great for transformation. 66 00:03:54,200 --> 00:03:56,670 And Predicate accepts a parameter, and 67 00:03:56,670 --> 00:04:00,550 returns a boolean value, helpful when filtering value. 68 00:04:00,550 --> 00:04:05,023 You probably noticed that the remaining functional interfaces in the package were 69 00:04:05,023 --> 00:04:07,039 either a typed version of those four or 70 00:04:07,039 --> 00:04:11,075 a version that changes the amount of parameters that the function accepts. 71 00:04:11,075 --> 00:04:15,730 The amount of parameters that a function accepts is referred to as parity. 72 00:04:15,730 --> 00:04:19,740 If it helps, I remember the bi prefix like the bicycle, like two wheels, 73 00:04:19,740 --> 00:04:21,730 like this bike here, two wheels. 74 00:04:21,730 --> 00:04:25,720 Now, that is versus the uni prefix like unicycle which means one wheel. 75 00:04:26,920 --> 00:04:30,060 So now, we've got our basic function shapes, we are ready to move on to 76 00:04:30,060 --> 00:04:35,110 the next level of functional programming in Java, the super powerful streams API. 77 00:04:35,110 --> 00:04:36,380 Let's go plug in those shapes.