thank you
[Music]
[Applause]
[Music]
what's going on guys welcome to amigos
code my name is nelson and in this
course i'm going to teach you everything
you need to know about functional
programming with java
so when i first saw functional
programming i was like you know what is
this i saw functions by function
predicates being used all over the place
and to be honest
i only realized the power of it when i
learned about the clarity of programming
so you see most of us write imperative
programming which means that you define
every single implementation detail of
your code whereas declarative you simply
tell it what you want instead of
defining you know the details for
example you might have a variable or
actually you might have a loop
right so you define you know the initial
value how to increment it and how to
stop it right so with declarative
programming you have none of that you
simply tell it what you want and that's
what i'm going to teach you in this
course
so in this course i'm going to teach you
exactly you know what the differences
what the differences are between
imperative and declarative programming
and why you want to move away from
imperative programming then i'm going to
teach you about the functional
interfaces that java has to provide so
functions buy functions predicates
consumers suppliers
and all that good stuff right so once
you learn that you can then start to use
streams and optionals to write code in a
declarative way and then towards the end
i'm going to teach you one of my
favorite design patterns when it comes
to functional programming and that is
the combinator design pattern which is
really really awesome it's really good
for validation where you simply define
all of your validations in an interface
and then you define exactly step by step
what you want your validation to be it's
really really awesome and finally i'm
also going to teach you exactly about
lambdas callbacks and
the properties of functional programming
so that you fully grasp exactly the
whole concept
so if you haven't subscribed to my
youtube channel go ahead and subscribe
because that way i can you know provide
more awesome content like this without
further ado let's go ahead and learn
everything you need to know about
functional programming with java
all right so before you start this
course i want to make sure that you
really
learn from this course because i want to
add value so what i want from you is to
go ahead and practice as i teach because
that's the best way for you to learn any
new technology
also go ahead and join the private
facebook group so if you have any
questions you can go ahead and engage
with other students and the group is
full of cool people just like you
so this is all i wanted to say for now
and enough of me talking let's go ahead
and learn about functional programming
all right let's go ahead and bootstrap
our application where i'm going to show
you all the examples throughout this
course so i've got intellij right here
and if you've never used intellij then i
highly suggest you to enroll to my free
course where i teach you pretty much how
to use
this awesome ide and to be honest as a
software engineer this is probably the
ide of your choice when it comes to
java back-end development
so let me go back to intellij
and
the first thing i want to do is to
create project
and right here i'm going to pick maven
now the project sdk it doesn't really
matter whether you use java 11 12 or any
other version as long as is above
1.7
so i'm going to stick with 11 right here
and as i said the version does not
matter
next let me go ahead and click next
and then right here for the group id i'm
going to say com dot and then amigos
and then code so right here this could
be your own organization
and right here let me simply name this
as java and then functional
and then i'm going to press
next and then i'm going to save this
under code and then java functional
and then simply press finish
there we go let me actually collapse
this maven tab right here and also let
me collapse this
and right here go ahead and simply
enable auto import to import
dependencies
and this is the actual palm file right
so if i open the functional oh actually
java functional folder inside you see
that we have only the pump file
and this contains the group id artifact
id and then the actual version so let me
just
remove that line
and also remove this pop-up
and finally i'm going to put this full
screen
and this
is all and actually let me just close
this because we're not going to need
this palm file and this is pretty much
everything you need in order to
bootstrap the application to follow
along so obviously i'm going to give you
the source code at the end of this
course but i highly suggest you to
practice as i teach because that's the
best way
of you learning any new topic this is
all for now
if you have any questions about intellij
as i said go ahead and roll to my course
right here where i teach you everything
you should be aware of this awesome ide
let me go ahead and give you an example
of imperative programming
so imperative programming involves you
as a developer defining every single
detail about your implementation
so right here inside of the source
folder
inside of main and then java
let's go ahead and create a new package
so you can right click on this folder or
i'm going to use keyboard shortcuts and
also you can see
down here so right here you can see all
the keyboard shortcuts that i'm going to
use throughout this course
so right here i'm going to use
the error so i'm going to select package
and then right here i'm going to name
this as imperative and then okay
now inside go ahead and create class
and this will be called as main
now let me collapse this
and inside i'm going to type p
s
and then vm for public static main
there we go
and now we have a main method that we
can run
so let's go ahead and define a list
and the type of this list will be of
type and then person
so right here let me go ahead and simply
name this as people
equals
to and then list
and then dot and then off so right here
what we're going to do is simply create
few people or actually
a few people of time person so right
here let's go ahead and simply create a
class so
uh let's go ahead and say static and
then class
and then person
and this person will have a name so
private
and then final
string and a name
and we also will define a gender
so
enum
and this will be gender
and the gender will be male or female
and then right here what we're gonna do
or actually underneath is simply say
private
final and then gender
and that will be the gender
and let me add this to constructor
and there we go
so now we have
this class right here called person
and let me also define a two string
method right here so two and then string
and there we go
so now right here we can simply say list
of
and then new
and then person
and right here let's give it a name so
john
and john is a male
and let me have a static import for this
and let's go ahead and duplicate
this so i'm going to press command d a
couple of times
and there we go so let's have maria
and she's a female
let's have
aisha
she's also a female
just like that
let's have
alex
it's a mel and let's finally have alice
just like that and alice she's of course
a female
so
now that we have this
list right here
let's go ahead and find out how many
females that we have
so
the way that we will do it with
the imperative approach is let me add a
comment right here so in
an imperative
approach is simply by doing the
following so first we need an empty list
and then of type and then person
and then let me simply say females right
here equals to a list
of
and then or actually not even a list of
let's go ahead and use a new
and then array list so this is an empty
list
now what we need to do is to loop
through people
so let's go ahead and simply say four
and then person
inside of people
and right here we're going to say
if and then person
or actually let me go anything to say
female
and then dot
equals to person
dot and then gender just like that
so if this is the case i'm going to say
females dot and then add
and then person just like that
and that would semicolon and then right
here at the very end what i'm going to
do is simply print each people so again
let me simply copy this
just like that
and then right here
this instead of
people
this will be
females
and this will also be
female
and then
south
just like that so system.out.printline
oops
just like that and then
female
now if i go ahead and pretty much run
this class right here so let's go ahead
and run this class
can
and there we go
so right here you can see that
we have only females in our output
and basically this is pretty much what
imperative programming is so basically
we are defining every single
detail so right here you can see that
first we have an empty list
and then we loop through that
and then we have an if statement
and finally we have a final for loop
looping through
females and you can see that there is a
lot of logic not you know you know a
substantial amount of logic but you can
see that for example this if statement
is some logic and also you can see that
this is a lot of code for something very
simple and this is what i wanted to show
you about the imperative approach next
let's go ahead and implement this
using the declarative approach and you
will see that it's much neater
and we pretty much just tell it what we
want to do instead of defining every
single implementation detail right here
so to use declarative approach with java
ia functional style programming we
simply have to do the following let me
let me collapse this
so right here so what we're going to do
here is
instead of us creating a list
and then having two for loops what i'm
going to do is the following
first let me go ahead and pretty much
have a comment and right here this will
be declare
relative and then approach
now
the way that we use declarative approach
is by using streams with
collections
so
don't worry about um
so much about what a stream uh is
because i'm gonna show you exactly later
on
but for now just follow along so go
ahead and simply say people so people is
the original list right here so this
list of people
and then
dot and then stream
right here so now we have a stream and a
stream allows us to go into an abstract
mode where we simply tell it what we
want
so right here let's go ahead and simply
say dot and then filter
and what we want to filter
is
basically
this so right here we have a person so
this takes a person and then i'm going
to say the exact same thing right here
so i'm going to take this
if statement
oops
let me press command w a couple of times
command c
and then put it here there we go
so you see that i'm filtering and then
this takes a person
and i'm using this
same if statement so basically
female.equals and then person.gender
right
and then finally i'm going to say dot
and then collect
and then collectors
and then to list
end up with semicolon
and
right here what i'm going to do is
put this on a new line
and the same with collect just like that
and right here
we can simply now go ahead and simply
say dot and then for each
and then we can use system
dot and then out
and then column column and then print
line
so now let me actually add um
right here so let me simply add this as
a south so i'm going to print this
just like that
there we go
and let me do the same here so i'm going
to copy that
and then paste that take this comment
put it here so you can see exactly in
the console what i'm doing
so now
you can see that
this
is much concise and right here you can
see that i'm not creating a list and
then adding to that list
and then
doing the if statement so on and so
forth so you can see that
is very straightforward and i'm just
telling what i want so from the people
array list or a list basically this is a
list
i'm going to stream so i'm going to go
into this abstract mode
then i'm going to filter all the females
and then i'm going to collect to a list
and finally i'm going to
print everything so technically i don't
even need this collector list because
i'm simply printing out things
so let me simply run this for now so you
can see exactly what's happening
there we go so basically you can see
that we have the exact same
output right so if i put this whole
screen you can see that we have the
exact same output but the declarative
approach is much neater
so obviously if you were to have this
into a list right here so this is where
collect comes into play
and then i can remove
this line
and this with semicolon
and then extract this to a variable
and then say females
and then two right
and you can see that now i've got a list
right here and i could again simply say
females
to
dot and then for rich
system dot out column column print
line
so if i run again
you can see that this is the exact same
output
so there we go now you should have a
full understanding of the differences
between declarative and imperative
approach
so
obviously i've just shown you uh you
know the difference between both but
there are a few things that we need to
understand in order to
write code
like this
right so i'm going to teach you about
functions by functions predicates by
predicates consumers
and you know all the good stuff that
java has to offer when it comes to
functional programming if you have any
questions go ahead and drop me a message
otherwise let's move on
all right let's go ahead in this section
learn about the java.util.function
package
so basically this package contains
functional interfaces that provides
target times for lambda expressions and
method references
so each functional interface has a
single abstract method
basically you can have one interface
with
multiple
methods but you must have only one
single abstract method to be considered
as a functional interface
so basically you can see some examples
right here so the assignment the
assignment context so this is how you
for example
define a predicate
and this is how for example you use it
within the actual
uh you know stream so right here you've
seen that we can get stream from a list
right so streaming and filter
and
there's another example where you can do
some casting
so
this package right here contains a bunch
of
functional interfaces right here you can
see by consumer by function
binary operator by by predicate boolean
supplier consumer
and the list goes on
so they are pretty much
identical apart from the type so the
ones that you will most likely be using
are functions by functions predicates by
predicates
and consumers and also there is a by
consumer but i'm like but i'm going to
explain exactly how everything fits
together
because once you learn
those main ones then the rest will be
very simple
so right here i want to show you that so
we have a predicate so where is the
predicate
so let me find predicate
so right here so a predicate let me
quickly show you a predicate
and
show you that we actually have used
you know a predicate before so a
predicate represents
a predicate
basically this particular represents a
predicate
a boolean value function of one argument
so let me go back to intellij
and right here remember where we had
this declarative approach
so right here so this filter
so basically if i pretty much extract
this into a variable
you can see that this is a predicate
and look at this
this predicate
looks like this
so the predicate accepts a person
and then returns true or false
based
of that person i.e whether the person
is of type
female
right but what we've done right here we
simply inlined
like that that's why you don't see the
predicate keyword but indeed
this is a predicate and a predicate
looks like this
so
don't worry about
this if it doesn't make sense but i'm
going to try my best to explain you
exactly
how you go from
uh you know normal java functions to the
functional interface world
join me in the next one
all right so i've gone ahead and created
a new package right here called
functional interface
and inside of this package i'm going to
go ahead and create a class
and first i want to name this class as
underscore and then function
and i'm going to explain why i'm saying
underscore right here but for now go
ahead and simply say underscore and fun
underscore function or you can name this
anything other than function without
underscore right
so right here what i'm going to do is
simply say public static void main to
have a main method
and
right here what i'm going to do is
simply do the following
so let's go ahead and create a function
so static
and then function so this function will
take a number
right so take a number and then
increment that number by one
so let's go ahead and simply say
int
and then
increment
and then this will take an integer
number
and then it will return
number
and then plus one
so now if i go ahead and simply say
increment
and then zero
and then end up with semicolon
and
if i pretty much extract this to a
variable and for now let's go ahead and
simply sound this
increment
and let me put this full screen let me
run this now
oops i'm running the wrong one so i need
to run this function
there we go so you can see that the
result is one very simple so if i put
one right here
this will be
two
so what i want to show you is how we go
from
this so
this is what you are used to
to
this right here so let's go ahead and
learn about the function
um
interface so where is the function
so
this one right here
so
this
says that represents a function that
take that accepts one argument and then
produces a result
so this is pretty much what we have here
right so this function that we have so
this is a normal function in java so
this is what you are used to
so this
takes
an input
and then returns
one single output right here and this
is pretty much the equivalent right here
so represents a function that accepts
one argument and produces a result so
the way that we convert this into a
function right here is the following
so let's go ahead and simply
say
function
and if i actually delete this so i want
to show you something so function
you can see that this function takes a t
right so t and then r so basically these
are the data types of the input and the
output so let's go ahead right here and
simply say that this
will be of type integer so the input is
integer
and the output is also an integer
right here
so now let me go ahead and simply say
increment
and then function
or actually increment by
and then one
function right here
and then this
will be
equal to
right now this is where things become a
bit tricky so here
equals two and then this will be the
actual
number
and then right here we pass a lambda
and then i'm going to say number
and then plus plus
there we go
and i would semi column
so
you can see that
this
and this are the exact same thing
so basically
this integer right here corresponds to
the actual input so
this one right here right this data type
this integer corresponds to the return
type
and then this number corresponds to the
parameter to this function right here
and then i'm saying
number plus plus well actually let me
simply make them the same so number plus
one
and let me also rename this function to
increment by and then one
so
you can see that
this is pretty much the same but now we
are talking in
functional
style world
so now the way that we use this is as
follows
so go ahead and simply say increment by
one and the reason why we're not seeing
this this function is because we can
actually put it inside of this method
so inside of this main method we can
paste that function there
or
if you want this can also be static
like that right
so let me actually um
put this on a new line so you see
exactly
how it is
so right here i can say increment by one
function
and what i'm going to do is simply say
dot and then apply
so the apply pretty much takes the
actual argument so let me go ahead and
simply say 1
and then right here
i'm going to say
increment and then 2
and i can change this data type to
integer
and then simply say south
and then increment 2
then
let me simply run this again
you can see that we have the exact same
output
so this is how you use
a function
so basically
it's the exact same thing as this
function right here but in a functional
style way
so if you have any questions about this
go ahead and drop me a message
but i'm gonna show you exactly how
all of this fits together right because
now you might be asking you know why
should i use
uh you know this function right here
instead of a normal regular function
right
and don't worry by the end of this
course
everything will be clarified
so there are a few methods that i want
to teach you so right here
if i pretty much say increment function
uh or actually increment by one function
dot you can see that we have
um apply and then this method right here
and then so i want to teach you
what this method is because you might
use it later on
so let me simply
remove that
and this is all for now join me in the
next video see ya
all right let's say that you want to
combine multiple functions together to
achieve
a desired state or a desired output
so let's say that you have this
function right here so increment by one
function
but let's also create another function
oh actually let me not copy base but
simply say
static and then function
and this function will take an integer
and
this function will also return an
integer
and then i'm going to say multiply
by
and then 10
right here
so this will take the number
oops number
and then this will be lambda just like
that
and then number
times and then 10 just like that
so
we have this function right here
and what we want to do is after
incrementing so after incrementing
uh by one right here we want to apply
that or the function immediately
so you could do something like this so
you could say
int
increment
or actually multiply
and then equals two
and then multiply by ten
uh let me actually rename this to a
function
just like that
and then right here you could say
equals to
multiply by 10 function dot
apply
and then increment
nn2 right
so
uh this is
not quite right
because
this is a name so right there
and then remove that
and this should be multiply
by 10
let me grab that name there
paste it there and there we go so now if
i do a south
and then multiply by 10 function
well actually
i'm getting all confused right here so
this should be multiply
there we go so this is the actual output
so now if i run this
you can see that
we take 2
multiplied by 10 is 20.
so obviously
this is you know quite normal for us to
do but we can do something better so
what we can do is
simply combine the two functions
together
so right here what i'm going to do is
simply say increment by one function
dot
and then and then
multiply by 10 function
and right here
what i'm going to do is simply extract
this to a variable so this will give us
yet another function
and let me simply say
add by
1
and
then multiply
by
10 there we go
so if i put this on a new line and you
can see exactly what it's doing so now
let's go ahead and simply say add
by 1 and multiply by 10
and then simply say apply
and you can see that you can keep on you
know chaining these functions as you go
along but for now let's simply chain two
functions so now i'm going to say apply
and
the number that i want is
one right
so
this should give us the exact same
result so 1 plus 1 2
2 plus
10 you guessed it right that would be
20.
so
obviously i need to
put this around
system.out.printline
just like that
run this again
you can see that we have 20 right here
so if i change this
to let's change this number right here
so instead of one let's go ahead and
change it to four
four plus one five
times ten
fifty right here
and this is pretty much how you chain
functions together
so if you have any questions go ahead
and drop me a message
next let me go ahead and teach you about
by functions
all right so what a by function is
is
the same as
a function
but instead of taking one
input it takes two inputs and produces
one output
and by function for
two things right
so
right here
we have this function right here so this
one add by one and multiply by 10.
so let's say that the first number that
we give it
is the one that it should increment and
then the second one is the one that it
should multiply
by
so right here let me go ahead and define
this as a normal function so you can see
exactly
um
the equivalent so increment and we'll
actually increment by one
and
multiply
just like that so now right here
we have a second argument so
int num to multiply
by
just like that
and now i'm going to wrap everything
within
parenthesis
and then say times and then num to
multiply by
now the way that we define the exact
same thing
but using
a by function is as follow
so let's go ahead and simply say buy
and then function
so the first argument is the
first input which is the actual number
that we want to
add one by so let me go ahead and simply
say this is an integer
the second one is also an integer
the third one is also an integer
so bear in mind that you can have
different data types depending of your
circumstances
so right here let me simply say
that this will have the same name but by
function
so usually you wouldn't include the pi
function
for the name but i'm just doing it so
that you understand exactly
and reason about these things so right
here this now will take
two things so because it takes two
arguments we need to wrap this within
parenthesis
so this will be
number
to
increment
by
one
and then number
two
mul
t
ply and then buy
and this will be now a lambda and what
we do is simply say number two
increment by one plus and then one
right so this is the exact same thing
that we did here
and then this will be within parenthesis
and then finally
times and then number to multiply by
and that would semicolon
and let me just put this on a new line
yet again
so you see exactly everything so i've
got bigger font because i want you to
see exactly properly
but you know you shouldn't indent things
like this but basically
this right here is the exact same thing
as this
so now let's go ahead and use it so
right here remember
we had this function so add by 1 and
then multiply by 10. so let's go ahead
and say this now is a by
function
and this was the function example
so let me actually add a comment so
takes one
argument
and
produces
one result
so now
the by function
is almost the same but it takes two
arguments right here
so let's go ahead and use our function
so right here so increment by one
and then multiply
let's go ahead and give it four
and then let's multiply four
by one hundred
so basically this will be
four plus one five
five times 100
500
so let's go ahead and simply
uh i'm not going to even
put this into a variable i'm simply
going to say south
and then just like that so let me
put like that
and there we go
so now if i run this
you can see that we have 500 right here
now let me go ahead and use
the by function so if i escape out of
this
and let's actually have the same system
right here so
system.line
and right here
let me simply use the actual
function
i think we have to have it as static so
this guy right here
so this has to be static
there we go now it should appear
by function
and this should be dot and then apply
and apply takes 4 and 100
now if i run this
you can see that we have the exact same
result
so
this one right here is using
normal java function
and this one right here is using the buy
function
let me go ahead and show you the actual
docks you'll see that we have lots of
buy so let me say let me search for buy
so you could see that we have
you know a double
by function to int by function to long
by function
and all of these by functions are the
exact same thing as the function but
they take two arguments as you can see
here right
so the same with predicate so it takes
two arguments and returns true or false
so this is the difference between a
function and a by function if you have
any questions go ahead and join me
message
otherwise let's move on
all right next let's go ahead and learn
about consumer
so a consumer so let me search for
consumer
right here so consumer
so a consumer represents an operation
that accepts a single input argument and
returns no result
so let me show you exactly first the
non-functional way and then the
functional way so let's say that right
here again let's have public static
void main
and right here let's go ahead and define
a static class
and then customer
and then let's go ahead and simply say
private
final and let me put this full screen so
private final
and then string
and then
customer
name
private
final
string
customer
phone
number just like that
so next let me add that to constructor
and
now what i'm going to do is have a
function right here
so let me go and simply say static
and then right here let's go ahead and
say greet
and then customer
so this will take a customer
and right here let's go ahead and simply
say south and then hello
and then customer
and then dot customer name
and then right here plus
and comma
thanks for registering
and then phone
number
and then plus
customer
dot and then customer phone number
so let me actually put this on a new
line so you can see everything
so now let's go ahead and simply create
a new customer so new
customer
this will be
let's say that maria
and then my real phone number will be
99999
right
so
let me go ahead and cut this
and then simply say great customer pass
new customer just like that and end up
with semicolon
just like that
so
now if i
run this class
you can see that we have hello maria
thanks for registering phone number 9999
so the way that we achieve the same but
using the functional interface
so the consumer functional interface
is by doing the following
so let me go ahead and simply say static
and then
consumer
so consumer takes one argument so this
will be of type
customer and then right here let me go
ahead and simply say greet
customer consumer
equals
to
and then we'll take the customer
what we're going to do is pretty much
the exact same thing right here so let
me grab all of this
and then paste it there
and basically
now i can go ahead and simply say
greet customer consumer
dot and then right here
instead of apply we simply accept
now let me go ahead and extract this
customer
so maria
and then also pass maria right here
so there we go so this is using
functional
interface
so this is actually
consumer
and then normal
java function
right
so
if i run this
you can see that we have the exact same
thing right
so basically a consumer takes one
argument
and pretty much returns
nothing it's like a void function right
so this customer right here is the data
type of the input so is this one
this customer
is the actual name of the
uh input so this customer right here
and void you could technically say that
void is a consumer
so if you have any questions on this go
ahead and drop me a message
otherwise let's learn about by consumers
all right so let's go ahead and learn
about the buy consumer so the same way
that we've done
with functions
and by functions we have the same for
consumer so pretty much
every one of these uh functional
interfaces they have the
buy
version so in front of it they have the
buy version
so the by simply means that instead of
taking one argument it takes two
arguments
now let's go ahead and change this
function so let me simply
grab that
and
let's go ahead and simply say v2 for
example so this will be the version 2.
so what we're going to have is a by
consumer right here
and let me import this
so first we will take the customer
and then we will have the boolean so
right here so this boolean
so this boolean
will represent whether we should hide
the customer's phone number
show
and then
phone and then number
so now we have an error because we have
two arguments and because we have two
arguments we need to enclose these
within parenthesis just like that
now right here what i'm going to do is
instead of printing the actual phone
number like that
i'm going to say the following so i'm
going to put this within parentheses
just like that
and
right here i'm going to say show phone
number so if show phone number
then we can pretty much print that
otherwise i'm simply going to have some
stars just like that
so now let's go ahead and test this out
so let's go ahead and simply say great
customer v2 and let's pass maria
and then right here let's simply say
true for now
so this is actually complaining because
we need to say dot accept
and if i run this
you can see that
this is the version two of our
function now let's go ahead and simply
say false
run that
and you can see that now we are hiding
the number right here so basically if
you were to
convert this into a normal regular java
function
this would be as follow so let me just
duplicate all of this
just like that
call this v2
and then right here
we simply take the exact same variable
so show phone number
but right here
boolean
show phone number
and let me grab everything from here
and then put it here
there we go
and i would semicolon
and
i can simply now go ahead and say get oh
actually great customer
v2
pass maria
and then true
run
there we go
and you can see that we have the exact
same information so there we go this is
how you use a
by consumer
if you have any questions go ahead and
drop me a message otherwise let's move
on
all right let's go ahead and learn about
predicates so i've gone ahead inside of
the functional interface package created
this underscore predicate
class
and inside let's go ahead and simply
type public static void main
and let's go ahead and learn about
predicates
so
let's say that you want to
do the following so let's say that you
have a function so a normal travel
function so static
and then this will return a boolean
and right here let's simply go ahead and
say
is
phone
number valid
so let's go ahead and simply take a
phone number so string and then phone
number
and for now let's go ahead and keep it
very simple but this validation is not
real
but it's just to illustrate the actual
example and purpose of predicates
so right here let's simply say that this
note this phone number in order to be
valid it has to meet the following
criterias so let's return phone number
dot
and then starts
with
and
let's say that the phone number has to
start with
zero seven
and let's also say that the phone number
and
phone number
dot
and then length
is equal to
eleven
so this is a simple phone validation uh
method but this is not real it's just to
illustrate the purpose
now let's go ahead and simply do a south
here
and let's go ahead and have this phone
number so zero seven
and then zero zero zero zero zero zero
zero zero zero
and let's have another one
zero and then
nine and then we can add um you know
a couple digits like so
right
so the validation is not real but this
will illustrate the purpose so now if i
run this so let me run this main method
you can see that i'm printing the number
but what i meant to do was to actually
let me cut this
so what i meant to print was is phone
number valid and then pass the actual
phone number so same with that one
cut it
is phone number valid and then paste it
run it again
you can see that the first phone number
is valid so it meets the actual
criterias let's also have another one so
the same but this will be 10 characters
long
and if i run this again
you can see that the first one is valid
the second one the second one the second
one isn't and the third one isn't as
well
so right here we're simply doing a
simple phone validation uh check
now in order for us to use the
functional style we can use the
predicate so predicate right here so you
can see represents
a boolean valid function of one argument
right
so
right here the way that we use a
predicate is as follows so this exact
same function as predicate will be
something like this
static
and then right here predicate
and then this will take a string so this
will be the phone number
and then let's copy this exact same name
and then let's simply say predicate
and right here
this will be equal to and then
phone number
so let's actually grab this
and then this lambda
and
on a new line i'm simply going to grab
this
and then paste it
and then end with semicolon
now let's go ahead and use it so to use
it
is phone number valid predicate
dot
and then test so this is the actual
method that we need to use
so now we're going to test
the following let's test the first
number
so we know that the first number is
valid so basically this will be the
exact same thing so we need to simply
south this
just like that
there we go
and let me duplicate this
and basically we should have the exact
same output
there we go
and then paste that in
let me do south
without
predicate
and then right here
weight
predicate
and if i run this
you can see that we have the exact same
result so true false false
and without predicate true false false
so basically this is the equivalent so
the predicate is the equivalent
as
this normal java function
and the cool thing about predicates is
that you can have a bunch of predicates
and then combine them together so the
same way that we've done with function
using and then we can we can also use it
with predicates so let's say that we
have another predicate so let's have
another project so static and then
predicate
and this predicate will take a string
and then let's say contains
and then number and then let's simply
say number three so if the phone number
contains number three
so phone number
and right here i'm simply gonna say
phone number
dot and then contains
and then three
just like that
now the way that you
change predicates is as follow
so right here let's simply go ahead and
say is phone number valid so we're gonna
pass this phone number so this one right
here there we go
and then we can say dot and then
is phone number valid predicate
dot
and then and another predicate so the
other predicate is contains number three
dot
and then test
let's actually pass
this number right here so the last one
there we
go and let's wrap everything without a
system.print line
let me put this on a new line
and
just like this there we go
so if i say right here
is
phone
number valid and
contains
number
three
equals and then plus
the actual result
let's do the same for a different number
so let's actually
let's actually do for this one very
first one
so right
here and let's actually put a three
right here
so
this should be false because the phone
number
isn't valid but it contains number three
right here but this one the phone number
is valid so it starts with zero seven
eleven characters long
and contains number three at the end
so let's go ahead and
run this method
and there we go so you can see that the
second one is true as i explained why
and the first one is false
now you can use ands but you can also
use or
so or means either one of them is true
so if i was to say right here so
instead of and i can use or
right and then if i remove
three here and put a zero
run this
this will still be true if true because
we only need one condition to be true if
you use an and so if you chain multiple
predicates together using an and all of
them have to be true
so the same way that we have
predicates we have by predicates by and
then predicates right here and basically
they are the same thing
but instead of taking one argument they
take two arguments
so i'm not going to give you a full
example on by predicates because you
should be familiar now with by
predicates by functions by consumers so
on and so forth so if you have any
questions on predicates go ahead and
drop me a message otherwise let's go
ahead and learn about the final
functional interface i want to show you
and then we can start putting all the
puzzle together so that all of this
starts to make sense and how you
actually end up using it
join me in the next one see ya
all right go ahead and inside of the
functional interface package create a
class called underscore supplier with a
public static void main
method
so
let's go ahead and learn about what a
supplier is so if i go to the docs
right here you can see that they say
supplier t
represents a supplier of results
so basically
sometimes you might have a method right
here that simply
uh let's say that
we have a method
like that so like
a method like this so static and then
get or actually string
get
connection
url
watch let's simply say get db
connection url right
so
this
will
take no arguments
and let's simply return
and then for example j dbc
column for slash forward slash localhost
and then
5432
forward slash
let's say
users
right so this is not real but you get
the idea
so
the way that we can
use this function now is if i simply say
south
and then get
db connection url
and then if i run this
you can see that we get the actual
connection url for some kind of database
so the purpose of this function right
here is simply to return
a value right here so this is the value
that it returns
so we can do
so we can use a supplier to represent
the exact same thing so i'm going to
copy this url
and to
have this java function as
functional
we simply have to say supplier or
actually static
and then supplier
and then this will supply a string
and then get
let's
grab this
and then supplier
and right here equals two and then what
we need to do is simply have
parenthesis
and then we simply pass or actually
return this
just like that
so if i put this on a new line so you
can see exactly what i'm doing
and you can see that now this is the
exact same thing but if i go ahead and
simply say system.out.life
so south
and then right here to use this i can
say get
db connection url supplier
dot and then get
so now if i run this
i do get the exact same result
so this is pretty much how you use a
supplier so a supplier simply
returns
any kind of value that you want right so
this data type doesn't have to be a
string it could be you know your custom
classes your custom objects
um
you know an array
pretty much anything that you want so
for example if you wanted to return a
list right here you could simply say
list
and then of type and then string right
here
and then here i would say list dot of
and then just like that and that would
semicolon right so now if i run this
you can see that this now is a list so
if i was to return
something else so
right here
let me simply add comma new line
and then this
like so so it's much neater
just like that
oops just like that
and right here let's simply say uh
customer for example right
so get a connection urls
and then supplier and then here urls run
this
and there you go so now we are returning
a list with two urls
so if you have any questions on
suppliers go ahead and drop me a message
otherwise let's move on
all right so by now you should see
exactly the differences between
writing a normal java function and
functional interfaces
so
the benefit of you know knowing the
functional interfaces is when you start
to use optionals and streams but more
specific streams right so once you start
using streams with all of these
functional interfaces you will start to
write clean code and that's what i'm
going to teach you next
okie dokie now that you've learned about
consumers functions by functions
predicates by predicates and suppliers
and also i think by consumers
now let's go ahead and actually learn
how this is useful to us when writing
code
so
go ahead and create a new package under
java so package
and call this as streams
now go ahead and create a new class and
simply call it underscore and then
stream
just like that
enter public
static void main
enter
now remember inside of the imperative
uh package go ahead and open that up
and let's actually steal some code so
right here let's steal
all of this so the enum gender as well
as the
person class
so let me close this
and then right here let me paste that in
and i'm gonna cancel because i don't
want to import from imperative.main so
cancel
and
let me go back again
and let's actually steal some code again
so let's take this list right here so
we're going to take this list and then
we can close this once more
and then right here inside
let's go ahead and paste it
and you see right here it's trying to
import from imperative.main
cancel out of it
and let's simply import the actual list
ourselves so i'll just press option
enter and right here let's import mail
and gender
just like that and finally let's go
ahead and add a static import for those
so i'm just pressing
option and then enter add static import
now let me go ahead and close this
and
let's actually use what we've learned in
the previous section
with streams so this is pretty much
where everything starts to make sense
so
let's go ahead and use a couple of those
functional interfaces that we've learned
so if i collapse this or actually open
this so consumer function predicate and
suppliers
so
go ahead and simply say people
dot
and then stream
right so
stream pretty much takes us to
an abstraction mode and if you want to
learn about all of this i've got this
course right here where i teach
everything about the java streams api
because once you've
because once you learn this
the way that you write your java code
will change quite significantly so stuff
that you use to take 20 lines of code
you can simply collapse into perhaps
five lines of code and to be honest
knowing how to use the java streams api
will make you a much better developer
so i also have a free video on youtube
where i teach about filter soar group by
all match basically this is a an
introduction of this awesome api but for
now let's go ahead and learn some of
these methods
so go ahead and say people.stream
and now go ahead and simply say dot
so what i want you to do is to actually
look
carefully so right here so look at the
methods that we have we have map collect
filter find first for reach all match
but what i want you to see is exactly
look the map function takes a function
right takes a function
of some type that extends person and
then you can see the other one so the
other argument so question mark extends
our
mapper so this is exactly what we've
learned right so a function takes an
input and
produces an output
so the same with collect so collect
is a supplier
and you can see you buy consumer so this
is awesome right this is everything that
we've learned so the same with filter
takes a predicate
find first so this returns simply an
optional
for which takes a consumer right
let's look another one so for example
drop wow this takes a predicate find any
flat map so this is a very useful
function
takes a function inside
and right here flat map to wind
map to double so this takes a two double
function right
and you can see right here
non match a verify you of a very useful
function as well
reduce
binary operator
and then right here you have another
reduce
and then look at this
sorted takes a comparator it's not
relevant to us
take wow you can see that this takes a
predicate
and to array into function basically
this is pretty much everything that we
have learned right so functions by
function consumers predicates
and the other good stuff so basically
this is now where everything comes into
action so let me go ahead and show you
for example how to use this map function
so go ahead and simply select the map
function and the map and the map and and
the map function simply does
transformations
so right here this takes a person
right and from here what we want to
return so i want to return person
dot and then the actual gender
now let's go ahead and simply say dot
and then collect
and then collect us to set
right
so
and also um
let me extract this to a variable
genders
and then south
and then genders
or actually not even south so let me let
me simply remove that
because i want you to see exactly what
we're doing so right here so instead of
collect let's go ahead and simply say
dot
and then look at this
for each takes a consumer and we've
learned about consumers right a consumer
simply takes an argument and produces
nothing so void right so right here we
will take a gender
and what we want to do with this gender
so we want to simply do a
system dot out
dot print line and then gender
and now we can use method reference with
this
like so and beautiful code
now let me go ahead and run this method
so you can see what we are doing
tada there we go male and female right
so you can see that we mapped everything
so
right here
we
stream the people list
so
oops
so right here
we stream the people list right contains
a bunch of people right here so john
maria
aisha alex and alice
so
we map so we perform a transformation on
that list so we convert
every single one of them into a gender
so now we simply have a list full of
genders
and then we collect to a set removing
duplicates and then we simply print
that's why you see that we have only
male and female
right here
so if i was to have another gender right
here
cannot
actually
prefer
not to
say but basically this is not gender i'm
just giving you an example so right here
let's add another one another person
right here
there we go
then prefer not to say
this will be bob
and you could tell that this is a gender
but
so if i run this
you can see that now we have prefer not
to say as well right here
so if i was to now
map this into
the actual name right so let's say name
and then run this
we simply now get the actual names
instead of genders
so this is really really really awesome
so let me actually let me also show you
that we can simply get the length of
each name so let's also perform another
map operation
right or in
or in this scenario let's say map
to int
this takes the actual
uh
name so this time it's name
and then simply say name dot and then
length
and
right here we're not going to collect
set
so let me delete that
and then we can even change this to
method reference
just like that if i run this
you can see now the length of each name
so this is the power of streams
so you might be asking you know what is
this
right or what is this
or what is this
so let me just show you right now so
what i'm going to do here is simply
extract this to a variable
and also this to a variable
and also this
to a variable
and now check this out so this is what
you've been learning in the past videos
so look at this function
that takes a person returns a string
and then the same here two in function
we haven't learned to int function but
basically this is the same thing so this
takes a string and returns an integer
that's all it does and you can see right
here in consumer so an end consumer
simply takes
an integer and it prints out the result
so right here so let me actually replace
this with a lambda so you can see
exactly what is this you see that
this is what we've been
learning throughout this course but
when you use it with streams you don't
have to
have all of this syntax basically you
can ditch the left hand side and just
keep these right so if i now inline
everything so
command shift n
command shift and there
and look at this
right
and here even method reference
and there we go so if you want to learn
more about streams
go ahead and check my course on java
streams api where you learn
everything about writing code in a
declarative way
this is all for now join me in the next
one
all right let me show you some other
methods right here so still right here
so let's go ahead and
uh for now i'm gonna pretty much just
comment everything right here so i'm
gonna comment this
and right here let's go ahead and simply
say people dot and then stream
and then right here let's go ahead and
simply say all
and then match basically we want to know
that in our list we only have females so
right here so this takes a predicate and
remember a predicate looks like this
so this will take a person
and then in this list we want to make
sure that we
only have
females so female
dot and then equals
person
dot and then
why should not people person
dot and then gender there we go
and now and i'll end this with semicolon
and if i extract this to a variable you
can see that this
is a boolean now let me go ahead and
simply say contains only females there
we go
and if i do a south on this south
and then contains only females
and let me actually go ahead and
destructure all of this so i'm going to
extract this to a variable
and check this out so right here so
we've learned about predicates right so
this is what we've learned previously
so now let me go ahead and run this
again
and you can see that it's false right
because in our list
so right here in our list
we contain both male and females
so
if you want for example to say um
or is
say the opposite you could say for
example i think match let's look at for
let's look at match so any match right
here right so any match
so any match
means
any match taking our predicate so
if we have at least one female then this
will return to
run this
there we go true right so if i for
example remove all the females here
just like that
run it again
and it's false now right
so let me undo all of that
and finally we have uh i think it's
non-match
right so let's say that now we change
this to
prefer
not say
and let's remove bob from here
so basically
i want to make sure that in our list we
do not have someone with prefer not say
gender
if i run
this you can see it's true
if i add bob back
this will be false
there we go
so you can see the power of streams
right so
obviously we have
other methods really really useful
methods so if i do a person or actually
people dot and then stream
dot and
look at those methods right
so
mean peak reduce sorted
take wow
um
so
right here drop while
not much you've seen
find first
map and map to int so basically
once you learn
all of this the way you write java code
will change and you will become a much
better java developer
so this is all for
this video next let me also show you
that you can use um you know all of the
goodness of java functional interfaces
with optionals because you know again
optionals changes the way that you deal
with null pointer exceptions in java
join me in the next one see ya
all right let me go ahead and show you
how to use java optionals with
functional interfaces so if you've never
heard about java optionals or if you've
never used java optionals go ahead and
check my youtube channel where i've got
this
crash course on java optionals
and i highly suggest you to learn how to
use optionals because
they will help you a lot in terms of
dealing with null pointer exceptions
so let me go back to intellij
and let me create a new package
so right here
optionals
and inside let's go ahead and create
main class
and right here
public static void main
and there we go
so now let's go ahead and simply say
optional
dot
and then off
nullable
so right here let's simply say no
right
and
if i now press
dot
check this out you can see that right
here you have a bunch of methods but if
you look closely again so these methods
they do take for example the first one
or else to throw
takes a supplier right so this supplies
an
exception you can see or else
or so right here
this takes a supplier
map again function
if present consumer
filter
flat map
so right here if present or else right
is present or else get so this is a
supplier so let me actually use this one
or else get
and then remember suppliers so right
here
oops
there we go
so let me simply say
default
value
just like that
extract this to a variable
and let me simply name this as value
now if i go ahead and simply say south
on value
there we go
if i run this
you can see that we get default value
but now check this out if i simply add a
value here
so
hello
run this again
you can see that we get a low right
so let's
try something else so instead of or else
get
let's go ahead and simply say dot
or
so right here so or else
throw so this again we have to
supply an exception
oops just like that
new
illegal set exception
and you can have for example a message
right there
and
exception
for example right
so
you can see that if i extract this to a
variable
this is a supplier and we've learned
about suppliers remember so right here
so this is a supplier
right
it takes no arguments but it supplies a
value so there we go
now let me again show you something else
so if i delete this
so instead of saying or else throw let's
go ahead and use for example
so let's use something that has a
consumer so if present
there we go if present we receive the
actual value here
and then we do something with the value
so right here let me simply remove that
and let me move this above
and then this
there we go
now because i only have one single value
but basically you could do like some
logic right here right if you had like
you know something that you wanted to do
like a business logic right
so now
because i only have one value i can
delete all of this
i can delete that
and i can even go one step further
use lambda method reference
just like that and look at this looks
beautiful so now let me go ahead and
simply say john
gmail.com
in fact let's go ahead and pretty much
um
have a custom message so right here so
email
and what we're gonna do is simply say
south
and then sending
email to
and then
email
there we go
now if i run
this you can see that right here
we do get sending email to john
but now
as soon as i pass a null here
right
if i run this
we get nothing but now i can go ahead
and pretty much change this with
so there is another method right here so
if present or else takes the consumer
and then the other empty action is a
runnable so that
is our consumer so let me actually grab
this so you can see what am i doing so
this is the first one so this email
and then right here we have to pass
comma
right comma
and then a runnable so a runnable goes
like that
and there we go
so let me
pretty much just put this on a new line
and this on a new line just like that
and then if i close that
indent things
there we go and then here what i'm going
to do is simply say south
cannot send email
there we go now if i run this
you see that we have cannot send email
as a default value so
again i can remove that and have method
reference
and look at how this now starts to flow
guys
so this is the power of functional
programming streams optionals
and you know once you start writing code
like this you can achieve a lot more
instead of using imperative approach so
as i said go ahead and roll to this
course right here where i teach
everything you need to know about java
streams api
also if you want to learn about
optionals go ahead and check this video
where i show you how to use optionals
and how to be safe with no values
this is all for this section if you have
any questions go ahead and drop me a
message
otherwise let's move on
let's go ahead and learn about this
awesome design pattern called
combinator
so
let me go ahead and first create a new
package
and right here i'm going to simply name
this as combinator
and then pattern
so
right here what i want to do is to
create a class so i want to create class
and let's say that we will start with
the user so the user actually a customer
so we have a customer
and this customer has few properties so
let me collapse this so private final
string name
let's also give it an email
phone number
and let's also give it a local date for
date of birth
now let me go ahead and pretty much just
add those to a constructor
and let me generate some getters right
here so get it
just like that
so
now let's say that you
have a system right where customers do
register to your system and you and you
need to perform some kind of validation
right so what you would do for example
would be something like this so inside
of your package you would create a class
and then customer
validator
and then service
right
and in this class right here you would
have few methods to validate few
properties of a customer right
so you could have a method so right here
you could say public
and then boolean
is
email valid
right and this takes a customer
or better takes a string
email
right
and right here you'd simply say return
and then email
dot contains you know this is a rubbish
validation but this is just for
illustration purposes
right so contains that
or actually contains the add symbol for
now let's keep it simple
then you might want to have order
validation for the actual
um
phone number so right here so is and
then
phone
number valid so this will be phone
number
and then right here phone number
right here contains
so instead of contains let's go ahead
and simply have a simple validation so
it starts with
and then
plus and then zero right
and then what did we have so if i go
back so we have uh date of birth
so let's go ahead and simply say that
the date of birth so right here
is adult so is adult
so right here we take a local date
and then
dob
and then what we're going to do here
is simply say
period
dot and then between
so we're going to
calculate the number of years between
two dates so the start date will be uh
inclusive so this will be the dob so
customers and date of birth and then
local date dot and then now
and then right here
dot and then get years and let's say
that for someone to be an adult they
have to be
over 16
just like that
right so if i put this whole screen so
you can see
so
this is then your customer validator
service right so now the way that you
would use it is
let's go ahead and create a new class so
main
and then public static void main like
that
customer
and then customer equals to new and then
customer
and then right here let's go ahead and
simply say that this is alice
and then she has an email of alice at
gmail.com
and then the actual phone number is plus
and then zero so oops plus and then zero
and then any random number
and finally let's go ahead and simply
say that she
was born
so local date of
and then 2 000
just like that
and then one and then one and there we
go so if i collapse this
and you can see that now
we have a customer that we can perform
some validation now obviously
um
you know this um validator service right
so this could even be private
so private
and then private
and then right here so let's go ahead
and combine the both
let's go ahead and combine everything so
is
oh actually let's go ahead and simply
say public so this is the actual
uh interface that we provide to clients
so public
and then boolean
and then
is
valid
just like that
and this now will take a customer
and now what we're going to do is simply
say return
and then is
email valid
customer dot get email
and then and
and then
is phone number valid customer dot get
phone number
and finally and is in then adult
customer dot and then get
and then dob
just like that
so this is actually you know normal code
uh
but
so
what i want to teach you really is how
are you going to be able to use a
functions to
change this in a much better way and a
neater way right so here if you have
extra validation you have to have
another method
like that and then simply append here
and then basically right here you don't
have
visibility
whether um you know whether the
phone number was invalid or it wasn't an
adult or or anything right because this
simply returns a boolean right is valid
so if i go ahead and show you what i
mean so right here
let's go ahead and simply say customer
validator service
service equals to actually let me rename
this to
uh validation validate the service
new and then customer validated service
now let's go ahead and say validator
service
dot
is valid and then pass customer
there we go so what i'm going to do
actually is in line this oops not that
so basically i want to inline
um
this guy right here so like that
and then i'm gonna do a south
just like that and then end that like
that so now if i go ahead and run this
there we go so you can see that this
customer is valid and from now on
let's say that
we
can
store customer
store customer in
and then db right
so
now
um
actually let me say if and then valid
right
so now if i go ahead and pretty much
just for example remove the add sign
run this
you can see that now it's false so it's
not valid
and also let me go ahead and remove the
plus sign right here
there we go
still not valid but now you don't know
exactly you know what is wrong with our
validation so the combinator pattern
it's an awesome design pattern that
allows you
to chain functions together so basically
a combinator is a function that might
take other function as arguments and
returns new functions
so this is an awesome pattern that i'm
pretty sure that you will love and
that's what i'm going to teach you next
all right let's go ahead and use the
combinator pattern to improve
this validation service right here that
we have so validated service actually
so this validation sub so so this class
right here
is
okay but we can do
way better than this right because right
here for example if you want to have
um you know a different method that
validates for example
just email and their phone number then
you would duplicate this
so right here
and then you would say is valid
phone number and email
and basically just remove for example is
adult from here
and we don't want to do this right so we
want to have a very
beautiful interface that we can simply
chain all of these validations together
so let's go ahead first and expand the
project tab
and right here let's go ahead and simply
create
a
class
and this will be called customer and
then
let's actually name this as registration
validator
there we go and instead of a class let's
change it to an interface
now what i'm going to do here is the
following so
right here i'm going to say that this
will extend
a function so remember we've used
functions right here and the first
argument that we want for this interface
is a customer
so the type of is a customer
and then right here let's go ahead and
simply say validation
and then result
so let me end that
just like that
and let me put this on a new line so you
can see exactly what we're doing
now this validation result this will be
an enum so enum
and then validation or let me actually
just copy and paste this so validation
result just like that
and now i can import this oops
actually i did the right thing and let
me actually do a static import right
here there we go
now here what i'm going to do is have a
you know few results that i want my
validated service or actually my my
customer registration validator to have
so one i want this to be success right
so this is where all the validation is
successful
then we have few
errors so right here we have
phone
number
not and then valid
we also have email
not and then valid
and we also have
is
not
an
adult
just like that so the way that we're
going to
use this with this interface is as
follows so this is where combinator
pattern
uh kicks off so right here we're simply
going to have the validations that we
want right so here you can have as many
validations
as you want and then you chain them
together at the end so right here let's
go ahead and simply say static
and then right here what we're going to
return is
this guy right here so the same
uh interface so right here we will
return this function right here right
that takes a customer and returns a
validation result so right here i'm
gonna say is and then email valid and
then i'm not taking anything there
but right here what i want to say is
return
and then
customer
and right here i'm going to say customer
so basically the exact same
validation that we had before so
customer
dot and then contains
and then at
so if this is the case
what i'm going to do is simply return
you guessed it success
otherwise phone number
oh actually no phone number uh email is
not valid and let me have a static
import for this
and the same for success
there we go
now let's go ahead and do the same for
phone number
right here let me actually steal some
code so from here
what did we have we had this so i'm
gonna copy that go back
and then is
phone number valid
and then i'm going to
change that with this
and then customer
dot
get phone number
starts with that
so if that is the case then it's valid
otherwise so right here phone number is
not valid let's go ahead and do the same
for
is adult so right here
static
and then customer registration
validator
is
an adult
there we go let me steal some code from
here so
this is what i want
just like that go back
and then customer
will actually return customer
and then i'm going to paste
that
and then right here
customer dot
and then get dob
so if this is the case
right so if the customer
age is bigger than 16
what i'm gonna do so let me put this on
a new line
so if it's bigger than 16 what i'm gonna
do
is simply return success otherwise
is not an adult
there we go indent this properly
and there we go
so
one final thing that now we have to do
is right here we have all of these
functions of these three functions
and the combinator pattern allows us to
chain all of these functions together so
the same way that you saw with a
function where we can use and then and
the same with predicates by using
i think it was and
right or or we can have the same logic
with the combinator pattern so right
here what i'm going to do here
is the following so i'm going to have a
default
method and right here i'm going to
return a customer valid data so the same
thing that we did here and then i'm
going to say and
right so right here we're going to take
a customer data
and then i'm going to call other
so basically this is where the magic
happens right
so i'm going to say other
and
inside i'm going to return
and then customer
and then inside what i'm going to do is
simply say this dot and then apply
so we are going to apply
this customer
and i'm going to shrink this to a
variable so i'm going to say result
and now check this out i'm going to say
return
and then result
dot
equals
and then
success
so if it is equal to success
i'm going to apply the other
validator
dot and then apply and then customer
otherwise i'm going to simply pass the
actual result not super result
there we go
so if we get to this point we know that
we have an error
so now this is complaining because i
need to
pass semicolon right here
so let's go ahead now and use this
awesome interface
so let's go back to the main method
right here
and
now let me simply comment this out and
let me simply add an add sign right here
or actually let me simply
leave it as it is and then i'm going to
show you exactly all the errors so now
let's go ahead and simply say
customer or actually not here um
so
using
combinator
pattern
so right here let's go ahead and use a
customer
registration validator
and then
dot
and check this out so we have all of
these methods right is an adult is email
valid and is phone number valid
so let me go ahead and simply start with
is
a email valid right
but now what i want to do is the
following
so right here i'm going to say
dot
and then check this out we have this
method so and right here so this is the
one that we defined so i'm going to say
and
and then let's use the rest so if i do
if i press
ctrl shift you can see that we have the
other methods so is phone number valid
dot and then and
and then is an adult
there we go
so
if i
now
add a static import
you can see that now this becomes much
much cleaner so now
we have these three methods that we have
chained together the way that we use it
is simply by saying dot and then apply
and then pass customer
there we go
now if i extract this to a variable
so result
and let's go ahead and simply say south
and then result
so now
what i'm going to do here is say if
and then result
not
equals to success so success
we're going to throw
so let's throw
so throw
new illegal state exception
you could have your own exception but
for now let's go ahead and throw an
illegal state exception and then the
actual result
dot
and then name
there we go
and basically this will actually print
out the same thing right here but i want
you to see the actual result so
let's go ahead and scroll up and let's
start with a valid customer so at gmail
and then the phone number has to start
with plus and then zero
so if i go ahead and run this
there we go we have success right here
and
if i scroll up let's now check all the
validation that we have so let me remove
the add sign
and then
run
there we go you see that email is now
valid and we've thrown an exception
right so email or valid
so if i add that back
and if i remove it plus here
run this
there we go phone number not valid
and if i had a plus there
and let's make sure that this guy was
born
uh you know 2015
or this guy
alice was born 2015 so which means that
you know she's not an adult so if i run
this
you can see that is not an adult right
and you can even customize your
exception that we throw by including the
name or you know an id or anything like
that but basically now you can see the
power of the combinator pattern so
if you have more stuff that you need to
change to this validation so let's say
that the requirements change you can
simply duplicate this
and then have your custom implementation
inside of the
um interface that we've defined defined
so this one right here so customer
registration validator
and then just chain all the validation
logic and scenarios that you have and
this is the power of the combinator
pattern so one last thing that i want to
show you is that
so if i
uh command
z a couple of times there so basically
so right here you see that we call dot
apply so if i comment this
and then right here let me simply
remove the apply there
so the cool thing about um you know
these functions so let me change the
variable right here
to a validator so the cool thing about
this right is that this is
lazy right
none of this stuff is run until we
invoke the dot apply so if i say result
right here you can see that we have all
of these methods right we can compose
applies on and so forth but
if i run this
you can see that nothing gets printed
out and even if i for example right here
so
where we um
return here so the email
value
so right here so is email valid so let
me grab that
and then let me simply south
and then
running email validation
and then right here simply return that
and end up with semicolon there so even
if i run this
you can see that we still get nothing
but as soon as i
uh revert all of this
just like that
and then call apply
now check this out if i run
you can see that we have the running
email validation and i think i need to
undo all of this
and then run it again
and there we go so running email
validation right and then we have is not
an adult and the actual exception
so i hope that you see the power of the
combinator pattern
i
use it whenever i can to perform
validation like this and you should also
do it if you have any questions go ahead
and drop me a message
otherwise let's move on
all right so in this final section let
me go ahead and explain you few things
that
will help you to fully understand
the functional interfaces and functional
programming with java so so go ahead and
create a new package
and
in this package we're going to call it
as final
and then section right here
so inside create a main method
and then so let me
put this full screen like this
and then ps
and then vm for public study void main
and
right here what i'm going to do is i'm
going to write some javascript and there
we go so what i want to touch on this
video is call backs so
maybe you've done some javascript and
javascript they have this notion of
callbacks so a function in javascript
looks like this so this won't work in
java but it looks like this so you have
a function
and then you give it a name so
hello
and then right here you might have for
example a variable so let's say and then
first
name
last name
and then call back
something like that right
so
right here what we're going to do is
let's say for example the last name is
not provided then we simply have like
console.log so let's simply say
console.log
and then right here
first
and then name
and then i can say if
if we have last name
then we're going to console.log
and then last name
otherwise we will have this callback so
we're going to invoke the callback right
so you might have seen code like this in
javascript basically this is an extra
function so if i take this command c
and then go to chrome and then inspect
and then right here go to console
paste my function there
and then enter
you can see that we have undefined now
if i say hello
and then say
first name
john
and then pass
now there
and then right here i can pass a
function so this is a callback right
so
console
dot and then log
and then no
last
name provided and then end this with a
parenthesis here
if i now run this
oops we have a error there and they
should shouldn't have the column there
so enter
and you can see that now we have john
and then no last name provided
so basically you can see that this
callback right here is a function and
you can have like you know a lot of
logic in it right
but you know this is very simple so
we're just logging but if you want to
have for example
an extra logic that goes to the database
and there's you know some other stuff
you could definitely do it i'm just
showing you the callbacks in javascript
so right here if i now pause so right
here if i now say alex and then montana
and then
run this
you can see that now we're not printing
the actual callback right so now we have
john and then montana the last name so
we can achieve the same in
java using functional programming and
the functional interfaces so the way
we're going to do it is as follow so let
me simply
comment this right here so you have it
for reference
so now what i'm going to do is actually
have a static
and then void and right here what i'm
going to do is actually combine both
worlds so we can have a
regular method so normal java method
and also
inside as a parameter we can have a
function right so this is you know
programming because now we are free to
do whatever we want so right here you
see static void and then let's go ahead
and simply say hello
and then right here string
and then
first and then name
string
last and then name
and then right here
what do you think we need right so
remember we've learned about functions
by function consumers suppliers
predicates so
we kind of just want to have a function
that might take for example last name if
it's there print it otherwise have for
example a default message so right here
what we're going to do is simply say
consumer
and then of type and then string
and then let's simply name this as call
pack just like that
there we go and then right here let me
simply say south
and then first name
and then i'm going to say if
and then
um
last name
equals to no
right we could even say is blank right
but equals to null so if it's equal to
null or actually let's revert this so
it's the same as the
function that we have right here as
javascript so if it's not equal to no
let's go ahead and say south and then a
last name
otherwise else
we're going to say call back
dot and then accept and then the string
that we want is
first name for example right so let's
take this first name right here and then
do something with it
so now the way we use it is as follow so
let's go ahead and say hello
and then john
right here
and then montana
and right here let's have our callback
so let's go ahead and simply pass now
for now right
so if i run this
there we go you see that we have john
montana but now let's go ahead and
define this callback so right here we'll
have um
a callback that takes a value and the
value will be the actual first name it
doesn't matter and then we will do
something with it so right here
south
and then what did we have so
right here we said
last name not provided so let's do the
same here
and then four
and then plus and then john and then
value right
so you could even you could even change
this to first name if you want but i'm
just going to leave it as value now if i
run this
you see that we have john montana but
now watch this if i pass no right here
and then run
there we go
no last name provided for john so
basically i just want to show you that
you can combine
you know a normal function
with
these functional interfaces and do stuff
like this
and achieve
a similar concept
as javascript that they call callbacks
so there are other languages that have
the similar
concept but javascript you see a lot of
these callbacks been thrown and you can
pretty much do the same with functions
another way that you might see callback
is
so right here so because right here you
might not want to pass a default value
right so you might have something like
this so right here
let me just duplicate this
and then call it two
and then right here so i'm gonna simply
remove this
and then runnable
and this will be the callback
and then right here
call back dot and then run
right so this is a bit different now
so right here the way we use it is
simply say hello
like here and then two
and then right here you simply pass this
runnable right here and you have no
value there
basically this now is the exact same
thing
as
this concept right here right because
we're not taking any value and because
um i only have one line there i can
simply do this
and then check this out just like that
and beautiful code so now if i run this
you see that it's the exact same thing
but right here we're not taking any
arguments
so there we go so
this is how you combine both worlds
where you can mix and match these
functional interfaces with normal
functions and have callbacks if you have
any questions go ahead and drop me a
message otherwise let's move on
let me go ahead and teach you about
lambdas
so we've been using lambdas throughout
this course with you know all these
functions but basically let me show you
exactly how they're used and for example
if you only have one return statement if
you have two arguments what you should
do
uh when using lambdas so
let's go ahead and
expand this so commander n1 and right
here for this main method let me go
ahead and simply name this as callbacks
just like that
now let's go ahead and create a new
class and then simply call it as lambdas
there we go
oops not lambers but lambda
and then this
just like that
now let me collapse that
and then public static void main
now inside let's go ahead and have a
function
there we go that takes a string
and returns a string
there we go so let's simply say
uh print
and then name
equals two so this guy will take name
and then right here
so this part right here it's a lambda
right so this is a lambda where
this is the actual argument right so
this name right here
is what comes from this data type so
right here then you have to pretty much
just
uh do the following so if you have only
one line so if you simply return
immediately you can simply do this so
south
and then
actually not even south my bad
so you can simply um and this is not a
good example so not print name
but let's simply say
upper and then case
name for example something like this
right
so now right here if you simply return
immediately you can simply do this so
name and then dot and then uppercase
right
so you can immediately do this so this
is only when you have
one
return statement
right after
this um
error right here
so because you only have one return
statement and you can even see that
intellij is telling you that you can use
a method reference so this is because
right here this is the same as doing
this
right it's the exact same thing so
this is more understandable
but you know once you get too
comfortable with this you'll start to
use code like this
now
another thing is
right here so
if you have to do some extra logic and
not return immediately you need to have
these curly braces right here so now you
can have a logic right so you can for
example say if
and then name
dot um or actually is blank right here
so if it's blank
then you can
throw
new illegal
and an argument exception something like
this right
and then right here
otherwise now
because
i need to return i need to say return
and then name to uppercase but also i
need to
add
semicolon right here
right so you can see that now
this is the actual method body for this
lambda right so this lambda so this is
the entire thing right here
so another thing that you could do with
lambdas is you see right here
where you have name
so if this is a by function so by
function
uh just like that
and then right here let's simply have
um
let's have an integer right here integer
and this will be h
right so now you see that
i need to put age right here because
this data type is for this one and this
data type is for the second one
right and string is what this by
function returns now because i have two
arguments i need to do this
i need to enclose them within these
parentheses so let me actually go back
for a second so right here so where we
have a function
so you see this is a function
i can even you know enclose them with
parentheses but because i only have
one
argument
right into this function right here i
can simply ditch
this right but if you have a by function
and right here let's simply say integer
again
and then
age now you need to enclose like that
right
and you could you know
do something like another logic right
here so south for example
and then h something like that right
and you know the way you'd use it is as
follows so uppercase
name dot
apply
and then
alex let's have alex here and alex is 20
right
there we go and this returns a string
right here
upper
cased
name
and then south
and then uppercase name
i can even in line this like so
if i run this
there we go so 20 and then alex
so basically this is uh this logic
doesn't really make sense but i just
want to show you exactly different ways
that you can use these lambdas right
here
so
if you have any questions on lambdas go
ahead and drop me a message otherwise
let's move on
all right so just to let you know that i
think i didn't mention this at the
beginning of this course
but when you use these functional
interfaces
you see right here where you define the
actual
inputs and outputs of functions right so
string and an integer and then string
so these right here they have to be the
object type and not the primitive so for
example integer right so you could have
an integer like this
so integer and then number
equals to no right
and then you can also have integer and
then let's say counter
and right here
i can't say
this
so this is invalid right because this is
the primitive
um
version of
this object type right here right so
right here you either have to say a
number so it could be negative number or
positive number so if i do a zero right
here you could see that this goes away
so
for that reason
you are not allowed to have
primitives in here right because if you
have to pass nose then then you can so
with primitives you cannot
so right here you see that we have this
integer right here so i can go ahead and
simply say no right here
and you see that works but if i was to
have this integer so the primitive type
that does not work
and also
one thing that i've been doing a lot
throughout this course
is having static right here so static
so the reason why i've been doing static
is because i'm outside so right here i'm
outside
of the main function and the main
function is static right and you can
only use
static methods inside of this main
function so right here you can see that
this still works but if i remove the
static keyword you can see that
it does not work right so this is
something that to bear in mind uh your
functions and buy functions they don't
have to be static right in fact having
static throughout your code base is not
practical so ideally what you would do
is
simply have for example so let's say
that you have a class let me actually um
put this inside
so just like that
so let's say that you have a class
and then call this a service for example
right
what you should really be doing is like
having you know publix
and then your function for example or
predicate or consumers right so consumer
of string
and then blur
[Music]
equals two
and an s right
just like that
and then basically this is your public
interface
you can also change this to private
or
no
access modifier at all so this is
package protected right
so just to bear in mind that
you know they don't have to be static
and you should avoid static but the
reason why i've been using static is
because i've been doing everything
inside of this public static void main
method
so there we go if you have any questions
on primitives and
the use of solid keywords go ahead and
drop me a message otherwise catch me on
the next one
all right let me go ahead and quickly
show you the rules that come with
functional programming and everything
that we've learned throughout this
course so the first thing i should know
is that
um
there should be no state around
your functions right so the functions
they have to be pure functions so they
shouldn't depend on any other state so
for example if you have a global state
like
or a global variable your function
should not depend on that global state
right so that's the difference between
pure and in pure functions so pure
functions
don't depend on global state impure
functions do depend on global state
and that comes with the side effects
right so your functions should have no
side effects
other than
everything inside of the body of your
function right it shouldn't have a side
effect
outside of the function and also higher
order functions so
a function is considered to be higher
order function
if one of these two conditions are met
the function takes one or more functions
as parameters so we saw how to so we saw
how to pass
functions inside a normal functions
right
or if the function returns another
function as result so with the
combinator pattern that's exactly what
we've done so let me actually show you
so right here so with callbacks so right
here
so right here
so you can see that right here we are
passing
a function right here so this is a
function right and also within our a
combinator pattern so remember so
customer and then registration validator
check this out so right here
we are
returning
a function so this interface extends
customer
or actually extends function that takes
a customer and sends a validation result
but if you check the return type of
all of these methods right here so if i
do a
command shift
and then minus you can see that
they all return
the function right here so this means
that
we are satisfying with this property
higher order functions
so
make sure that for example let me
actually show you uh you know the no
state and pure functions inside effect
so if i go back to
let's say lambdas right here
so you see this function right here so
ins by function right here so
i should never depend
on for example counter right so let's
say that i do for example south
and then counter or if i change the
state of counter right i should never do
this because it will break
all of this right so you see that there
might be some side effects on the
outside of
this function right here right so
outside of this function there might be
there's some side effects
also you see that
you are now mutating state
and it's no longer a pure function it's
an impure function because it depends on
um
global state so you never want to do
this or for example increment the number
so number
and then you know increment um you know
plus plus something like that you should
never do it right so your functions
right here they should be
self-contained right
so they should only depend on the state
that you define inside so right here i
could say in inc and then counter
equals to zero so this is absolutely
fine right because this counter
so this counter right here it's only
visible inside of this function and not
the outside world
so if you have any questions on these
properties
go ahead and drop me a message and i can
give you more examples but this
is pretty much everything you need to
know about functional interfaces and
functional programming
with java
if you have
any questions go ahead and drop me a
message
next let's go ahead and wrap up this
course
okey dokey amigo
i want to congratulate you for
completing this course and practicing
throughout so as you saw for you to
write functional programming you just
have to shift the way that you think you
no longer think about regular java
functions but you start to use functions
by functions
predicates consumers suppliers so on and
so forth
now the next step for you is to enroll
to my course on java streams api because
once you combine everything that you've
learned in this course with java streams
you will start to write code that really
really shines
instead of you writing code that takes
for example 20 lines of code you simply
take
five lines of code with streams and
functional interfaces
so this is all for now go ahead and
subscribe to my channel to get more
videos like this and it really means a
lot to me to see a lot of you you know
sending me messages suggesting for new
courses
and i'm trying my best to give you
awesome content
that will allow you to become a software
engineer also if you haven't joined the
private facebook group go ahead and join
because that way you can see what's up
you can connect with all the students
and if you have any questions just drop
them and i'm pretty sure one of us will
be able to help you this is all for now
and i'll catch you on the next one see
ya
[Music]