Welcome back.
Let's now create a new project for functional programming.
I'll say New project: Java project.
I'll call this functional-programming. When you click Finish, a new project would be created
and in the source, I can create our first class.
So, let's create a very simple class and I would put it in the Package: com.in28minutes
functionalprogramming
and I would call this class as FunctionalProgramming
Runner.
There are a wide range of things that we would be discussing.
So, I don't really have a better name than that.
So, let's add in a main method and click Finish. In the previous video, we said functional programming
is all about giving first class status to functions.
It's kind of a cheesy definition, but let's see what it all means
in this specific step. We'll try and create a very simple example, not using functional programming, that's
basically using procedural or object oriented programming, and we will see how it can be implemented
in functional programming.
Let's create a very simple list, right? So, List, I'll say list = we did it many times during
the collections thing, right?
So, List.of("Apple",
"Banana",
"Cat", and "Dog");
Typically, if I would want to, oops, there are imports, let's do the imports as well.
Typically, when we would want to loop this, we would probably use a normal loop, right? List oops, for(String
string:list) { if I want to print out,
I would say, sysout
oops, (string); right?
So, if I want to loop through and print all the values,
this is what I would typically do. Cool, right? So, now, Apple, Banana, Cat, Dog
That's cool.
So, I can right click: Refractor and extract method and I'll call this printBasic, right?
This is how we would print any list, right?
However, with functional programming, it brings in another approach.
I'll write in some very simple piece of code, you might not be able to understand that completely
in this specific step, but over the period of next few steps, we will try and understand, and break down the
code that I would write.
Now, I'll create a very simple static method, I'll copy this basic.
So, what we want to do is print with functional programming,
I'll call this printWithFP and in functional programming, the way you can do the same thing is by saying,
list.stream.
So, I'm taking all these elements in the list and I'm converting into a stream of values.
So, this becomes a stream of values.
So, first Apple would come, then Banana,
then Cat, and Dog. So, list.stream().forEach(action);
What do I want to do with forEach?
You can see what action you want to do,
action is nothing but method, right?
What action do I want to do? What I want to do is, I would want to take each of the elements. So, I would
want to take each of the elements and I would want to call System.out.println
(elements);
Now, focus on this syntax. So, after forEach after the parenthesis, I am pressing enter and I am pressing an enter
after here as well, so that, you can actually focus on the syntax. So, what I'm saying here is,
list.stream().forEach open parentheses, within the parentheses we are saying, element->,
this is a specific syntax,
this is called a lambda expression.
We'll discuss that a little later. What we are saying is, for each element in this list, do a
System.out.println().
This is nothing but logic, right?
This is method code. So, I am saying for each element in the stream list, do a System.out.println()
of element.
Now, what I'll do here is instead of printBasics,
I'll say printWithFP.
Now, let's see what would happen.
aha, Apple, Banana, Cat, and Dog,
right?
Instead of looping and telling what to do with each string,
what we are doing is we are converting this into a stream, and we are calling forEach in it and we are
passing what to do with each element.
Interesting approach, isn't it?
Another interesting thing in here is the fact that what are we sending to forEach?
Is this data?
Nope.
This is not data.
This is action.
We are sending the code, we are sending for each element,
execute this. We are sending a function code to forEach.
So, I said in the last video, function as first class citizen
and this is the first step to it.
We are actually sending a function to execute on each of the element in the stream, and we saw it being
executed and printed in.
I can even change the logic in here. So, I can say for each element just say, ("element -"+ element). What
would happen?
Aha, element Apple, Banana, Cat, and Dog. So, let's put a space here
to make it even more beautiful,
cool, right?
So, not a lot that we have done in this specific step, but we have introduced you to a important syntax
of something called a lambda expression.
Don't worry about it.
The important thing is we are able to pass in a function as a parameter to another function and loop
around it.
That's the first important concept about functional programming that we have learnt.
This is just the stepping stone.
There are a lot of things you'd need to learn about functional programming and we'll continue the journey
in the next step.
Until then, bye-bye.