1 00:00:00,420 --> 00:00:05,170 Scala provides us with a wide array of built in, higher order functions. 2 00:00:05,170 --> 00:00:08,690 Most of the time, we'll make use of these built in functions. 3 00:00:08,690 --> 00:00:13,570 However, there may be times where we want to create our own higher order function. 4 00:00:13,570 --> 00:00:15,120 Let's take a look at how to do that. 5 00:00:17,150 --> 00:00:21,270 In an earlier video, we created a map which holds the name and 6 00:00:21,270 --> 00:00:23,505 ranking of our avengers. 7 00:00:23,505 --> 00:00:27,806 Let's create a higher order function which takes three avengers and 8 00:00:27,806 --> 00:00:31,301 compares their ranking by finding the men and the mask. 9 00:00:31,301 --> 00:00:34,060 Let's copy the avengers map we have created earlier. 10 00:00:54,233 --> 00:00:57,871 Now let's take out the rankings from these and store them into variables. 11 00:01:38,490 --> 00:01:39,100 Awesome. 12 00:01:39,100 --> 00:01:40,900 Now we have the three rankings. 13 00:01:40,900 --> 00:01:42,330 Let's use them in our function. 14 00:02:19,560 --> 00:02:20,140 Great. 15 00:02:20,140 --> 00:02:22,360 We've created a function called compare, 16 00:02:22,360 --> 00:02:27,210 which takes three arguments that are integers and has a function noted by f: 17 00:02:27,210 --> 00:02:31,810 that takes these three arguments and combines them to one. 18 00:02:31,810 --> 00:02:34,580 There are several ways we can invoke such a function. 19 00:02:34,580 --> 00:02:36,680 Passing the values of the rankings for 20 00:02:36,680 --> 00:02:41,910 each Avenger, along with the function we would like to use to compare the three. 21 00:02:41,910 --> 00:02:46,596 First, let's get an idea of which one of our avengers has the best ranking. 22 00:03:05,000 --> 00:03:05,900 In this example, 23 00:03:05,900 --> 00:03:10,680 we're comparing the three arguments passed in by finding the minimum between them. 24 00:03:10,680 --> 00:03:14,040 Remember our compare function takes a function parameter 25 00:03:14,040 --> 00:03:16,390 which accepts a three arguments. 26 00:03:16,390 --> 00:03:21,340 The function we pass to compare is the min function with underscores as wildcards 27 00:03:21,340 --> 00:03:25,840 which represent avenger1, avenger2 and avenger3. 28 00:03:25,840 --> 00:03:28,498 Let's recompile our code and check out our results. 29 00:03:36,850 --> 00:03:37,733 As expected, 30 00:03:37,733 --> 00:03:43,070 we get eight as a result, which is the minimum ranking between our Avengers. 31 00:03:43,070 --> 00:03:46,350 A ranking of eight belongs to Captain America. 32 00:03:46,350 --> 00:03:52,290 Captain America is our best ranked Avenger, as his value is closest to one. 33 00:03:52,290 --> 00:03:56,985 Similarly, we can pass another function and find out the worst ranked Avenger. 34 00:04:27,910 --> 00:04:28,656 In this case, 35 00:04:28,656 --> 00:04:33,130 we compared the three arguments passed by finding the maximum between them. 36 00:04:33,130 --> 00:04:36,520 Remember, our compare function takes a function parameter 37 00:04:36,520 --> 00:04:38,980 which accepts three arguments. 38 00:04:38,980 --> 00:04:43,310 The function we passed to compare is the max function with underscores as 39 00:04:43,310 --> 00:04:48,540 wildcards which represent avenger one, avenger two, and avenger three. 40 00:04:48,540 --> 00:04:49,948 Let's check out the results. 41 00:04:54,850 --> 00:04:59,610 Awesome, as expected we got the ranking that's furthest away from one between our 42 00:04:59,610 --> 00:05:01,450 three avengers. 43 00:05:01,450 --> 00:05:02,170 Cool. 44 00:05:02,170 --> 00:05:05,960 Not only did we learn about how to use higher-order functions, but 45 00:05:05,960 --> 00:05:08,240 we also learned how to create our own. 46 00:05:08,240 --> 00:05:11,860 In the next video, we'll discuss another topic, closures. 47 00:05:11,860 --> 00:05:12,450 See you there.