Function Composition8:36 with Craig Dennis
You can chain functions together to create a pipeline. Let's explore doing that.
Just like how we saw that you could chain together predicates. 0:00 We can also do that with the functional interface, function. 0:03 So you know how functions take something, process it and then return something else? 0:07 Well, you can chain together functions, so 0:13 that the return value is passed along to the next function. 0:14 Which takes something and processes it and return something else. 0:18 The whole chain is a function, right? 0:23 One that takes something and returns something else. 0:25 The entire composition itself meets the functional interface. 0:28 Now, don't worry, we'll go over it in code and 0:32 not just some word version of rock, paper, scissors. 0:34 But first, we've got to create that problem for yourselves, I got one. 0:37 So let's assume that one of the key decision makers of our app 0:42 is very particular about how they want to see dates represented in our app. 0:46 They always want to see month, space, slash, space, date, 0:51 space, only two values for the year. 0:56 It's not a very common format but they love it, and 0:58 they've asked us to be sure to always produce dates in that style. 1:02 Our dates on the job posting are in a different string format. 1:05 So, basically what we need to do is take the string from the job posting and 1:09 turn it into a date object. 1:13 And then we need to take that date and 1:15 turn that into a new format that the boss wants. 1:17 So, basically we need to take the job posting date, process some stuff and 1:20 then turn it into that specific format. 1:25 Man, I'm doing that rock, paper, scissors thing again. 1:28 Let's just go do it in code. 1:30 Okay, so let's start by seeing what we got in our job date time field. 1:31 So let's loop through the first five of our jobs, 1:38 and just print out the dates as they exist. 1:41 So first what we'll do, is we'll do jobs.stream() and 1:43 then well heck, why don't you just do that. 1:47 You display the result of the first five jobs get date time string. 1:51 There's a method on jobs called get date time string. 1:56 So pause me, give it a go, and then unpause me when you get it, and 1:59 I'll show you how I did it. 2:03 Ready, pause me, okay, so here's how I did it. 2:04 So what we'll do is we'll just flip the stream, the job coming through. 2:07 We will map that to the getDateTimeString. 2:12 And then we only want to have five. 2:17 So we will limit that to 5, and of course we'll print them out. 2:20 And that is our friend, System.out. 2:27 Println, there we go, so when we run that, 2:32 There we go, so that's the format. 2:40 So that looks kind of familiar, right? 2:42 So we're gonna need to parse that string. 2:44 I'm gonna open the documentation up on the newest Java 8 class local date time. 2:48 Cuz I don't remember exactly how it works, and it's okay to look at documentation. 2:52 So let's open that up, so I'm gonna say Java, localdatetime. 2:58 Here it is, this is new in Java 8. 3:04 So I know that we wanna take a string and turn it into a date, right? 3:07 So I'm gonna search for that, I'm gonna search for String. 3:13 So here's format, this is great, this is going the other way, right? 3:16 So we take format and it will create a string, 3:19 we'll have to remember that for later. 3:21 Let's do another search, perfect, here it is, parse, that's what it was. 3:23 So it obtains an instance of LocalDateTime 3:27 from a text string using a specific formatter. 3:30 And the formatters are here and if I remember correctly, 3:32 I think there's these built in formats. 3:36 Here they are, perfect. 3:39 So I think that this looks familiar. 3:41 Here it is, look, check this out. 3:43 So RFC 1123, so RFC is Request For Comments. 3:46 It's a standards document, so that is totally what's going on over here, right. 3:49 That's the same, the same style, RFC 1123 Date Time. 3:53 Let's define a function, let's do that right here in the explore method. 4:01 We'll just make a local one here. 4:05 We'll make a function, that takes a string, and returns the, LocalDateTime. 4:07 And let's call that indeedDateConverter. 4:15 Okay, and so what that's gonna do is that's gonna take a string, 4:20 it's gonna take a dateString and it's gonna return a LocalDateTime. 4:23 Let's just go ahead, I'll drop this down to a new line here. 4:27 It's gonna take a LocalDateTime and 4:30 it's gonna say .parse, and we'll pass in that dateString, 4:34 and it's gonna take a DateTimeFormatter. 4:39 And we'll see there is that RFC down here, 4:43 there we go, DateTimeFormatter_RFC_Date_Time. 4:46 Let's also put this on it's own line, just keep things clean, 4:50 we don't have much space here on this smaller screen. 4:54 Cool, so now we have a function that does just that, right? 4:59 It takes a date string and it returns a new date. 5:02 So we can just pop that into our stream, right? 5:06 Cuz again map takes a function, right? 5:09 So we can just pop in a map, and I'll push in this indeedDateConverter. 5:11 Cool, and now if we run it, awesome, 5:17 so this is the default, this is actually a date. 5:21 But this is a two string of a date but 5:23 you'll notice it's a different version, right? 5:25 It read it in and it's writing it out so awesome, we've got our dates. 5:26 Okay, so now what we need to do is we need to take that date and 5:30 turn it into our new strain format. 5:34 So remember, that was like that was this format here. 5:37 That was like 3/15 the ides of March 17 with spaces like that. 5:40 I don't know why they like that but they do. 5:48 Sometimes you don't get to make the decisions, right. 5:51 So, let's write that function. 5:53 So what that functions looks like is it's a function and 5:55 that one takes a LocalDateTime and it returns a string. 5:58 And we're gonna call that site date, cuz this is for 6:04 a website and we'll do the same sort of thing. 6:06 So this is gonna take a date and 6:11 then it's gonna use that format that we saw on the date time, right? 6:13 So it's gonna take format and it's gonna take a date time formatter and 6:17 you can make your own. 6:22 Because this is a custom one, right, so this is our own. 6:25 So I looked that up, and the way that you do 6:27 that is you do M / d and then YY for year year. 6:32 There's more of that in the teacher's notes. 6:38 I can link to where I found that, cool. 6:39 So now we've got a function that takes a date and returns a string. 6:42 So we can put that right afterwards, right? 6:46 So we'll do .map and we'll say, in our weird formatter, 6:48 which is called siteDateStringConverter, which is a function. 6:53 And again, maps take functions, so we're gonna refresh that. 6:56 There it is, nice, you're gonna be so 7:01 happy with that format and that works, right? 7:03 But it kinda weird to pass it through two functions just like that in 7:06 the map, right? 7:09 We don't really care about this intermediary steps where it becomes 7:11 a day, right? 7:14 We just want it to be one. 7:15 So actually the same way that predicates can change, so can functions. 7:16 And the way that that works is there's a thing called .andThen. 7:21 So it will run whatever came from the function, pass it into the other one. 7:25 See, just like that, and now if we run it we get the same thing. 7:29 And of course, the real power exists when you take this together. 7:35 I'm gonna extract this to a variable, and it's gonna be a function. 7:39 And lets call this 7:44 IndeedToSiteDateStringConverter. 7:47 And I'll put this on a new line so that it's clean. 7:56 It's pretty cool right? 8:00 So I could actually export this and make this available all over the place and 8:00 then anybody can just use this. 8:05 And it's one thing and it's running through both of those, pretty cool right? 8:07 We composed those two functions together to make one. 8:10 It's kind of like plugging legos together, you know what? 8:13 Let's take a look at our Parking Lot document. 8:16 We've got Functional Composition covered. 8:20 You know what would be cool? 8:24 What if we had a more generic method that actually created a function 8:26 that we could use to go between any kind of Date Time strings? 8:30 Let's take that on right after this break. 8:34
You need to sign up for Treehouse in order to download course files.Sign up