Kotlin - Apply, Let, Run, With, Also - Higher Order Standard Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello welcome to resale coder in this tutorial we are gonna take a look at a bunch of standard library cuffin functions like run with let and apply they are higher-order functions which means that they actually execute a function themselves and the function which they execute is supplied to them by us some of these functions are actually extension functions but now without any further ado let's get right to it first up we need to have some kind of a class to demonstrate these functions on so we're gonna create a data class it will be called person and it's going to have three properties and all of them are going to be mutable so var name it's gonna be of type string then also var age will be of type int and finally it will have var job it will be of type string it's not gonna inherit from anything and it's going to have one function print person and all that this function is gonna do is going to print line and it's going to print this person convert it to a string or we can actually write it like this that to string so that it's more clear if you don't know what the data class is it's basically a classic class no pun intended it's just that it already has the two string and hashcode implemented you can also copy a data class so that is pretty nice and you can pick and choose which properties are going to be copied and which not so that's pretty sweet and that's about it then we are gonna create one extension function on a string class so string and is going to be print line and this extension function will just print line of this string alright now let's go to main function and we are gonna create two people so while first person it's going to be a person and its name will be John and he will be twenty years old and he will be a programmer and then second person it's gonna be again person its name or his name will be David and he will be 30 years old and he will be a bus driver now let's suppose that you want to print the person which is older you could normally do it like this while older person it's equal to F first person that age is greater or actually equal to second person that age and then we want to return from this if statement first person are wise so else second person is older and now we would write older person that print person and that's how you would do it without these higher-order functions if you want to do the same thing but without creating a variable you would use run function and inside this function block you would put this precisely the same if statement all right and paste it in here and now you would print the person which this run returns if we check what's written in the documentation about this run function it says that it caused the specified block and returns its result and it does precisely that it executes all of the code between its curly braces and then whatever is returned so first person or second person and actually we can see that it returns this run because there is this signed it this is returned from run and the second person is also returned from run so that is returned and we can then use it to print the older person then we have a width function as you can see here you need to specify a receiver of any type the receiver in this case will be our first person this width function is useful when you want to modify an object and return what you want like in the run function above you can specify what you want to return so let's modify this first person we can write this that age because this is a receiver and just like in any normal class you don't need to write this almost never so you can omit this keyword even in the width function and we can plus equal 1/2 this H and then we can return age is now and interpolate age and then we can print line whatever that was returned from this width function here we are using the extension function which we created for our strings when we actually run the code now we can see that the run and also the classical way of getting the older person prints out the same thing so dave is older so it prints out Dave and then this width it returns H is now 21 because we increment a VH by 1 then similar to the width function is also a run function but unlike this run above this is an extension function on any kind of class so we can write first person that run and now this first person is automatically the receiver for this function so this run has actually a more concise syntax of width and also if 1st person was knowable you can do a check here so no save access operator and you would be good to go so I would advise that you use run instead of with almost always like I don't even use with in my code because I didn't have any reason to use it I always use run so again we can put the same kind of code inside here like in width and then we can print line and it's going to do the same thing actually and then we have let unlike width and run let us passed an argument and not a receiver and a receiver is this so we can actually emit this but remember that we can always write this before H and it's going to be just fine but we can omit this so why would we write that and let has an argument and not this receiver so first person that let and as you can see it right here that it is person not this is person and arguments can be renamed so we can rename it to modified person and then use it inside this function so modified person that H again plus equals one then we want to return ages now and interpolate and because we are gonna use an access operator inside it inside the interpolation we need to put two curly braces after the dollar sign and inside that we can write modify person the age and then we can also print line and when we run this after the width executes the H is to anyone after run executes the H is 22 and after let execute the H is 23 now we are coming to my beloved higher-order function and that is apply it's again a generic extension function so we can write second person this time apply and apply is my most used standard library higher-order function it has a receiver this and it returns also this which is the modified object so unlike with run and let you cannot return some string or some in from here it always returns the same object on which you call this apply you have no other choice if we look at the documentation so ctrl + Q it writes here that it calls the specified function bag with this value as its receiver and returns also this value apply is used when you want to initialize an object and you cannot put all of the arguments in the constructor because for example the constructor doesn't accept all of the arguments which you want to supply it in this case we can write age plus equals 1 and we will change the second person's job to be hard dark cellar and then we can print the person basically apply is a replacement for writing second person that age plus equals 1 and then again second person that job equals hot dog seller right blah blah blah we have a duplication of code here we are writing two times second person but we can use apply and right second person only once so that is pretty nice on a sidenote in Android with ankle library there's also apply recursively which can modify all of the members of a view group so if you have a linear layout I don't have it here I am using plain Kotlin here but let's suppose that we have a linear layout if you are not an Android developer you can freely ignore this stuff and it has a bunch of let's say text views inside it and we can apply recursively on this linear layout and inside this we are going to be accessing all of the individual views inside the linear layout and not the linear layout itself and here we could check if this is textview and then set its text size to be some kind of like SV 50 SP right if you are using anko and if you want to learn how to use anko you can check out my tutorial by clicking on the cart in the corner and also in android this wouldn't be an error it would actually work if you have anko installed and sometimes you want to use something like apply but you want to have a parameter which you can rename for example instead of a receiver for that there is also a function and it's called also so as you can see here we are getting a person as an implicit eight parameter and we can write it dot age plus equals one and eight dot job is equal to youtuber and then we can also print person and we could obviously rename it to be some kind of modified person like up above inlet and now when we run this code you can see that both apply and also change the Dave person successfully and that's it for this tutorial if you don't want to miss more tutorials like this subscribe to this channel and also hit the belly button so that you are gonna be notified about all of my new videos I'm also doing android tutorials and all of that good stuff if this tutorial helped you with understanding the higher-order standard library functions give this video a like and also share it to get the code from this tutorial click on the link in the video description which is gonna take you to resew coder comm if you have any questions on this topic or on something else if you want to suggest something if you want to suggest some of my future videos leave a comment follow me on social media and see you in the next video [Music]
Info
Channel: Reso Coder
Views: 37,883
Rating: 4.9207921 out of 5
Keywords: resocoder, tutorial, programming, code, programming tutorial, kotlin, apply, also, let, run, with, higher order functions, kotlin tutorial, kotlin higher order functions, kotlin higher order functions examples, kotlin standard library
Id: -Lha9n8VJf0
Channel Id: undefined
Length: 11min 46sec (706 seconds)
Published: Fri Jun 01 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.