1 00:00:06,020 --> 00:00:06,450 Hello. 2 00:00:06,480 --> 00:00:11,390 The students that they were going to start, important and very interesting topic, I'm talking about 3 00:00:11,390 --> 00:00:16,510 functional programming and this lesson, we are going to understand what functional programming is. 4 00:00:16,790 --> 00:00:21,590 Well, understand how it is different from object oriented approach after this lesson. 5 00:00:21,590 --> 00:00:24,530 And you know what the difference between method and function is? 6 00:00:24,800 --> 00:00:30,080 And only after we will understand the basics of functional programming, we will talk about functional 7 00:00:30,080 --> 00:00:31,330 programming in Java. 8 00:00:31,610 --> 00:00:37,580 We are going to review Jaberi till function package and understand its structure will not have enough 9 00:00:37,580 --> 00:00:40,250 time to learn all types from that package today. 10 00:00:40,400 --> 00:00:44,180 But you will understand the structure of the package and what it exists for. 11 00:00:44,660 --> 00:00:49,670 The things that we are going to learn in this class and will help us to boost our learning of functional 12 00:00:49,670 --> 00:00:50,820 programming in Java. 13 00:00:51,350 --> 00:00:55,880 Let's start and let's understand first what is a functional programming. 14 00:00:56,270 --> 00:01:02,210 Functional programming is a programming paradigm where programs are constructed by applying and composing 15 00:01:02,210 --> 00:01:04,490 functions in functional programming. 16 00:01:04,490 --> 00:01:10,520 Functions are treated like the main core element of each program, and they can be assigned to variables 17 00:01:10,520 --> 00:01:14,390 and can be returned from the Massett, the same as other data type. 18 00:01:14,840 --> 00:01:16,800 To understand functional programming better. 19 00:01:16,970 --> 00:01:20,290 Let's discuss concepts that are specific to functional programming. 20 00:01:20,860 --> 00:01:23,990 The first concept is a concept of first class function. 21 00:01:24,440 --> 00:01:30,020 Usually this term is applied to programming language that can treat functions as first class citizens. 22 00:01:30,560 --> 00:01:36,560 That means language supports and functions as an argument to other functions, return and functions 23 00:01:36,560 --> 00:01:40,730 from other functions as value and assign functions to variables. 24 00:01:41,420 --> 00:01:45,940 The next concept that is related to functional programming is pure functions. 25 00:01:46,280 --> 00:01:48,980 These are functions that have following properties. 26 00:01:49,710 --> 00:01:53,320 The first one, its evaluation has no side effects. 27 00:01:53,660 --> 00:01:59,390 That means the function doesn't modify some state variable values outside the function. 28 00:02:00,020 --> 00:02:05,540 And the second property is that original value of pure function is the same for the same arguments. 29 00:02:05,810 --> 00:02:09,710 That means, given the same input, will always return the same output. 30 00:02:10,280 --> 00:02:11,600 This can be used for code. 31 00:02:11,600 --> 00:02:15,950 Optimization, for example, is a result of pure function is not used. 32 00:02:16,010 --> 00:02:18,950 It can be removed without affecting other expressions. 33 00:02:19,490 --> 00:02:25,940 That also means that pure functions are completely independent from outside state and can be used in 34 00:02:25,940 --> 00:02:27,770 parallel or asynchronously. 35 00:02:28,370 --> 00:02:34,040 It is easy to move them around in your code, making your programs more flexible and adaptable to future 36 00:02:34,040 --> 00:02:34,560 changes. 37 00:02:35,210 --> 00:02:40,190 The third concept of functional programming is a recursion and functional programming. 38 00:02:40,200 --> 00:02:42,230 There is no for and while loops. 39 00:02:42,590 --> 00:02:48,710 But what is used instead calling the same function from another function is called recursion and can 40 00:02:48,710 --> 00:02:50,090 be used for iteration. 41 00:02:50,870 --> 00:02:55,550 The fourth concept is that functional programs do not have assignment statements. 42 00:02:56,040 --> 00:03:01,670 That means the value of a variable is immutable and doesn't change state once it was defined. 43 00:03:02,040 --> 00:03:04,520 This is called referential transparency. 44 00:03:05,180 --> 00:03:08,600 The next concept in our list is lazy evaluation. 45 00:03:08,900 --> 00:03:09,970 What does that mean? 46 00:03:10,340 --> 00:03:16,460 Each programming language has strategies to evaluate code blocks and when we call a function, programming 47 00:03:16,460 --> 00:03:19,400 language should define the strategy to produce arguments. 48 00:03:19,640 --> 00:03:22,190 There are two possible main evaluation methods. 49 00:03:22,200 --> 00:03:29,330 They are strict, sometimes called EGA evaluation and non strict so-called lazy evaluation. 50 00:03:29,750 --> 00:03:32,480 What is the difference in strict evaluation? 51 00:03:32,480 --> 00:03:38,090 Arguments are processed immediately when we invoke a function and give a list of arguments. 52 00:03:38,090 --> 00:03:45,350 Each argument is processed and calculated before colon's a function, whereas in lazy evaluation expression, 53 00:03:45,350 --> 00:03:47,030 evaluation is deferred. 54 00:03:47,360 --> 00:03:53,360 Evaluation is delayed as long as possible until it is necessary to evaluate the expression to produce 55 00:03:53,360 --> 00:03:53,900 a result. 56 00:03:54,620 --> 00:03:58,100 That's only five key concepts in different literature. 57 00:03:58,100 --> 00:04:03,410 You can find the different list of concepts or just different names for the same concepts that I have 58 00:04:03,410 --> 00:04:04,220 just described. 59 00:04:04,700 --> 00:04:09,440 I would say that the list of the current concepts is enough to understand the functional programming. 60 00:04:09,920 --> 00:04:15,260 What advantages and disadvantages functional programming has been through professionals. 61 00:04:15,260 --> 00:04:21,410 We have to understand advantages and disadvantages of functional programming approach the disadvantages 62 00:04:21,410 --> 00:04:26,900 advantages it is worth to mention that because of using pure functions that don't change any state and 63 00:04:26,900 --> 00:04:33,800 are entirely dependent on the input, they are simple to understand the simplicity of functional programming 64 00:04:33,800 --> 00:04:35,960 is one of the advantages that is mentioned. 65 00:04:35,960 --> 00:04:36,860 Most often. 66 00:04:37,370 --> 00:04:40,010 The testing is easier than the bargain is. 67 00:04:40,010 --> 00:04:42,350 Easier is it is the first advantage. 68 00:04:42,800 --> 00:04:49,100 The second advantage is that because of the pure functions, implementation of concurrency is much easier. 69 00:04:49,460 --> 00:04:54,700 We shouldn't worry about state being changed from the different threats of execution in parallel. 70 00:04:55,370 --> 00:05:00,770 To be honest, these are the biggest advantages you can Google advantages of functional programming 71 00:05:00,770 --> 00:05:02,720 if you wish, but all of them can. 72 00:05:02,780 --> 00:05:08,900 Be grouped in these two advantages that are listed, somebody would also mention that programs are more 73 00:05:08,900 --> 00:05:11,000 concise and more bulletproof. 74 00:05:11,270 --> 00:05:16,670 But I would rather said that this is associated with a simplicity that we have just discussed and the 75 00:05:16,670 --> 00:05:18,910 logical benefits of PUA functions. 76 00:05:19,640 --> 00:05:24,740 Somebody would also notice that absence of classes in functional programming makes the program code 77 00:05:24,740 --> 00:05:25,750 easy to read. 78 00:05:26,180 --> 00:05:31,130 But in my humble opinion, these advantages depend on the type of software you develop. 79 00:05:31,550 --> 00:05:36,920 While functional programming works great for simple programs and for beginner programmers, I would 80 00:05:36,920 --> 00:05:41,180 like to see how people who write articles that functional programming is the best. 81 00:05:41,420 --> 00:05:46,970 I would love to see how they would implement some complex enterprise solutions that requires the mind 82 00:05:46,970 --> 00:05:49,610 driven design and object oriented approach. 83 00:05:50,090 --> 00:05:52,590 And remember, team, we are professionals. 84 00:05:52,610 --> 00:05:58,040 That's why we don't say that functional programming or object oriented approach is better than something 85 00:05:58,040 --> 00:05:58,370 else. 86 00:05:58,820 --> 00:06:04,580 We just say that each approach works the best for specific cases and we need to know when to apply a 87 00:06:04,580 --> 00:06:06,170 different approach if needed. 88 00:06:06,710 --> 00:06:11,480 Talking about these advantages of functional programming, I'd like to mention a few items here. 89 00:06:12,020 --> 00:06:17,620 The first thing that you probably might think of is that in case all values are immutable and functions 90 00:06:17,630 --> 00:06:22,940 doesn't change state that would cause significant performance issues during the processing of a big 91 00:06:22,940 --> 00:06:27,260 amount of data because a lot of values will be created that are not used. 92 00:06:27,680 --> 00:06:31,100 That would also require a specific mechanism for memory management. 93 00:06:31,750 --> 00:06:37,370 The second disadvantage is partially we talked through imagine that huge online store with customer 94 00:06:37,370 --> 00:06:43,040 management, personalized promotions, specific rules for discount management, affiliate program, 95 00:06:43,220 --> 00:06:45,560 warehouse management, etc.. 96 00:06:45,830 --> 00:06:49,420 Imagine that this kind of program is written with only functions. 97 00:06:49,880 --> 00:06:51,770 Would that really make code readable? 98 00:06:52,250 --> 00:06:55,190 Would that make the code easier to maintain and extend? 99 00:06:55,610 --> 00:07:01,550 Or onboarding of new engineer to the project will require a month or two for learning all functions 100 00:07:01,550 --> 00:07:02,850 that exist in the project. 101 00:07:03,390 --> 00:07:08,900 That's why among the disadvantages of functional programming sometimes mentioned that in particular 102 00:07:08,900 --> 00:07:13,790 cases, programs that are even with the help of only functions are hard to read. 103 00:07:14,510 --> 00:07:19,760 The third important disadvantage that is worth to mention is that while writing of pure functions might 104 00:07:19,760 --> 00:07:26,510 look easy, it is hard task to combine pure functions and make them work together to achieve some specific 105 00:07:26,510 --> 00:07:29,510 goal and integrate them with the rest of application. 106 00:07:30,140 --> 00:07:33,590 Now, you know, advantages and disadvantages of functional programming. 107 00:07:34,370 --> 00:07:37,460 Functional programming is a form of declarative programming. 108 00:07:37,850 --> 00:07:43,580 After you understood what a functional programming is, it will be easy for you to understand the definition 109 00:07:43,580 --> 00:07:44,990 of declarative programming. 110 00:07:45,410 --> 00:07:51,020 Declarative programming is a programming style of building the structure and elements of computer programs 111 00:07:51,020 --> 00:07:57,230 that express the logic of a computation without describing the order in which individual statements, 112 00:07:57,230 --> 00:08:01,760 instructions or function calls of program are executed or evaluated. 113 00:08:02,090 --> 00:08:08,660 Does it make sense now on the opposite of declarative programming, we have imperative programming in 114 00:08:08,660 --> 00:08:09,530 computer science. 115 00:08:09,530 --> 00:08:15,200 Imperative programming is a programming paradigm that uses statements that change a programs state. 116 00:08:15,710 --> 00:08:21,080 To make you understand imperative programming, I would like to use object oriented development approach 117 00:08:21,080 --> 00:08:21,890 as an example. 118 00:08:22,340 --> 00:08:27,080 We have objects that have state and masses to modify the state. 119 00:08:27,560 --> 00:08:33,950 Usually Java is considered to be an imperative programming language, and in Java we usually use term 120 00:08:33,950 --> 00:08:39,560 method instead of function to describe some specific block of code that can be executed. 121 00:08:39,920 --> 00:08:43,970 What is the difference between the function and method or this is the same? 122 00:08:44,450 --> 00:08:50,900 A function is a piece of code that is called by name and method is a piece of code that is called by 123 00:08:50,900 --> 00:08:53,240 name that is associated with an object. 124 00:08:53,630 --> 00:08:58,580 And I believe you can see that these two definitions are close, but not the same. 125 00:08:59,060 --> 00:09:01,340 There are two main points to consider here. 126 00:09:01,880 --> 00:09:05,090 The first one is that MassArt is called on the object. 127 00:09:05,510 --> 00:09:10,760 That's why Massager is considered to be closer to object oriented programming approach. 128 00:09:11,150 --> 00:09:15,890 Function can exist without any object and can be called independently. 129 00:09:16,430 --> 00:09:22,550 And the second point, the method is able to operate on data that describes the state of the object. 130 00:09:23,060 --> 00:09:26,150 Function works with arguments that are pastorate. 131 00:09:26,630 --> 00:09:27,590 Does it make sense? 132 00:09:28,100 --> 00:09:28,790 Help now? 133 00:09:28,790 --> 00:09:31,520 The difference between function and MassArt is clearer. 134 00:09:31,970 --> 00:09:34,760 Let's talk now about functional programming and Java. 135 00:09:35,330 --> 00:09:40,550 Functional programming was introduced in Java eight and I'd like to add more specifics here. 136 00:09:40,820 --> 00:09:43,220 In my opinion, it will sound correctly. 137 00:09:43,250 --> 00:09:47,930 It will say that functional like syntax was introduced in Java version eight. 138 00:09:48,560 --> 00:09:50,900 Java still remains imperative language. 139 00:09:51,170 --> 00:09:57,410 Java still uses object oriented approach, and even functional programming in Java is implemented in 140 00:09:57,410 --> 00:09:58,790 object oriented manner. 141 00:09:59,300 --> 00:10:00,770 That is really impressive. 142 00:10:01,190 --> 00:10:02,570 Java combines is. 143 00:10:02,680 --> 00:10:09,790 So declarant functions and it's pure object oriented style that deprives the Java feeling during the 144 00:10:09,790 --> 00:10:17,890 code in Java introduced syntax improvements, namely now we can use lambda expressions and methods references 145 00:10:17,890 --> 00:10:18,970 during the development. 146 00:10:19,330 --> 00:10:21,280 We're going to learn what is this? 147 00:10:21,280 --> 00:10:22,390 And a separate lesson. 148 00:10:22,390 --> 00:10:27,160 And we'll practice in writing our own lambda expressions and create references. 149 00:10:27,760 --> 00:10:33,670 Java also introduced new package with the language function that's called an overview of this package 150 00:10:33,670 --> 00:10:35,760 to understand what was introduced there. 151 00:10:36,370 --> 00:10:40,040 And here you can see official documentation of your function package. 152 00:10:40,330 --> 00:10:42,790 Why do we need it in this package? 153 00:10:42,790 --> 00:10:44,620 Functional interfaces are grouped. 154 00:10:45,100 --> 00:10:47,080 What are functional interfaces? 155 00:10:47,350 --> 00:10:54,550 In short, every interface that declares only one abstract method is a functional interface in our next 156 00:10:54,550 --> 00:10:54,910 lesson. 157 00:10:54,920 --> 00:10:58,440 So we are going to learn in detail what functional interfaces are. 158 00:10:58,750 --> 00:11:01,920 But for this lesson, it is enough to imagine interfaces. 159 00:11:01,930 --> 00:11:04,600 It always has only one abstract method. 160 00:11:04,930 --> 00:11:08,980 And when I will say functional interface, you will understand what I am talking about. 161 00:11:09,490 --> 00:11:14,470 The functional interfaces provide target types for lambda expressions and methods references. 162 00:11:14,800 --> 00:11:16,850 Let me explain this in simple words. 163 00:11:17,350 --> 00:11:23,980 This package group, all types that can be used during the declaring of lambda expressions and method 164 00:11:23,980 --> 00:11:30,430 references, all types in this package follow specific naming convention and this is specified here, 165 00:11:31,000 --> 00:11:35,650 will not learn this right now because in my opinion it is better to learn during the practice. 166 00:11:36,250 --> 00:11:39,670 You can see that this package was introduced in Java version eight. 167 00:11:40,300 --> 00:11:43,480 And here you can see a list of types that we are going to learn. 168 00:11:44,290 --> 00:11:47,680 Clear explanations written right here next to type name. 169 00:11:48,160 --> 00:11:52,810 I can leave the link to this documentation in attachments to the lesson and you would read it after 170 00:11:52,810 --> 00:11:53,320 the lesson. 171 00:11:53,710 --> 00:11:59,200 Don't try to understand everything because we are going to have lessons that will teach you how to work 172 00:11:59,200 --> 00:12:00,700 with types from this package. 173 00:12:01,210 --> 00:12:04,390 But you can try to understand why we need some types here. 174 00:12:04,780 --> 00:12:07,900 For example, here you can even see a function type. 175 00:12:08,350 --> 00:12:13,840 This type is developed to represent the function that accepts one argument and produces a result. 176 00:12:14,290 --> 00:12:19,300 While learning stream API will use some of these types to declare a lambda expressions. 177 00:12:19,870 --> 00:12:25,450 And now when you can see the whole list of types that allows us to create an object of these types, 178 00:12:25,570 --> 00:12:27,220 you can understand why. 179 00:12:27,220 --> 00:12:32,950 A few minutes ago I said that functional programming Java implemented with a feeling of object oriented 180 00:12:32,950 --> 00:12:33,580 paradigm. 181 00:12:34,060 --> 00:12:39,340 You can see that function is also a separate type and I can create an object of function type. 182 00:12:40,060 --> 00:12:45,250 I believe this information is more than enough to understand what functional programming is and what 183 00:12:45,250 --> 00:12:46,140 we are going to learn. 184 00:12:46,600 --> 00:12:51,580 Let's recap what we have learned today, that iminent what functional programming is. 185 00:12:51,760 --> 00:12:54,480 We discussed main concepts of functional programming. 186 00:12:54,670 --> 00:12:58,800 After that, we discussed advantages and disadvantages of functional programming. 187 00:12:59,290 --> 00:13:03,250 Also today, we learned what is imperative and declarative programming. 188 00:13:03,910 --> 00:13:07,570 We learned what the difference between the function and the method is. 189 00:13:08,020 --> 00:13:13,810 And at the end of the lesson, I told you what functional programming in Java is and hold an overview 190 00:13:13,810 --> 00:13:15,760 of Jabbawockeez function package. 191 00:13:16,150 --> 00:13:21,160 I believe now we have enough theoretical knowledge to practice our learning of functional programming 192 00:13:21,160 --> 00:13:21,730 in Java. 193 00:13:22,270 --> 00:13:25,060 That's all what I wanted to discuss with you during this lesson. 194 00:13:25,480 --> 00:13:26,860 Thanks a lot for your attention. 195 00:13:27,100 --> 00:13:28,750 See you in the next lesson.