Placeholder Image

字幕表 動画を再生する

  • What's going on, guys, welcome to a medical school.

  • My name is Nelson.

  • And in this course, I'm gonna teach you everything you need to know about functional programming with job.

  • So when I first saw functional programming, I was like, you know, what is this?

  • I saw functions by function, credit cards being used all over the place.

  • And to be honest, I only realized the power of it when I learned about the clarity programming.

  • So you see, most of us write imperative programming, which means that you define every single implementation detail off your code.

  • Whereas the clarity 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 howto increment it and how to stop it.

  • Right?

  • So with the clarity of programming, you have none of that.

  • You simply tell you what you want.

  • And that's why I'm gonna teach you in this course.

  • So in this course I'm a teacher.

  • Exactly.

  • You know what the difference is?

  • What?

  • The differences are between imperative and clarity, programming and why you want to move away from imperative programming.

  • Then I'm gonna teach you about the functional interface is that Java has to provide so functions by functions predicated consumer suppliers and all that good stuff, right?

  • So once you learned that, you can then start to use streams and optionals to write code in a clarity of way.

  • And then towards the end, I'm gonna 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 simply defined all over your validations in the 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 gonna teach you exactly about Lambda is callbacks and the property's off functional programming so that you fully grasp exactly the whole concept.

  • So if you haven't subsided my YouTube channel, go ahead and subscribe, because that way I can provide more awesome content like this without further ado.

  • Let's go here, then everything you need to know about functional programming with Java.

  • All right, so before you start this course, I wanna make sure that you really learned from this course because I wanna 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, go ahead and engage with the 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 land about functional programming.

  • All right, let's go ahead and both drop our application where I'm gonna show you all the examples throughout this course.

  • So I've got intelligent right here.

  • And if you've never used intelligent, then I highly suggest you to enroll.

  • Tow my free course where I teach you permit how to use this Awesome I d.

  • E.

  • And to be honest, as a software engineer, this is probably the idee over your choice when it comes to Java back and development.

  • So let me go back to intelligence, and the first thing I'm gonna do is to create project and right here I'm going to pick Maven now.

  • The project s decayed.

  • It doesn't really matter whether you use Java 11 12 or any other version as on this is above 1.7.

  • So I'm gonna stick Weighed 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 I d.

  • I'm gonna say calm, don't and then and me goes and in code.

  • So right here.

  • This could be your own organization.

  • And right here, let me simply name this as Java and then funk, you know, an animal press next.

  • And then I'm gonna save this on the code and then java functional, and then simply press finish.

  • There we go.

  • Let me actually collapse this maven tab right here and also limit collapse this and right here, go ahead of simply enable or import to import dependencies.

  • And this is the actual palm foul, right?

  • So if I open the functional Oh, actually, Java functional folder inside, You see that we have on Lee Deep Palm foul and this contains the group I d alter fact I d and then the actual version.

  • So let me just move that line and also removed this pop up.

  • And finally, I'm gonna put this full screen, and this is all on.

  • Actually, let me just close this because we're not gonna need this poem foul.

  • And this is pretty much everything you need in order to both trapped the application to follow along.

  • So obviously, I'm gonna give you the source code at the end off this course, But I highly suggest it to practice as I teach, because that's the best way.

  • Aww.

  • You learning any new topic?

  • This is often Now if you have any questions about intelligent.

  • As I said, go ahead and roll to my course right here where I teach you everything you should be aware off this awesome i d.

  • Let me go ahead and give you an example off Imperative programming.

  • So imperative programming involves you as a developer.

  • The finding every single detail about your implementation.

  • So right here inside offthe e source folder inside, off main and in Java.

  • Let's go ahead and create new package so you can right click on this folder or, um, I used keyboard shortcuts and also you can see down here.

  • So right here.

  • You can see all the keyboard shortcuts that I'm gonna use throughout this course.

  • So right here, I'm gonna use on the er someone select package.

  • And then right here, my name decides Impair that if and then Okay, now inside.

  • Go ahead and create Kloss and this will be called as Maine.

  • Now, let me collapse this and inside I'm gonna type P s and then V m for public static.

  • Maine.

  • There we go.

  • And now we have a main effort that we can run.

  • So let's go ahead and define a list.

  • And the time off this list will be off type and in person.

  • So right here, let me go and complete name decides people equals two and enlist and then dot and then off.

  • So right here we're gonna do a simply create feel.

  • People were actually a few people off time person.

  • So right here, let's go ahead and simply create a class.

  • So let's go intimacy, static and in class.

  • And then person and this person will have a name so private and then final string and in name.

  • And we also will define a gender.

  • So Denham and this will be engender.

  • And the gender will be male or female.

  • And then right here, what we're gonna do Oh, actually underneath basically say private final and then gender.

  • And that will be the gender.

  • And let me ideas took were struck there and there we go.

  • So now we have this class right here called person, and let me also define into extreme effort right here, so two and a string.

  • And there you go.

  • So now, right here, we can simply say list off and then new and then a person.

  • And right here, let's give it a name.

  • So John and John is a male.

  • And let me have they started in port for this.

  • And let's go ahead and duplicate this.

  • So I'm gonna press Come on d A couple of times.

  • And Eric Oh, so let's have a Maria.

  • And she's a female list of pay.

  • Isha, she's also a female.

  • Just like that.

  • This have Alex the male, and this finally have Alice just like that.

  • And Alice, She's off course a female.

  • So now that we have this less right here, let's go ahead and find out how many females that we have.

  • So the way that we will do it with Ah, the imperative approach is let me have a common right here.

  • So in an imperative approach is simply by doing the falling.

  • So first we need an empty list and then off time and in person.

  • And then let me simply say females right here equals to list off.

  • And then I actually know even least off Vesco Agency knew in an array list.

  • So this is empty list.

  • Now.

  • What we need to do is to loop through people.

  • So let's go ahead and you'll be safe.

  • Four and in person and in person, inside off people.

  • And right here we're going to say, if an in person actually let me go innocent was, say female and then dot equals two person dot and engender just like that.

  • So if this is the case, I'm going to say females don't and an ad and then a person just like that, and that would send my column and then right here at the very end.

  • What I'm gonna do it simply print each people.

  • So again, let me simply copy this just like that.

  • And then right here this instead.

  • Off people, this will be females, females, and this will also be female and then south, just like died.

  • So system got out the print line Hopes 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 on Lee females in our output.

  • And basically, this is pretty much what imperative programming.

  • It's so basically we're defining every single detail.

  • So right here you can see that first we have an entire list and then we loop through that and then we have an IV statement and finally we have a final four loop looping through females and you can see that there was a lot of logic not, you know, you know, a substantial amount of logic.

  • But 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 D clarity of approach and you will see that it's much neater.

  • And we put 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, a functional style programming, we simply have to do the falling.

  • Let me collapse this So right here.

  • So what going to do here is instead of us creating a list and then having to fold up So what I'm gonna do is the following first, let me go ahead and premature.

  • Have a comment, and right here this will be declare authorities in an approach.

  • Now, the way that we used the clarity of approach is by using streams, we'd collections.

  • So don't about, um so much about what stream is because I'm gonna show you exactly they're on.

  • But for now, just follow along.

  • So call ahead and simply say people.

  • So people is the original list right here.

  • So this list off 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 was simply telling 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 I want to say the exact same thing right here.

  • So I'm gonna take this eighth statement lips.

  • Let me press command w a couple of times come and see and then put it here.

  • Very cool.

  • So you see that I'm filtering and then this takes a person that I'm using This the same if statements, basically female dog equals and then personal gender, right?

  • And finally, I'm going to say don't and then collect and then collectors and into list and the week semi column.

  • And right here, what I'm gonna do is put this on a new line in the same we'd collect just like that.

  • And right here, we conceivably 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 gonna print this just like that.

  • There we go.

  • And let me do the same here.

  • Someone copy that.

  • And in priest that Take this comment, put it here so you can see exactly in the council 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 adding to that list and then doing the if statement so on and so forth, you could see that is very straightforward.

  • And I'm just telling you what I want.

  • So from the people array list or a list, basically, this is a list I'm gonna stream.

  • So I'm gonna go into this abstract mode, Then I'm going to filter all the females, and then I'm gonna collect to a list.

  • And finally, I'm gonna print everything.

  • So technically, I don't even need this collector list because I'm simply printing our things.

  • So let me simply run this for now.

  • Secrecy exactly was happening.

  • There you go.

  • Basically, you could 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 D clarity approach is much Nita.

  • So obviously, if you were to have this into a less right here, So this is where collect comes into play and then I can remove this line and this with semi column and an extra interest to a variable and then say females and then to write.

  • And you can see that Now I've got a list right here, and I could again simply say females tue dot and then for rich system, don't out calling 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 off the differences between the Clarity's and imperative approach.

  • So obviously, I've just shown you, you know, the difference between both.

  • But there are few things that we need to understand in order to write code like this, right?

  • Someone teach you about functions by functions, pretty goods by predators, 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 Germany message otherwise, let's move on.

  • All right, let's go ahead in the section learn about the Java thought you till that function package.

  • So basically, this package contains functional interface is that provides target times for lambda expressions and method references.

  • So each functional interface as a single abstract method.

  • Basically, you could have one interface with multiple default methods.

  • But you must have on Lee one single abstract method to be considered as a functional into face.

  • So basically, you can see some examples right here.

  • So the assignment, the assignment context.

  • So this is how you, for example, to find a critic it and this is all, for example, you use it within the actual, 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 off functional interface is right here, you see, by consumer by function, binary operator by predicated bully um, supplier consumer and the list goes on so they're pretty much identical about from the type.

  • So the ones that you will most likely be using our functions bye functions Paretti kids by predators and consumers and also there is a by consumer, but I'm But I'm gonna explain exactly how everything fits together, because once you learn those main ones, then the rest will be very simple.

  • So right here only show you that.

  • So we have a predicated.

  • So where is the pretty good?

  • So let me find credited.

  • So right here is a pretty good Let me quickly show you a credit it and surely that we actually have used, you know, predicated before.

  • So a predator represents a pretty good basically.

  • This pretty represents a predicted bullion value function off one argument.

  • So let me go back to intelligence and Ray here.

  • Remember where we had this declare it eve 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 predicated.

  • And look at this.

  • This predicated looks like this.

  • So the pregnant accepts a person and then returns true or false, based off that person I eat whether the person ease off type female, right?

  • But what we've done right here, we're simply in lined like that.

  • That's why you don't see the president keyword.

  • But indeed, this is a predicated and a pretty it looks like this.

  • So don't worry about it.

  • This if it doesn't make sense.

  • But I'm gonna try my best to explain you exactly how you go from, you know, normal Java functions to the functional interface world.

  • Join me in the next one or a so I've gone ahead and created a new package right here called Functional Interface.

  • And inside off this package, I'm gonna go ahead and great a class.

  • And first I'm gonna name this class as underscore and then function.

  • And I'm gonna explain why I'm I'm saying underscore right here.

  • But for now go everything we say and the score in front and the score function.

  • All e Kaname, this anything order, then function without underscored.

  • Right.

  • So right, Here's what I wanna do is he will say public static void main have a main method.

  • And right here what I'm gonna do, it's simply do the falling.

  • So let's go ahead and clear function so static and then function.

  • So this function will take a number, right?

  • So take a number and an increment done number by one.

  • So let's go ahead and simply say and and then in crypt meant 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 Sam a column And if I permit extract is too available And for now, let's go ahead and simply south this increment and let me put this full screen.

  • Let me run this now Hopes I'm running the wrong one.

  • So I need to run this function.

  • There he goes.

  • We could 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 then about the function, um, interface.

  • So where is the function?

  • So this one right here.

  • So this says that represents a function that takes the accepts one argument and then produces a result.

  • So this is pretty much what we have here rights of 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 up.

  • Oh, 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 sauna showing something so function, you can see that this function takes a T right so t and then are so basically these are the data types off the input and the output.

  • So let's go ahead right here and simply say that this will be off type in teacher.

  • So the input is integer and the output is also an integer right here.

  • So now let me go ahead.

  • And then we say increments and then function.

  • Watch the 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 possible Lambda.

  • And then I'm gonna say number and then plus Plus, there we go.

  • And that which semi column So you can see that this and this are the exact same thing.

  • So basically, this interview right here corresponds to the actual input.

  • So this one right here, right?

  • This stereotype, 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 watch.

  • Let me simply make them the same.

  • So number plus one.

  • And let me also rename Dysfunction to increment by and then one so you can see that this is pretty much the same.

  • But now we're talking in functional style world.

  • So now the way that we use this is as follows.

  • So go ahead and simply say increments by one.

  • And the reason why we not seen this dysfunction is because we can actually put it inside off this method.

  • So inside of this main method, we can pace 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 gonna do it simply say dot and then apply.

  • So the apply for much takes the actual argument.

  • So let me go ahead.

  • You say one and then right here I'm gonna say increment.

  • And in two and I can change the state of time to integer and then simply say south and then increment, too.

  • And then let me simply run this again.

  • You can see that we have the exact same output.

  • So this is how we 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 that?

  • You know this function right here, Eastern off a normal regular function, right?

  • And the worry.

  • By the end of this course, everything will be clarified.

  • So there are a few methods that only a teacher So right here, if I permit, say increment function.

  • Oh, actually, inquiry and buy one function.

  • Don't you can see that we have, um, apply.

  • And then this memory here on dhe, then So only teach you what this method is, because you might use it later on.

  • So let me simply move that, and this is often Now join me in the next video CIA.

  • All right, let's say that you want to combined multiple functions together to achieve a desire state or a desire apple.

  • 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 know.

  • 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 gonna say multiply Bye.

  • And in 10 right here.

  • So this will take the number hoops number, and then this will be a lambda just like that, and a number times and then 10 just like that.

  • So we have this function right here, and what we want to do is after incremental thing.

  • So after incremental thing by one right here, we want to apply that or the function immediately.

  • So you could do something like this.

  • So you could say in't increment Oh, actually multiple apply and then equals two and then multiply by 10.

  • Uh, actually, we named this to function just like that.

  • And then right here, you could say equals two.

  • Multiply by 10 function, don't apply and then increment and then to write.

  • So this is not quite right, because this is the name so right there and then removed that and they should be multiply by 10 Me.

  • Grab that name there, basted there.

  • And there we go.

  • So now I'm afraid to assault and then multiply by 10 function, wifely.

  • 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 could see that we take to multiply 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 combined the two functions together.

  • So right here What I'm gonna do is simply say increment by one function.

  • Don't and then and then multiply by 10 function And right here What I'm gonna do this, simply extract this toe a variable.

  • So this will give us yet another function.

  • And let me simply say ad buy one and then multiply by 10.

  • There we go.

  • So if I put this on a new line and you can see exactly what he's doing So now let's go ahead and simply say ad by one and multiply by 10 and in simple say apply.

  • And you can see that you can keep on, you know, chaining thes functions as you go alone.

  • But for now, let's simply chain to functions.

  • Sanoma say apply.

  • And the number that I want is one right.

  • So this should give us the exact same result.

  • So one plus 12 to place 10.

  • You guessed it right, there will be 20.

  • So obviously I need to Ah, put this around system throughout the print line.

  • Just like that.

  • Run this again.

  • You could see that we have 20 right here.

  • So if I change this to let's change this number right here.

  • So I said off one.

  • Go in and change it to four.

  • Four plus 15 times 10.

  • 50 Right here.

  • And this is too much how you change functions together.

  • So if you have any questions, go ahead and drop me a message.

  • Next, let me go ahead and teach you about bye functions or right?

  • So what by function, is is the same as a function, but instead off taking one input, it takes two inputs and produces one output and by function for to thinks right.

  • So right here we have this function right here.

  • So this one ad buy one and multiply by 10.

  • So let's say that the first number that would give it is the one that issued increments.

  • And then the 2nd 1 is the one that should multiply.

  • Bye.

  • 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 watch the increment by one and mole to play just like that.

  • So now I hear we have a second argument so int numb to multiply by just like that.

  • And now I'm gonna wrap everything with then parenthesis and in ST times, and then them 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 seem to say bye and in 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 2nd 1 is also an integer.

  • The 3rd 1 is also an Inca.

  • So bear in mind that you can have different data times, depending off 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 by 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 with din parenthesis.

  • So this will be number two increment by one.

  • And then number two mo t fly and then bye.

  • And this will be now a lambda.

  • And what we do, it's simply say number two increment by one plus and in one, right.

  • So this is the exact same thing that we did here and then this will be within prevent is's And then finally, times and a number to multiply by.

  • And that will send Michael.

  • Um, and let me just put this on a new line yet again.

  • So you see exactly everything.

  • So I've got bigger fund because only to see exactly properly.

  • But, you know, you shouldn't invent 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 one and then multiply by 10.

  • So let's go ahead and say this now is a buy function.

  • And this was the function example.

  • So let me.

  • Actually, either common So takes one argument and produces one result.

  • I said No D By function, it's almost the same, but it takes two arguments right here.

  • So let's go ahead and use our function So right here.

  • So increments by one and and multiply.

  • Let's go ahead and give it for and then let's multiply four by 100.

  • So basically this will be four plus 155 times, 105 100.

  • So let's go ahead and simply Ah, 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 win this you 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 daughter, pit mine and right here let me simply used the actual function.

  • I think we have to have your static.

  • So this guy right here.

  • So this has to be static.

  • There we go.

  • Now we should appear by function, and this should be dot and then apply and apply.

  • Takes four and 100.

  • Now, if I run this, you see that we have the exact same result.

  • So this one right here is using normal travel function, and this one right here is using the by function.

  • Let me go ahead and show you the actual dogs.

  • You'll see that we have Lord's off by.

  • So let me say such for by you could see that we have, you know, double by function to end by function too 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 the same week predicated that 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 the message.

  • Otherwise, let's move on.

  • All right.

  • Next, let's go ahead and about Consumer.

  • So a consumer.

  • So any search for consumer right here.

  • So consumer, so a consumer represents an operation that accepts a single input argument, returns no result.

  • So let me show you exactly first non functional way and end the functional way.

  • So let's say that right here again, let's have public static.

  • Avoid main and right here let's go ahead and define a static class and in 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 time behind that to construct their and now what I'm gonna 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, you know, and in customer and then don't customer name.

  • And then right here, Plus and Kama.

  • Thanks for registering and in phone number.

  • And then plus customer don't in a customer phone number.

  • So let me actually put this on in your line so you can see everything.

  • So now let's go ahead and simply create new customers.

  • So, new customer, this will be Say that Maria.

  • And then my real phone number will be 99999 Right?

  • So let me go ahead and cut this and then simply saying greet a customer passed new customer just like that.

  • And which semicolon just like that.

  • So now if I run this class, you can see that we have a low Maria.

  • Thanks for registering phone number 9999 So the way that we achieved the same but using the functional interface.

  • So the consumer functional interface is by doing the following.

  • So let me go ahead and seem to say static and then consumer.

  • So consumer takes one argument, so this will be off type of customer.

  • And then right here, let me go, Edison, to say, greet customer consumer equals two.

  • And then we'll take the customer.

  • And what We're gonna do it so much?

  • The exact same thing right here.

  • So they meet travel office and in place it there.

  • And basically, now I can go ahead and simply saying greet customer consumer don't.

  • And then right here instead of apply, we simply accept No, let me go in and extract this customer.

  • So, Maria and then also possum Marie, Right here.

  • So there we go.

  • So this is using functional interface.

  • So this is actually consumer and then normal java function.

  • Great.

  • So if I run this because either 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 off the input.

  • So is this one.

  • This customer is the actual name off the importance of this customer right here.

  • And avoid you could technically say that void is a consumer.

  • So if you have any questions on this, go ahead.

  • Enjoy me message.

  • Otherwise, let's learn about bikers humors.

  • All right, so let's go ahead and learn about the by consumer.

  • So the same way that we've then with functions and by functions, we have the same four consumer.

  • So pretty much every one of these functional interface is they have the buy version, so in front of A, they have the by 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.

  • It's interstate feed to, for example, So this will be the version, too.

  • So we're gonna have is a by consumer right here and let me important this.

  • So first we will take the customer, and then we will have the bullion so right here.

  • So this pulling so this bullion will represent whether we should hide the customer's phone number, show and then phone and then number.

  • So now we haven't air because we have two arguments and because we have two arguments we need to enclose these with Dean.

  • Parenthesis is just like that.

  • Now, right here.

  • What I'm gonna do is instead off printing the actual phone number like that.

  • I'm going to say the falling so I'm gonna put this with in parenthesis is just like that.

  • And right here, I'm going to say show phone number.

  • So if chauffeur number, then we can pre much print that.

  • Otherwise, I'm simply going to have some stars just like that.

  • So now let's go ahead and testes out.

  • So let's go ahead and simply say, great customer V two Endless past, Maria And then right here, listen to say true for now.

  • So this is actually complaining because we need to say dot except And if I win this, you can see that this is the version two off our function.

  • Now, let's go ahead and say false when that If you see that now we're hiding the number right here.

  • So basically, if you were to convert this into a normal regular job of function, this would be us follow.

  • So let me just duplicate all of this just like that.

  • Call this V two and then right here we simply take the exact same variable.

  • So show phone number.

  • But right here, bullion, show phone number and let me grab everything from here and then put it here.

  • There we go.

  • And that would send my column.

  • And I can simply now go ahead and say, Get oh actually great customer V two past Maria and into run.

  • There we go and you can see that we have the exact same information.

  • So there you go.

  • This is how you use a by consumer.

  • If you have any questions, go ahead and drop me a message or the wise Let's move one.

  • All right, let's go head on about predicates So I'm gonna head inside of the functional interface package created this underscore predicated class and inside.

  • Let's go ahead and simply type public static.

  • Avoid main and let's go ahead and then about Pathetic.

  • It's 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 return a bullion, and here, let's simply go ahead and say is phone number violet.

  • 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 no riel, but it's just to illustrate the actual example and purpose off.

  • Pathetic.

  • It's so right here.

  • Let's simply say that this note this phone number.

  • In order to be valid, it has to meet the following crack.

  • Curious.

  • So let's return phone number don't and then starts with.

  • And let's say that the phone number has to start wheat 07 And let's also say that the phone number and phone number don't and in length is equal to 11.

  • Okay, so this is a simple phone validation method, but this is not really is 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 07 and then 000000000 and let's have another one zero and then nine.

  • And then we can had, um, you know, a couple of digits, like so right.

  • So the validation is not riel, 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 printed number.

  • But what I meant to do was to actually let me cut this.

  • So what I'm into print was is for number valid and in past the actual phone number, So same without one.

  • Cut it, Ace Farnum a valid and then pacing run again.

  • You could see that the 1st 4 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 could see that the 1st 1 is valid.

  • The second, the second.

  • The 2nd 1 isn't and the 3rd 1 isn't as well.

  • So right here was simply doing a simple phone validation.

  • A check.

  • Now, in order for us to use the functional style, we can use the pathetic it so predicated right here you can see represents a bull invited function off One argument, right?

  • So right here The way that we use a predicate is as follows.

  • So this exact same function as political will be something like this static.

  • And then right here, critic it 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 listen, please say critic it, and right here this will be equal to and then phone number filled.

  • This actually grab this and then this lambda.

  • And on any line, I'm simply going to grab this and paste it and an end with semi colon.

  • Now let's go ahead and use it.

  • So to use it is phone number, valid predicate don't 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.

  • 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 couple.

  • There we go and then placed it in.

  • Let me do south without critic it.

  • And then right here, wait predicated.

  • And if I run this, you can see that we have the exact same results.

  • So true, false, false and without predicated, true, false, false.

  • So basically, this is the equivalent.

  • So the predator is D equivalent as this normal Java function, and the coating about predicates is that you can have a bunch of prick.

  • It's and then combine them together.

  • So the same way that we've been with function using and then we can we can also use it.

  • Wait predicates.

  • So let's say that we have another prick it.

  • So let's have another product so static and then predict it and this product it will take a shrink 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 going to say phone number Don't and in contains and then three just like that.

  • Now, the way that you chain predicates is as follow.

  • So right here, let's simply go ahead and say is phone number of 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 his phone number, valid Prickett don't and then and another product it So the other product it is contains number three don't end in test this actually pass this number right here.

  • So the last one There we go and let's wrap everything without a system.

  • Dr.

  • Bright Line.

  • Let me put this on any line.

  • And just like this, There we go.

  • So if I say right here is a 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 1st 1 So right here and let's actually pulled 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 before number is wanted.

  • So it starts with 07 11 characters long and contains number three at the end.

  • So let's go ahead and run this method, and there we go so you could see that the 2nd 1 is true.

  • I've explained why, and the 1st 1 is false.

  • Now you can use ants, but you can also use or so or means either one of them is true.

  • So if I was to say right here, so they set off and I can use or right.

  • And if I remove three here and put a zero run this, this will still be true.

  • It's true because we only need one condition to be true.

  • If you use an answer, if you chain multiple, pray kids 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're the same thing.

  • But instead of taking one argument, they take two arguments.

  • So I'm not gonna give you a full example on by predicates because you should be familiar now with by predators, by functions by consumers.

  • Onus off off.

  • So if you have any questions on predicates, go ahead and drop me 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 CIA.

  • All right, go ahead and inside of the functional interface package, create class called and the score 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 dogs right here, you can see that they say supplier T represents a supplier off results.

  • So basically, sometimes you might have a method right here that simply let's say that we have a method like that.

  • So a method like this so static and then get watch Lee String, get connection.

  • You are well, quite listen to get d B connection.

  • Your oh, right.

  • So this will take no arguments and I simply return.

  • And then, for example, J.

  • D.

  • P c Colon, Foresight, sports slash local host and then 5432 foot slash Let's say users, right?

  • So this is not really but you get the idea.

  • So the way that we can, um, uses function now is if I simply say south and then get D B connection or well, and anywhere in this you can see that we get the actual connection euro for some kind of database.

  • So the purpose off this function right here, it's simply to return a value right here.

  • So this is the volatile returns, so we can do so we can use a supplier to represent the exact same thing.

  • So I'm gonna cooperate this euro.

  • And to have this gentle function as functional, we simply have to say supplier actually study and then supplier.

  • And in this will supply ish trained and then get that's ground this and then supplier and right here equals two.

  • And then what we need to do is in behalf Prentice's nn was simply pass.

  • Oh, actually returned this just like that.

  • So if I put this on in your 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 say it was a system the out up in line so south and right here to use this, I can say get D B connection your oil supplier don't and then get so now if I run this, I don't 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 won, right?

  • So this data type doesn't have to be a string.

  • It could be, you know, your custom classes you cut some objects?

  • Um, you know, Honore 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 off type and then string right here.

  • And then here I would say list, don't off.

  • And then just like that and I would say my column.

  • Right.

  • So now if I rent this, you 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.

  • Hopes just like that.

  • And right here listening to say, Ah, system, for example.

  • Right.

  • So get a connection, your rails and then supplier.

  • And then here, you or else run this and there goes.

  • So now we are returning a list with two euros.

  • 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 job function and functional interface is so the benefit off knowing the functional interface is is when you start to use optionals and streams of more specific streams.

  • Right.

  • So once you start using streams with all of these functional interface is you will start to write clean code and that's wanna teach you next.

  • Okey dokey.

  • Now that you've learned about consumers functions by functions pretty good bye predicates and so pliers and also I think, by consumers.

  • Now, let's go ahead and actually learn how this is useful to us went writing code.

  • So go ahead and create a new package on the Java so package and call this as streams.

  • Now, go ahead and create a new class and simply call it on this court and then

What's going on, guys, welcome to a medical school.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

A2 初級

Functional Programming in Java - Full Course

  • 9 2
    林宜悉 に公開 2020 年 03 月 28 日
動画の中の単語