1 00:00:06,840 --> 00:00:07,320 Hello. 2 00:00:07,830 --> 00:00:14,130 In this lecture, we're going to talk about Question six, what are Funk's and lambda expressions? 3 00:00:14,460 --> 00:00:21,090 When thinking about objects, we usually think about things that carry some data as the payload, as 4 00:00:21,090 --> 00:00:24,770 well as the operations that can be executed on this data. 5 00:00:25,210 --> 00:00:32,880 An integer holds the value of five a person object, Holt's name, last name and year of birth, and 6 00:00:32,880 --> 00:00:35,430 perhaps a method calculating the age. 7 00:00:35,790 --> 00:00:41,730 We can easily understand that we can have a variable of such types, or that we can pass objects of 8 00:00:41,730 --> 00:00:43,590 those types as parameters. 9 00:00:43,950 --> 00:00:47,040 But could we have variables holding functions? 10 00:00:47,370 --> 00:00:49,830 Can we pass a function as a parameter? 11 00:00:50,220 --> 00:00:53,520 As it turns out, we can, and it's extremely useful. 12 00:00:53,940 --> 00:01:01,200 Let me show you one example I want to write a method that works if any number in our collection is larger 13 00:01:01,210 --> 00:01:01,830 than 10. 14 00:01:02,250 --> 00:01:03,780 This is how I could do it. 15 00:01:05,070 --> 00:01:05,760 All right. 16 00:01:06,180 --> 00:01:07,870 This method is pretty simple. 17 00:01:08,310 --> 00:01:12,000 It's just it allows the collection, and for each number, it's jokes. 18 00:01:12,000 --> 00:01:15,420 If it's larger than 10, in this case, it returns true. 19 00:01:15,840 --> 00:01:18,240 Otherwise, false will be returned. 20 00:01:18,810 --> 00:01:26,070 But soon after, I am asked to add another method that checks if any number in the collection is given. 21 00:01:26,490 --> 00:01:28,170 Let's create such a method. 22 00:01:37,820 --> 00:01:41,990 The problem is that this method is almost the same as the previous one. 23 00:01:42,530 --> 00:01:45,320 The only place in which they differ is this. 24 00:01:45,980 --> 00:01:47,690 This is the part that differs. 25 00:01:47,720 --> 00:01:53,600 So if I wanted to refactor those two methods into one, I would need to make this part a parameter. 26 00:01:54,050 --> 00:01:56,090 But what is this part exactly? 27 00:01:56,420 --> 00:02:00,560 At first glance, you may think it's a Boolean, but that's not correct. 28 00:02:00,860 --> 00:02:06,800 I couldn't pass this Boolean to those methods, as it may differ for each of the elements of the numbers 29 00:02:06,800 --> 00:02:07,460 collection. 30 00:02:07,910 --> 00:02:14,030 So it's not a Boyan, it's a function that takes a number and returns a Boolean. 31 00:02:14,450 --> 00:02:20,750 If I wanted to refactor those methods and make them a single method, it would need to take a function 32 00:02:20,750 --> 00:02:21,830 as a parameter. 33 00:02:22,100 --> 00:02:25,010 And for this, we can use the flag type. 34 00:02:25,370 --> 00:02:27,050 Let me show you how it looks. 35 00:02:36,370 --> 00:02:43,270 This fund can be assigned and the function that is taking a number and returning Abu, as you can see 36 00:02:43,330 --> 00:02:49,990 fund is generic and by type parameters, we define what is the return type of the function and what 37 00:02:49,990 --> 00:02:51,460 parameters it takes. 38 00:02:52,000 --> 00:02:57,490 The last type parameter is always the return type and everything preceding it. 39 00:02:57,640 --> 00:02:59,440 Are the types of parameters. 40 00:03:06,710 --> 00:03:13,370 For example, this Baraboo can be assigned and the function that takes an end date, time and string 41 00:03:13,370 --> 00:03:17,840 parameters and returns other similar for voice functions. 42 00:03:17,960 --> 00:03:19,700 We must use action type. 43 00:03:24,700 --> 00:03:29,560 This variable can represent and the vote function, taking two strings and a ball. 44 00:03:30,280 --> 00:03:32,020 Let's go back to refactoring. 45 00:03:32,260 --> 00:03:39,100 I want to have a single is animated, taking a collection of numbers and the function that defines the 46 00:03:39,100 --> 00:03:40,720 predicate that will be tracked. 47 00:03:41,290 --> 00:03:48,760 Now, instead of using number is larger than 10 or number is even, I will simply use the predicate. 48 00:03:52,990 --> 00:03:57,490 As you can see, we can call the funk object like another method. 49 00:03:57,910 --> 00:03:59,260 Let's use this method now. 50 00:03:59,530 --> 00:04:04,510 First of all, I will define is larger than 10 and is even methods. 51 00:04:05,660 --> 00:04:09,920 Now I can use those methods as parameters of that is animated. 52 00:04:24,180 --> 00:04:28,830 As you can see, we can simply assign methods to the variables of funk type. 53 00:04:29,190 --> 00:04:35,840 Naturally, it wouldn't compile if the signatures of the methods would not match the signatures of funks. 54 00:04:36,360 --> 00:04:41,760 I can, of course, keep declaring variables and simply pass the methods as parameters. 55 00:04:46,570 --> 00:04:48,400 Let's make sure this code works. 56 00:04:50,480 --> 00:04:55,550 Right through has been treated twice because this number is larger than 10. 57 00:04:55,760 --> 00:04:57,170 And this is even. 58 00:04:58,490 --> 00:04:59,010 Right. 59 00:04:59,210 --> 00:05:03,650 We learned that in C-sharp, we can treat functions like other objects. 60 00:05:03,920 --> 00:05:07,820 We can assign them to variables or pass them as parameters. 61 00:05:08,450 --> 00:05:12,090 Funk and action are the types that allow us to do this. 62 00:05:12,650 --> 00:05:18,320 Now let's move on to the ramped up expressions before I explain what they are exactly. 63 00:05:18,410 --> 00:05:20,630 Let's take another look at those two methods. 64 00:05:21,080 --> 00:05:23,090 Those methods are quite specific. 65 00:05:23,390 --> 00:05:26,300 I'm not likely to use them anywhere else. 66 00:05:26,450 --> 00:05:30,800 They needed to declare them in this class so I can pass them as parameters. 67 00:05:31,260 --> 00:05:38,000 Imagine you have a class that does plenty of a very specific tax on collections, like checking if any 68 00:05:38,000 --> 00:05:40,070 number is divisible by five. 69 00:05:40,280 --> 00:05:47,600 If the string is longer than 10 letters, or if any person is named John, we would clutter our class 70 00:05:47,600 --> 00:05:53,060 with tiny, simple methods that took those feelings and are not used anywhere else. 71 00:05:53,570 --> 00:05:55,040 That's not a good approach. 72 00:05:55,340 --> 00:06:01,880 It would be better if we could have an easy, short way of defining small, simple and specific functions. 73 00:06:02,120 --> 00:06:05,090 And that's exactly what the expressions are for. 74 00:06:05,580 --> 00:06:09,890 First, let me show you how they look, and then I will explain in detail. 75 00:06:15,200 --> 00:06:22,550 As you can see now, I don't use the is larger than ten and even the methods, I can safely remove them. 76 00:06:25,440 --> 00:06:32,220 I defined those jokes in form of lambda expressions, a lambda expression is a special way of defining 77 00:06:32,230 --> 00:06:33,360 anonymous function. 78 00:06:33,900 --> 00:06:39,690 This is the general pattern of any lambda expression on the left side of the arrow. 79 00:06:39,720 --> 00:06:44,760 We declare a parameter or parameters of the function on the right side. 80 00:06:44,820 --> 00:06:49,110 We declare the expression whose result will be returned from this function. 81 00:06:49,500 --> 00:06:53,700 We can also have lambda expressions with more than one parameter. 82 00:06:54,090 --> 00:06:57,720 In this case, we must put parameters in parentheses. 83 00:06:58,260 --> 00:07:02,070 This is much shorter than the traditional way of declaring functions. 84 00:07:02,430 --> 00:07:08,310 There is no return keyword, as it is simply assumed that the result is calculated on the right side 85 00:07:08,310 --> 00:07:09,060 is returned. 86 00:07:09,420 --> 00:07:15,390 The result type is not declared because it is inferred from what the expression evaluates to. 87 00:07:15,720 --> 00:07:22,110 For example, in this lambda expression, it is obvious that the return type of dysfunction is string. 88 00:07:22,740 --> 00:07:26,310 The types of parameters are also not formally defined. 89 00:07:26,580 --> 00:07:29,160 So how does the compiler know what they are? 90 00:07:29,550 --> 00:07:32,220 Well, it also infers them from the context. 91 00:07:32,580 --> 00:07:33,930 Let's see this code again. 92 00:07:34,770 --> 00:07:41,670 That is, the method expects a very particular function as the parameter, a function that takes a number 93 00:07:41,700 --> 00:07:42,860 and returns a ball. 94 00:07:43,230 --> 00:07:49,650 Because you use the lambda expression as the parameter of the animated, the CP. infers that the parameter 95 00:07:49,650 --> 00:07:53,940 of the lambda expression must be an integer because that is the method. 96 00:07:54,030 --> 00:07:56,670 Expect such a function as the parameter. 97 00:07:57,150 --> 00:08:03,000 This is also the reason why we can't assign lambda expressions to implicitly type two variables. 98 00:08:08,170 --> 00:08:14,080 As you can see, this doesn't work because there is no context from which the compiler can infer the 99 00:08:14,080 --> 00:08:15,490 type of the parameter. 100 00:08:16,300 --> 00:08:21,160 You may say that lambda expressions require a lot of assumptions and inferring. 101 00:08:21,400 --> 00:08:26,950 But see how little code we needed to write to declare a function checking if a number is given. 102 00:08:27,340 --> 00:08:32,380 And again, this is how this function looked like when we declared it in a traditional way. 103 00:08:33,040 --> 00:08:39,160 Lambda expressions are great when declaring short, specific functions that will most likely not be 104 00:08:39,160 --> 00:08:40,870 used in another context. 105 00:08:41,080 --> 00:08:44,830 For example, they are extremely often used when working with. 106 00:08:45,640 --> 00:08:51,880 Actually, that is a method we declared is almost identical to the animated from being. 107 00:08:52,690 --> 00:08:59,680 Let's summarize in C-sharp we can treat functions like any of our types, assign them to variables or 108 00:08:59,680 --> 00:09:02,170 post them as parameters to other functions. 109 00:09:02,560 --> 00:09:06,490 The funk and action types allow us to represent functions. 110 00:09:06,850 --> 00:09:11,140 Lambda expressions are a special way of declaring anonymous functions. 111 00:09:11,500 --> 00:09:17,560 They allow us to define functions in a concise way and our most useful when those functions will not 112 00:09:17,560 --> 00:09:19,450 be used in different contexts. 113 00:09:20,080 --> 00:09:26,140 When talking with the interviewer, you may have questions like what is the signature of a function 114 00:09:26,140 --> 00:09:28,870 that could be assigned to a variable of this type? 115 00:09:29,530 --> 00:09:34,010 It would be a function that takes two integers as parameters and returns. 116 00:09:34,030 --> 00:09:36,610 Abu What is an action? 117 00:09:37,270 --> 00:09:41,110 Action is a type used to represent voiced functions. 118 00:09:41,440 --> 00:09:46,870 It works similarly to func, but func can only represent non-votes functions. 119 00:09:47,500 --> 00:09:49,660 All right, that's it for this lecture. 120 00:09:49,990 --> 00:09:52,600 Thanks for watching, and I'll see you in the next one.