Hello.
The students that they were going to start, important and very interesting topic, I'm talking about
functional programming and this lesson, we are going to understand what functional programming is.
Well, understand how it is different from object oriented approach after this lesson.
And you know what the difference between method and function is?
And only after we will understand the basics of functional programming, we will talk about functional
programming in Java.
We are going to review Jaberi till function package and understand its structure will not have enough
time to learn all types from that package today.
But you will understand the structure of the package and what it exists for.
The things that we are going to learn in this class and will help us to boost our learning of functional
programming in Java.
Let's start and let's understand first what is a functional programming.
Functional programming is a programming paradigm where programs are constructed by applying and composing
functions in functional programming.
Functions are treated like the main core element of each program, and they can be assigned to variables
and can be returned from the Massett, the same as other data type.
To understand functional programming better.
Let's discuss concepts that are specific to functional programming.
The first concept is a concept of first class function.
Usually this term is applied to programming language that can treat functions as first class citizens.
That means language supports and functions as an argument to other functions, return and functions
from other functions as value and assign functions to variables.
The next concept that is related to functional programming is pure functions.
These are functions that have following properties.
The first one, its evaluation has no side effects.
That means the function doesn't modify some state variable values outside the function.
And the second property is that original value of pure function is the same for the same arguments.
That means, given the same input, will always return the same output.
This can be used for code.
Optimization, for example, is a result of pure function is not used.
It can be removed without affecting other expressions.
That also means that pure functions are completely independent from outside state and can be used in
parallel or asynchronously.
It is easy to move them around in your code, making your programs more flexible and adaptable to future
changes.
The third concept of functional programming is a recursion and functional programming.
There is no for and while loops.
But what is used instead calling the same function from another function is called recursion and can
be used for iteration.
The fourth concept is that functional programs do not have assignment statements.
That means the value of a variable is immutable and doesn't change state once it was defined.
This is called referential transparency.
The next concept in our list is lazy evaluation.
What does that mean?
Each programming language has strategies to evaluate code blocks and when we call a function, programming
language should define the strategy to produce arguments.
There are two possible main evaluation methods.
They are strict, sometimes called EGA evaluation and non strict so-called lazy evaluation.
What is the difference in strict evaluation?
Arguments are processed immediately when we invoke a function and give a list of arguments.
Each argument is processed and calculated before colon's a function, whereas in lazy evaluation expression,
evaluation is deferred.
Evaluation is delayed as long as possible until it is necessary to evaluate the expression to produce
a result.
That's only five key concepts in different literature.
You can find the different list of concepts or just different names for the same concepts that I have
just described.
I would say that the list of the current concepts is enough to understand the functional programming.
What advantages and disadvantages functional programming has been through professionals.
We have to understand advantages and disadvantages of functional programming approach the disadvantages
advantages it is worth to mention that because of using pure functions that don't change any state and
are entirely dependent on the input, they are simple to understand the simplicity of functional programming
is one of the advantages that is mentioned.
Most often.
The testing is easier than the bargain is.
Easier is it is the first advantage.
The second advantage is that because of the pure functions, implementation of concurrency is much easier.
We shouldn't worry about state being changed from the different threats of execution in parallel.
To be honest, these are the biggest advantages you can Google advantages of functional programming
if you wish, but all of them can.
Be grouped in these two advantages that are listed, somebody would also mention that programs are more
concise and more bulletproof.
But I would rather said that this is associated with a simplicity that we have just discussed and the
logical benefits of PUA functions.
Somebody would also notice that absence of classes in functional programming makes the program code
easy to read.
But in my humble opinion, these advantages depend on the type of software you develop.
While functional programming works great for simple programs and for beginner programmers, I would
like to see how people who write articles that functional programming is the best.
I would love to see how they would implement some complex enterprise solutions that requires the mind
driven design and object oriented approach.
And remember, team, we are professionals.
That's why we don't say that functional programming or object oriented approach is better than something
else.
We just say that each approach works the best for specific cases and we need to know when to apply a
different approach if needed.
Talking about these advantages of functional programming, I'd like to mention a few items here.
The first thing that you probably might think of is that in case all values are immutable and functions
doesn't change state that would cause significant performance issues during the processing of a big
amount of data because a lot of values will be created that are not used.
That would also require a specific mechanism for memory management.
The second disadvantage is partially we talked through imagine that huge online store with customer
management, personalized promotions, specific rules for discount management, affiliate program,
warehouse management, etc..
Imagine that this kind of program is written with only functions.
Would that really make code readable?
Would that make the code easier to maintain and extend?
Or onboarding of new engineer to the project will require a month or two for learning all functions
that exist in the project.
That's why among the disadvantages of functional programming sometimes mentioned that in particular
cases, programs that are even with the help of only functions are hard to read.
The third important disadvantage that is worth to mention is that while writing of pure functions might
look easy, it is hard task to combine pure functions and make them work together to achieve some specific
goal and integrate them with the rest of application.
Now, you know, advantages and disadvantages of functional programming.
Functional programming is a form of declarative programming.
After you understood what a functional programming is, it will be easy for you to understand the definition
of declarative programming.
Declarative programming is a programming style of building the structure and elements of computer programs
that express the logic of a computation without describing the order in which individual statements,
instructions or function calls of program are executed or evaluated.
Does it make sense now on the opposite of declarative programming, we have imperative programming in
computer science.
Imperative programming is a programming paradigm that uses statements that change a programs state.
To make you understand imperative programming, I would like to use object oriented development approach
as an example.
We have objects that have state and masses to modify the state.
Usually Java is considered to be an imperative programming language, and in Java we usually use term
method instead of function to describe some specific block of code that can be executed.
What is the difference between the function and method or this is the same?
A function is a piece of code that is called by name and method is a piece of code that is called by
name that is associated with an object.
And I believe you can see that these two definitions are close, but not the same.
There are two main points to consider here.
The first one is that MassArt is called on the object.
That's why Massager is considered to be closer to object oriented programming approach.
Function can exist without any object and can be called independently.
And the second point, the method is able to operate on data that describes the state of the object.
Function works with arguments that are pastorate.
Does it make sense?
Help now?
The difference between function and MassArt is clearer.
Let's talk now about functional programming and Java.
Functional programming was introduced in Java eight and I'd like to add more specifics here.
In my opinion, it will sound correctly.
It will say that functional like syntax was introduced in Java version eight.
Java still remains imperative language.
Java still uses object oriented approach, and even functional programming in Java is implemented in
object oriented manner.
That is really impressive.
Java combines is.
So declarant functions and it's pure object oriented style that deprives the Java feeling during the
code in Java introduced syntax improvements, namely now we can use lambda expressions and methods references
during the development.
We're going to learn what is this?
And a separate lesson.
And we'll practice in writing our own lambda expressions and create references.
Java also introduced new package with the language function that's called an overview of this package
to understand what was introduced there.
And here you can see official documentation of your function package.
Why do we need it in this package?
Functional interfaces are grouped.
What are functional interfaces?
In short, every interface that declares only one abstract method is a functional interface in our next
lesson.
So we are going to learn in detail what functional interfaces are.
But for this lesson, it is enough to imagine interfaces.
It always has only one abstract method.
And when I will say functional interface, you will understand what I am talking about.
The functional interfaces provide target types for lambda expressions and methods references.
Let me explain this in simple words.
This package group, all types that can be used during the declaring of lambda expressions and method
references, all types in this package follow specific naming convention and this is specified here,
will not learn this right now because in my opinion it is better to learn during the practice.
You can see that this package was introduced in Java version eight.
And here you can see a list of types that we are going to learn.
Clear explanations written right here next to type name.
I can leave the link to this documentation in attachments to the lesson and you would read it after
the lesson.
Don't try to understand everything because we are going to have lessons that will teach you how to work
with types from this package.
But you can try to understand why we need some types here.
For example, here you can even see a function type.
This type is developed to represent the function that accepts one argument and produces a result.
While learning stream API will use some of these types to declare a lambda expressions.
And now when you can see the whole list of types that allows us to create an object of these types,
you can understand why.
A few minutes ago I said that functional programming Java implemented with a feeling of object oriented
paradigm.
You can see that function is also a separate type and I can create an object of function type.
I believe this information is more than enough to understand what functional programming is and what
we are going to learn.
Let's recap what we have learned today, that iminent what functional programming is.
We discussed main concepts of functional programming.
After that, we discussed advantages and disadvantages of functional programming.
Also today, we learned what is imperative and declarative programming.
We learned what the difference between the function and the method is.
And at the end of the lesson, I told you what functional programming in Java is and hold an overview
of Jabbawockeez function package.
I believe now we have enough theoretical knowledge to practice our learning of functional programming
in Java.
That's all what I wanted to discuss with you during this lesson.
Thanks a lot for your attention.
See you in the next lesson.