1
00:00:00,610 --> 00:00:01,900
In this video.

2
00:00:02,020 --> 00:00:06,340
Let's talk about high order functions.

3
00:00:07,210 --> 00:00:13,440
Understanding this concept is going to help you to understand the code that you write.

4
00:00:13,450 --> 00:00:15,940
So what is higher order functions?

5
00:00:15,970 --> 00:00:20,640
Well, the previous video we talk about functions as a first class citizen.

6
00:00:20,650 --> 00:00:22,690
So what is high order functions?

7
00:00:26,350 --> 00:00:34,240
High honor function simply implements that functions, accepting functions as a segment and is also

8
00:00:34,240 --> 00:00:37,000
called callback functions.

9
00:00:37,120 --> 00:00:43,930
Again, callback function simply means that a function that is being passed in as an argument to and

10
00:00:43,930 --> 00:00:49,210
a function is said to be callback functions or high order functions.

11
00:00:49,570 --> 00:00:54,730
Well, in JavaScript, high order functions are everywhere.

12
00:00:55,060 --> 00:01:00,160
Talking about a real matter is the old David Ward high order functions.

13
00:01:00,580 --> 00:01:05,349
So let's see examples of high order functions in JavaScript.

14
00:01:05,379 --> 00:01:09,160
Do you remember we talk about reduced function.

15
00:01:09,580 --> 00:01:12,940
And you also talk about find method.

16
00:01:14,320 --> 00:01:16,150
And for Ouattara.

17
00:01:16,920 --> 00:01:18,420
And map.

18
00:01:18,570 --> 00:01:25,890
All these are based on high order function because it takes in a function as an argument.

19
00:01:26,220 --> 00:01:32,520
So let's say that we have this data here, let's say coms.

20
00:01:32,550 --> 00:01:38,250
Let's say quantity here is equal to array of, let's say one.

21
00:01:38,880 --> 00:01:44,070
Let's say one, four, five, and let's see these values.

22
00:01:44,100 --> 00:01:52,470
Now, I want to loop through this and display individual record or I want to add up the values from

23
00:01:52,470 --> 00:01:54,680
this and I can use reduce.

24
00:01:54,690 --> 00:01:58,140
So let's make our life easier and use, let's say find.

25
00:01:58,140 --> 00:02:02,590
I want to find if a value exists in this particular array.

26
00:02:02,610 --> 00:02:04,280
So here we go.

27
00:02:04,290 --> 00:02:08,600
I will take my quantity array and then dot find.

28
00:02:08,610 --> 00:02:16,710
And here we go to see that we need to pass in a function, not a variable, a function.

29
00:02:16,710 --> 00:02:19,230
And that function has no name.

30
00:02:19,260 --> 00:02:21,940
That's what you call anonymous function.

31
00:02:21,960 --> 00:02:22,950
Here we go.

32
00:02:23,100 --> 00:02:31,500
As that you see many that this function is high order function because it's taking a function as an

33
00:02:31,500 --> 00:02:32,340
argument.

34
00:02:32,430 --> 00:02:36,780
So here I can go ahead and retain or console.log it.

35
00:02:36,780 --> 00:02:45,540
So inside my callback here I have access to my record as Q to where let me say data so I can quickly

36
00:02:45,540 --> 00:02:49,450
console.log it as that and let's see.

37
00:02:49,470 --> 00:02:51,510
And indeed I have the record.

38
00:02:51,720 --> 00:02:58,680
So find is said to be high order function because it's taking in a function as a callback.

39
00:02:58,860 --> 00:03:05,220
So let's see how we can create our own high order function, a function that's going to take in the

40
00:03:05,220 --> 00:03:06,930
function as a callback.

41
00:03:06,940 --> 00:03:07,870
So let's see.

42
00:03:07,890 --> 00:03:11,880
So let's remove this once and let's start with clean sheet.

43
00:03:12,150 --> 00:03:22,110
Let's say that we want to calculate a bill and at same time we want a function to display the result

44
00:03:22,110 --> 00:03:23,010
of the bill.

45
00:03:23,220 --> 00:03:24,960
So we have two functions here.

46
00:03:24,960 --> 00:03:34,890
Say, calculate, calculate bill, and then second function is going to just display the bill as that.

47
00:03:34,890 --> 00:03:35,970
So here we go.

48
00:03:36,300 --> 00:03:43,440
So for the first one, for calculate, let's come here and say cost calculate.

49
00:03:44,910 --> 00:03:47,580
Bill and this going to be.

50
00:03:48,740 --> 00:03:55,970
A function, and this function would take a the quantity I want to calculate and then the price I want

51
00:03:55,970 --> 00:03:56,830
to calculate.

52
00:03:56,840 --> 00:03:58,030
So here we go.

53
00:03:58,040 --> 00:04:05,600
So I will return here as my quantity multiplied by the price.

54
00:04:05,810 --> 00:04:07,010
Now, here we go.

55
00:04:07,460 --> 00:04:11,990
So the next one, I'm going to be this play, what they build.

56
00:04:11,990 --> 00:04:18,200
Because here the response or the results coming back from this one is going to be the build I want to

57
00:04:18,200 --> 00:04:23,720
pay and this function is going to take the result here and then display it.

58
00:04:24,380 --> 00:04:26,900
So let's see how we are going to write that function.

59
00:04:26,900 --> 00:04:31,850
So it's going to be cost, let's say, display.

60
00:04:33,440 --> 00:04:39,950
Display below, and this function is going to take in a function as an argument.

61
00:04:39,950 --> 00:04:47,360
So here we can name it whatever you want as a function or callback or the actual name.

62
00:04:47,390 --> 00:04:48,800
The choice is yours.

63
00:04:48,800 --> 00:04:51,200
So let's use a name that is more descriptive.

64
00:04:51,200 --> 00:04:53,810
So let's say calculate below function.

65
00:04:53,810 --> 00:04:57,950
We know that this function is going to take in the calculate function.

66
00:04:58,220 --> 00:05:03,770
So here let's just console log into the console, but here we go.

67
00:05:03,800 --> 00:05:04,100
Okay.

68
00:05:04,100 --> 00:05:08,210
Before I do that, let me show you how we can I mean, return from this function.

69
00:05:08,210 --> 00:05:10,820
We can return from this function in two ways.

70
00:05:10,820 --> 00:05:16,940
One, either call the function directly, call build function inside us.

71
00:05:18,630 --> 00:05:27,530
Call be a function and as that did not pass in the value I want to calculate or I can remove it as that

72
00:05:27,540 --> 00:05:30,960
and upon calling our pass in my agreement.

73
00:05:30,990 --> 00:05:32,620
That's the right way to go.

74
00:05:32,640 --> 00:05:37,110
So for this one, let's console, log it to the console and start.

75
00:05:37,140 --> 00:05:37,800
Great.

76
00:05:37,800 --> 00:05:41,070
So now how can you call this particular function?

77
00:05:41,220 --> 00:05:44,760
Remember, I'm going to call this play bill.

78
00:05:45,690 --> 00:05:49,020
Display beer, then this one would take a.

79
00:05:49,090 --> 00:05:50,700
You see that callback function here.

80
00:05:50,700 --> 00:05:55,830
Is it I have calculate beer function any then I'll pass in my calculate beer function.

81
00:05:56,990 --> 00:05:57,710
As that.

82
00:05:57,710 --> 00:06:02,420
And this function takes inward to agreement quantity and price.

83
00:06:02,420 --> 00:06:05,540
So let's say quantity is two and price is five.

84
00:06:05,690 --> 00:06:08,420
Let's see, now I have ten.

85
00:06:08,420 --> 00:06:10,160
So how is it?

