WEBVTT

00:00.330 --> 00:05.190
Hello again! In this video, we are going to have a practical demonstration of lambda expressions.

00:05.760 --> 00:11.280
I am going to take the equal_strings function we wrote earlier in the course and rewrite that, using

00:11.280 --> 00:13.980
an algorithm and a lambda expression.

00:15.990 --> 00:22.260
We wrote the equal_strings function in the video on Character Functions. It takes two string arguments,

00:22.530 --> 00:26.730
and it compares each character in the string, after converting them to uppercase.

00:28.290 --> 00:32.310
So here is the code, which we have seen before.

00:35.610 --> 00:38.010
We have a test() function and a main().

00:39.900 --> 00:45.120
So this will ignore differences in the case, but it will not ignore different characters.

00:45.750 --> 00:47.790
We are going to use the equal() algorithm.

00:48.150 --> 00:50.640
This compares the elements in two containers.

00:51.240 --> 00:56.730
It takes 4 arguments: an iterator range for the first container, and an iterator range for the

00:56.730 --> 00:59.100
second container, and it returns bool.

00:59.760 --> 01:04.800
This will go through and compare each element in the first container with the corresponding element

01:04.860 --> 01:12.990
in the second container. As soon as it encounters a discrepancy - a mismatch - it returns false. If it gets

01:12.990 --> 01:15.690
through the first container without finding any mismatches,

01:16.110 --> 01:17.370
then it returns true.

01:18.300 --> 01:22.980
If the first container has more elements, it returns false, regardless of the data.

01:23.760 --> 01:28.320
If the second container has more elements, then those extra elements are ignored.

01:30.270 --> 01:36.450
By default, this will use the equality operator to compare the elements. And if the containers are

01:36.450 --> 01:42.000
strings, then that means it is going to do a case-sensitive comparison of each character in the string.

01:43.950 --> 01:50.100
There is an overload of the equal algorithm, which takes a callable object as an optional extra arguments.

01:50.730 --> 01:56.100
And we can use this predicate to change the way that the equal() function compares the elements.

01:57.870 --> 02:03.510
This predicate is going to be called instead of the equality operator so we can make this do whatever

02:03.510 --> 02:07.230
we want for - to change the criteria for equality.

02:08.860 --> 02:13.660
In our example, we want two characters to be equal if they have the same value after being converted

02:13.660 --> 02:14.530
to uppercase.

02:17.700 --> 02:22.920
The lambda expression is going to be called with one character from each of the two strings. Then we

02:22.920 --> 02:29.640
convert each character to uppercase. And then we compare them, and we return true or false, depending

02:29.640 --> 02:30.950
on whether they are equal or not.

02:31.350 --> 02:31.920
And that is it!

02:33.270 --> 02:37.350
So here is the version with the lambda expression. You will notice this is much shorter.

02:37.650 --> 02:44.100
The entire program fits on a single screen (almost!), whereas before the equal_string() function took up

02:44.100 --> 02:44.970
an entire screen.

02:46.050 --> 02:48.000
So here is the equal strings function,

02:48.000 --> 02:51.090
again. It just calls the equal() algorithm.

02:51.780 --> 02:55.410
So we pass the iterator range for the two string arguments.

02:56.070 --> 02:59.520
The first string and the second string argument.

03:00.240 --> 03:05.490
Then we have our lambda expression, which takes a character from each string. And then, in the body,

03:05.490 --> 03:11.310
we convert the characters to uppercase and compare them. And the rest of the program is the same.

03:14.860 --> 03:15.490
And there we are.

03:19.000 --> 03:24.970
So the advantage of this is, we replaced a screen full of code, with lots of things that could go wrong, with

03:24.970 --> 03:26.440
just one simple statement.

03:26.920 --> 03:32.830
And perhaps this statement is quite complex if you are not used to it, but it does become second nature.

03:33.070 --> 03:39.040
You can actually format it slightly differently, to make it a bit easier to read. Depending on your

03:39.040 --> 03:39.400
style.

03:40.810 --> 03:42.460
OK, so that is it for this video.

03:42.880 --> 03:45.700
I will see you next time, but until then, keep coding!
