WEBVTT

00:01.040 --> 00:09.380
Higher order functions are functions that either take functions as arguments or return functions.

00:09.380 --> 00:18.380
In this case, apply is a higher order function because, as you may be noticed or not, this OP argument

00:18.380 --> 00:24.650
is then called as a function which makes apply the higher order function.

00:25.400 --> 00:34.010
So we can also add an arrow function like this and pass it to the call when we are calling apply, which

00:34.010 --> 00:37.550
makes apply the higher order function.

00:41.000 --> 00:42.740
Let's see another example.

00:42.740 --> 00:45.380
So consider that we have this array.

00:45.380 --> 00:48.410
We would like to sort the people by age.

00:49.220 --> 00:52.640
We can use use sort for that passing this array.

00:52.790 --> 00:59.930
And the second argument in this case is just an arrow function that is doing the sorting logic.

00:59.930 --> 01:05.420
That's why you sought is an higher order function.

01:05.960 --> 01:11.450
So function that either takes functions as arguments or returns a function.

01:11.450 --> 01:14.000
And that's the result in this case.

01:15.620 --> 01:19.550
So we've already covered higher order functions before.

01:19.700 --> 01:27.200
If I now search for the Arraymap function inside the project, I can find it in the arrow functions

01:27.200 --> 01:30.170
and the anonymous functions files.

01:30.800 --> 01:38.120
So actually we've been using higher order functions without knowing that they are higher order functions.

01:38.150 --> 01:40.430
So Arraymap is a good example.

01:40.430 --> 01:46.670
And a lot of array related functions are also higher order functions.

01:46.670 --> 01:52.340
So I don't think there is a point in rewriting the examples from the slides.

01:52.370 --> 01:56.930
Instead, let's create a higher order function that returns a function.

01:58.010 --> 02:01.400
So let's create a higher Order.

02:01.880 --> 02:04.460
FBX file for this example.

02:05.090 --> 02:12.890
PHP tag and let me put some data because I just don't want to waste your time typing this on the video.

02:13.250 --> 02:20.360
So what I'd like to do with this array, this multi-dimensional array is from this main array, I only

02:20.360 --> 02:26.240
want the elements that are under the key role will have an admin.

02:26.240 --> 02:30.380
So at least that's the first filter that I would like to create.

02:30.380 --> 02:37.250
But I would like to be also a little bit more flexible with what filters I can create.

02:37.250 --> 02:43.970
So I'm not gonna make a specific function that will perform the array filtering for that key.

02:44.120 --> 02:49.070
We're going to create a generic higher order function that we can then reuse.

02:49.070 --> 02:52.970
However we want to filter for different properties.

02:53.870 --> 02:59.000
So I'm creating a function I'm going to call it create filter.

02:59.000 --> 03:07.370
And what it needs to know is which key I'm interested in and what needs to be value of that key.

03:10.790 --> 03:18.140
So that's not a higher order function yet, but now it is because I am returning a function from it.

03:18.890 --> 03:28.760
So this function for every item that it receives, it just needs to compare the item key with value.

03:28.790 --> 03:31.310
That's a strict equality operator.

03:31.340 --> 03:33.770
Now this might look complex.

03:33.770 --> 03:38.900
Right now you might not understand what the hell I'm doing, but don't worry about that.

03:38.930 --> 03:42.890
We're gonna run this function and I'm gonna explain it.

03:42.920 --> 03:50.480
So first, since this is a higher order function which returns a new function, this means we first

03:50.480 --> 03:55.910
need to call it with some parameters to get a function that only then we can use.

03:55.910 --> 03:58.460
So that's the purpose here.

03:58.490 --> 04:02.780
So I am creating a function that I'm going gonna call is admin.

04:02.780 --> 04:06.380
Remember, functions are first class citizens in PHP.

04:06.410 --> 04:11.720
They can be passed around as arguments, returned from functions, or assigned to variables.

04:11.720 --> 04:13.190
That's why I can do that.

04:13.220 --> 04:19.370
Now I'm calling create filter and take a look at the arguments that we have.

04:19.400 --> 04:21.320
We've got the key and value.

04:21.350 --> 04:24.110
Now those are the keys.

04:26.120 --> 04:31.250
So I'd like the role key to be exactly an admin.

04:31.250 --> 04:35.780
So I am creating a filtering function that does exactly this.

04:35.810 --> 04:37.400
What I'm gonna do with it.

04:37.430 --> 04:43.310
Well I'm gonna get the admins using the built in PHP array filter.

04:43.310 --> 04:44.270
By the way.

04:44.300 --> 04:53.840
There's also a higher order function, because one of the arguments here is a function, our filtering

04:53.840 --> 04:54.800
function.

04:54.800 --> 05:02.970
So the way it works is that for every element from the past array which is users this main array.

05:04.620 --> 05:07.920
Every item will be passed to this function.

05:07.920 --> 05:10.650
So how does our function look like?

05:10.650 --> 05:14.280
Well, we've created it using this higher order function.

05:14.370 --> 05:17.760
And it's gonna receive an item.

05:17.760 --> 05:22.980
That's why when we refer to item key we actually refer.

05:23.100 --> 05:25.590
This is for example an item.

05:25.950 --> 05:29.940
And in this item that's a key and value.

05:29.940 --> 05:32.670
Well that's this part.

05:33.870 --> 05:38.640
So we should be getting only the users that are admins.

05:38.640 --> 05:41.130
Let me squeeze that more into the screen.

05:41.130 --> 05:43.770
So it's visible what's happening.

05:43.800 --> 05:46.770
So finally I need to run that.

05:46.800 --> 05:52.200
So let's do Vardump and let's just see who's the admin.

05:52.590 --> 05:57.090
Let's open the terminal and run higher order PHP.

05:57.300 --> 06:04.250
And the result is an array with only one item because only Alice is an admin.

06:05.060 --> 06:06.050
I think that's correct.

06:06.080 --> 06:07.760
Others are users.

06:10.310 --> 06:15.710
Now we can create another example I can make another function called is Bob.

06:15.740 --> 06:21.590
If for any reason I'd like to talk to everyone who's name is Bob.

06:24.890 --> 06:27.830
So the name has to be Bob.

06:31.940 --> 06:37.490
And I can just directly vardump that without using any additional variables.

06:37.970 --> 06:44.960
So I do array filter on users and calling the is Bob function in this case.

06:46.370 --> 06:47.510
Now let's run that.

06:47.510 --> 06:58.370
And I expect in this second result to get one element inside this array which should match our requirements.

06:58.370 --> 07:00.560
So the name should be Bob.
