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