Kotlin Scope Functions - let,apply,also,run,with - Higher order function

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome back to my channel today our topic is scope functions in Catalan I will discuss about a bunch of cotton scope functions which are also part of Kotlin standard library and those are late Ron also apply and width in this video I will demonstrate the differences between those available scope functions and also will discuss relevant use cases in real life before starting I would like to mention one thing once a great Android developer said whatever you do with scope functions can be done without this means using a scope function is not mandatory but having a good knowledge on scope functions will definitely give you the aids courtliness core functions predominantly increase readability and make your code more concise alright let's start coding first we will go to the android studio ide although we are not doing any android expect here you can use any kind of Kotlin IDE like IntelliJ or Visual Studio code is also fine but I will show the demonstrate this example in Android studio so I go to the start new Android studio project and select an empty activity and name it like scope functions and please see it okay place it here and finish going back to our main activity and here we declare a variable which is inaudible type string a double tap and we said that value as Android and here I have created a function as latest scope function - and we call this function from our uncreate actually it is not necessary so - calling a function to demonstrate that late or other scope functions but as we are going to demonstrate all those school functions in our in a single activity or single program so better to separate them with some functions well our first first scope function we are going to discuss today is late well the latest core function so before before the you know straight how does delay how the late work we will show an example first without using the latest scope function and what is the difference or what advantages does late function gives us will show that ok consider and for situation that we want to say we want to be in the length of this stream if it is not null and if it is null it will simply print a 0 so how can we do that without using this the latest Co function we can simply declare a variable variable name it length and it will be a empty string and we can write length if if condition if normal stream not equals null it will be the end length will be this length will be normal string as and as it is it was not nullable Nolan Ella but so it will be dog named or twisting sorry twisting and else it will be simply print 0 and finally we dreamed Ln we will print the well in the length value so it will be without lettuce cofunction and fire will bring the value of length so if we run this app we can see from our lock that if I can if I put a filter here scope function and we can see the result the log is showing that it is 7 and this is the length of this string let's see how we can do the same thing is that the same thing using late scoff unction so we take the normal string here so stop it and normal string and ok first we okay let me it create a variable fast and the normal string and it is not able so dot we write the late scope function okay so we are operating there is this latest function on normal string on a string and this is called the receiver a receiver is nothing but a and it could be a variable a object or instance of for class so lady scope so late school function or any other ISM underscore function operates on a receiver and most of the scope functions have receiver so and we can see that is so a lambda function is created and it is the eat function if you seek if you can see that this it is type of string and actually the type of the receiver okay and here we can write here we can write simply eat dot went okay eat dot length and and that's it it will pin the pin print the length of the length of the string and for this Ilse statement we can use the LV separator here so it will be and if it will paint 0 and if we print this paint LM and copy the exact and it is not without it for later scope function and we will paint the value of x okay so it will be X and if we round this air our app now and from our locket we can see we can now see that the latest curve function is painting exactly the same output and you see how ridiculously less amount of code we need to write when we use the latest function or any other function basically the basic principle of our basic advantages of using the scope functions are it makes our code more idiomatic we first of all if we compare that the non-latex of function and its cofunction code we can see that we do not need to first create any if a statement or even create any variable here and it is easily readable and from here I would show you the structure of a lady scope function so lady scope function basically looks like this relate a later scope function has a receiver and in our case the normal string this is the receiver and receiver as I earlier said that receiver could be anything like object or any variable on which you are operating the echo function and late refers to the receiver or object to it so actually the late actually use the parameterised of fashion of our receiver or object whatever you say and changes the scope arrange returns the value of last statement please pay attention that late a scope function will return the last statement of this scope which is not very obvious from here it is repenting the e dot length the length of the stream we can do some a little experiment so I can write it can any iOS which is a string and what it would if I run this what it would return so if we run this and from the logcat we can see the iOS it is spending the iOS and this is the last statement and actually this is the structure of a latest cope function and as you know the the lab it returns only the last statement and although there is any implicit or any sorry any explicit return statement but almost all ESCO function returns something so this is the case and in real life I use late function basically with Android several types of views just like if I give you examples like or Y get a variable and the textview get a text view by though it takes to view is the test view you know our xml layout in the external node we are there is it's text view here so this is the text we referring this text view and we apply a late function and we thought we put some text I can and maybe it is Windows and yeah we want to make it uppercase so dot pre string dot to uppercase and finally we paid for a variable here so to be stopped it so okay and paint it and from this form a locket we can see this is showing windows in uppercase so this in this example we take text view as receiver and change this scope or a scope in their latest cope function and say delete later scope function and we get the output we get the last statement as a return statement but there's all about the latest cope function now we will discuss about the also a scope function so there is a function named also just minimize it and create a function also scope function and the function here okay so before doing anything related also scope function I would like to first show you the structure of also scope function which is very similar to late but there is a major difference this is the structure of that all swisco function and you see also also operates on your receiver and everything is same except the return statement there it returns the object or receiver or the receiver itself in late a scope function we see latest function returns only the last statement but also scope function returns the receiver itself so let me show the example okay a little bit expand here and in our last example what we did we will do the exact same thing inside our also scope function okay so copied and pasted it here and instead of using late we are using also and ladies cofunction we you know locket we write also and we run our app from our locket we see the all Cisco function is returning Android a extort app compat wheelchair something something and basically it is the textview object so this is returning that text view are okay the receiver itself so this is the thing the same exact code the latest code function is a returning a string that last value and the same code when we used or as also scope function we apply also scope function so it returns the same receiver here so that is the basic difference between late and scope late and also scope function okay a good thing about the also scope function is also you can use also as an extension function of your receiver what I say that means let me show you an example which I often time use in my real life coding okay let me let me create a scenario so as you so okay inside your also scope function just think there is a there is a list of string so first string list this example gonna take some time so you can skip it and go to the your next scope function of this video but I will recommend to show you this thing also because you can relate yourself with real-life project okay so it's dream list and it will be a mutable list of the list of the type of string and the couple of string and some of them are having length of one and some of having a hint of to sum of leg having the end of three four and others okay and we will apply a map function here it will be word and it will be string list thought we apply a map function and inside the map we take the length of each item so so what does the map - the map operate the map actually take operations on this string list and take the length and calculate the length of each item here and finally we apply a filter function here and we filtered out it and if and this result with the filter out all those elements who has length of length greater than 2 so don't bother too much if you don't know what is map or what is filter just thing that map first map will operate on this list and calculate each elements length and filter what filter will do filter will filter out all those elements who has who has lain together than to and make a list of them so just see if we show the list right now you can write outside or outside scope function and reservists there's at least okay we run it and from our look at we can see this is giving a output of three four five it is doing what it exactly supposed to do that it first created take the length of this first the net function take length of each element and the filter function have filtered out those element who has length greater than 2 so this is showing three four five this is this length is three is four and it is five which is easily understandable but what we want to let's come to the point what we want to do I want to get a log from exactly from here so that I can see that our nap function is working fine and if we first I will demonstrate how we can do this thing without using the also scope function and later I will show how we can do the exact thing using the high score function so let me show the code okay I will not write that code because it will be unnecessary lengthy the video will be unnecessary Lindy so I will just copy test it and here we haven't used any also scope function first we take a variable where we take the output of map and we this is our this Rob we want to print in our locket and as we need to we need to contact our result or on filter so we create another variable and we apply filter on our temperature list and we get the output so let me run this app and from a lock that we can see without also escrow function it is showing the length of those of our elements so it is working fine and I think this line is not necessary it has no use so a question is how we can achieve the same thing using all Cisco function it is very very interesting thing we can use all Cisco function as an extension function so let me show you okay let's just copy it here and paste it here and we can apply scope function also scope function here just here so also let me give show it in a new line okay more readable and I can say also scope function school function and it will be eat eat we are using it because you know this eat is referring to this sting list and this stream list is working as a as the receiver okay so so if we run this app okay first we add the locket and let me run this app I made a little mistake I have forgotten to use the print line command here because without println we cannot see anything and just cut it here and paste and clear the locket and run and from the locket we can see they are giving that's exactly same output and here we can see how it is more idiomatic the how the how also a scope function make our code very easy and easily understandable and very idiomatic and I personally use all Swiss cup function a lot in my program and hope you will also after watching this video you will also use also a scope function in your code without any problem and here comes my most used and most favorite scope function applied so apply function actually works as a similar it is very similar to the also but it has a quite a difference let me show the structure of applied so this is our apply it works on a receiver and refers to the receiver or object to this if you can remember our lake and also refers to refers our receiver through it and if I if you see the late-late also refers to the receiver as it but in our in the apply scope function and the receiver is refers as this actually what is this and what is it I will show a couple of minute later this is very basic principle what is it and what is this and this simply refers to the object itself but it actually works very similar to a parameters version of your functional or a argument okay I will discuss it later but let me first sohow apply works so and for this example I will create a class outside the main activity I will have copy I will copy pasted this class name is foo and it take string as constructor and inside it it has a member a member variable and it has a member function which will print the name and address and initially the address is unknown and let me create the function first so I apply function and inside the mainactivity I create the function and let me enjoy a very simple situation here let us create a creative object of this class and we want to change the address from this object how we can do it without that without using the apply scope function so here we can create over foo and it will be true and we give a name and we if you want to change the address so we can do it like who taught this address address and we give it a value so it will be and if we want to access the function here member function so it will be food in data and you can write without using apply school function okay now if we run our app from a locket we can see without using apply scope function the same the name and address L is Ireland okay so how we can achieve the same functionality using also apply scope function so we can use apply very easily just copy it here dot apply don't apply and apply refers to these these food this actually instance of direct instance of food so it will be you can yes you can write these thought these dot address but there is very cumbersome to write all them these days so you simply write address equals and simply print print data and run this and our former look at look at we can see these are those two statements are giving the same output same exact output but I think using applied after the using apply you have do not have to use the object name again and again it just did a Clea ccess all those member variables and member functions and another mostly used I will show you another example and this is the for the first time we will use the actual Android output so I have we have a text view here which is very basic and it is showing windows and there's very small text so we can use apply to manipulate the Android views and it is very easy and I use it most of the time to manipulate the take manipulated views on Android view systems so how you can use it just giving an example we have the text view dot apply by the way this text view is referring to the XML here text me okay this is the idea of text view so apply and change the text and text will be applied text and we can set a color say text color and it will be resources dot get color maybe we use the color accent so color it will be r dot color i think our thought color dot color accent and we will we can change the text size so text size we can do many more things okay so what we want to do we want to change the look and feel and the value of this textview okay so we're on this app and from the military we can see the value and color and the text size has been changed so this is the apply this is how apply scope function works so latex code function also scoped function and apply scope function and the mostly used scope functions considering my work real life work and there are two remaining scope functions those are round and wit and I almost never used run and with a scope function in my real life and I also do not I also will not give a brief description on run and width but I will go first but before starting the run and wit I would like to show you the differences between the DS and eight let's consider a class we can instantiate the class by creating a object of that class on the other hand we can create a reference of the class using the class variable as a function parameter these points to the instance of the class and each points to the reference of the class let's think the name of the class is car the car class has a member variable name we the initial value of wheel is full this car class can be initiated and the object could be changed the variable value from off will from 4 to 8 so when take the instance or object of the calculus with this keyword we get the change the value of will member variable in this particular case on contrary we can use the calculus as a reference to an object just like use a function parameter so when we use reference of a car plus as each keyword we always get a fresh object of car the value of member variable will always be 4 I think you have understand the basic differences between these and eight alright got back to our code and now I will show the uses of Ronnie's scope function and we discov function real quick so I give it a name and actually I do not see any good reason to use Ronnie scope function and with a scope function and for this I will make I will not go deeper with Ron and with I haven't to use run and with in my real life almost never so okay so Ronnie's cube function is very similar to lady scope function if you can remember the latest code function the Ronnie scope function related scope function is very similar both of them return to the return that last statement only the difference is late refer the refer refer the receiver as eat but run refer the of on receiver as these so this is only the difference so I create a variable will x equals and this is stream YouTube this is our receiver and when I apply run escrow function so when a user and scope function it is referencing as DS so what we can say this dot to uppercase and we bring this print again on a scope function and we put in the value okay so when we run this you you know what is the expected value it is it will show the YouTube in uppercase okay from the locket we can see Ronnie Scott function it is you know the value YouTube is showing the last disco function remains is weet weet has the similar feature as run like it also the width also returns the last statement of the scope but it has a width has two very distinct feature which is all those functions uses all those scope functions like whatever you say Ron or apply or also and late all are all performs like extension function as the receiver so this is the receiver and it just the sorry that scope functions like late also apply run acts like an ESCO ESCO function of the receiver but which is not we doesn't look like that with just which is a weekly scope function is just works as a regular function so it will be if I consider this example as width so it will be like so give it a name wheat wheat works as a regular function so we okay we create a variable first variable Y sweet and here is the receiver the receiver will be work as a function parameter so this is another properties [Music] ideal properties of wheat so with and and here we see the with worked as a regular function although it is a scope function and it takes receiver as a parameter all those other run or other escrow function takes the receiver as a extension they actually perform as a extension function on the receiver but the width function works as a regular function and takes the receiver as the parameter that's it and here we can write ad stored to uppercase and in the statement paint wine we're under from the locket we see it returns the YouTube the same thing has run just they we'd have we disco funk shown as a different structure and I have found an interesting thing in Internet this is a table this is a table that these are the scope function apply also later on with and how the object is different as these all apply differences these also references eat late eat run this and with this and what it returns the object object and late returns the last estate pendant I have discussed all those things and these big these actually calls the end of this tutorial I think you have enjoyed this I have tried to explain all those courtliness Co functions in this tutorial and if you liked the video smash the like button and subscribe if you not subscribed yet and stay home stay safe during the pandemic of in nineteen pandemic and see you in the next video till then good night
Info
Channel: Munir Hoque
Views: 1,294
Rating: 5 out of 5
Keywords: Kotlin Scope Functions, Kotlin Higher oreder function, let in Kotlin android, also in Kotlin android, apply in Kotlin android, Kotlin let apply also run with, let, apply, also, run, with, Kotlin Scope Functions example, Kotlin Scope Functions tutorial, this vs it kotlin, kotlin standard library, Scope Function, Kotlin let, Kotlin also, Kotlin apply, Kotlin run, Kotlin with
Id: kJqDGBwoH1Y
Channel Id: undefined
Length: 40min 45sec (2445 seconds)
Published: Fri Apr 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.