Scala Tutorial | Scala Tutorial For Beginners | Scala Programming | Spark Training | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the only reason why Frankie should learn to arrest because in a two major libraries of two major frameworks from Apache like Apache spark an Apache Kafka their major implementations in they have written their implementations in Scala and that's one of the major reasons why Scala became so popular the last couple of years and also there have been various other frameworks like haka framework in a like clear framework and a lot of other you know a lot of other database tools database management relations mapping tools which were released by scholars so that's one of the reasons why Scala became very popular in the last two three years because of major frameworks we have released now in this particular area it's not only big data even in couple of other criminal trial yoga or play and even something on the thorium functional relational mapping tools which have been released and that's what that's what it makes well a little scholar a little bit more popular okay they're always asking me that what about fighting with spark how a real-time project distributed for skyline right in between spark so are always asking me that you know what about by spark you know the buy ten EPS was part and how our projects distributed between Scala and peyten with spark now there's one interesting thing you know I I got a I got a survey results from data breaks data breach with the comp which was formed by the founders of spark so these people who founded spark you know the founded a company called data breaks and they did a survey this here 2016 survey and it's very surprising that for the last one year there has been a huge shift in from scholar to baton so a lot of people are actually moving to Buyten when it comes to spark so and I'll tell me the reasons later you know why people are moving to Python gradually so and maybe you should also have that plan you know to learn Python as well but color is important you should not rely should notice call EPA's but having said that you know I think the next one which in which should be in your on your radar to do the learning is basically should have Python okay but I'll tell me the reason later why python is required and why people are gradually moving two by two so here is here is the person who is one of the cofounders of the other languages name is Martino deskey he works in a company called another company he works in a in a university called EPFL it's called a call Polytechnique fédérale the Sun so there's a place into Dillon Colusa solar then he works in the school of Polytechnic so it's called a called Polytechnic federal so that's how it works as a as a professor for computer science and mathematics and he's the one who came up with the whole in a grammar and and the whole compiler design for the scarlet language so as we can see that you know Scala is a general-purpose programming language and such an purpose means you know it you can actually use it for anything you want you know you can you can use it to write spark which is a cluster computing framework you can use it to write a play framework which is a web development framework you can use it to write akka framework which is an actor based concurrency framework so it's a visual bubbles language you know it it doesn't have any fixed purpose you can just use it between one editor combination as I told you before it's a combination or different programming and functional programming styles so now you know what this means you know now that I've explained to you so you know what what these two phrases mean and this was publicly released in 2004 on a JVM platform and later a few months later it had a release from the dot pen platform so they started working around early 2000 and now we have pretty stable religious as color which runs out now here are couple of features of color the first feature is that it's statically typed language and by statically dead language I mean that you have to declare the type of the variable and unlike languages like Python or Ruby or JavaScript where you don't have to declare the language integrated own product particular the variable type but here in Scala you know you have to declare the type of the variable okay so you have to tell the compiler what is the variable type so that it can actually do something meaningful and it's xmoocs paradigm I told you it's mixed by then because it fully supports object-oriented programming concepts it has function programming concepts as well and just remember that everything everything is the object in Scala so you don't have a concept of primitive types if you if you know Java you know that you know Java has primitive types but in fact there is no primitive types everything is an object in Scala okay so online Java Scala does not have primitive types and you know it does support a concept of static so we're going to see that later in how how static entities are achieved in Scala and obviously there are you know improved support life traits which are very similar to Ruby modules we would feed that later as well and by mixed paradigm you know if you look at the functional part we see that you know it's a bot function programming we are going to look into the features a function programming later in the course maybe in the next module and you know we there is something called pure functional programming so obviously so there is not a pure function programming language and like a scalar Ireland so and I'll tell you why in a later when you're doing pure functional programming languages so solid supports also act the libraries way which allows both mutable and immutable variables so that means you can you can use the actor's library to create concurrency based actor based concurrency models so people who are interested in working on actor based concurrency models can use the actor libraries or scholar so actor based concurrency is a type of concurrency in computing so I'm not going to get into that but if you're interested you can just google on on the internet and read or how it is actor based concurrency and you know we're going to see later that in functional programming you know functions are first class citizens so this is very important is a very important term you know first-class citizens functions are first class citizens so this phrase basically means that you know particular function or a particular in Scala or in any functional programming language a function is just like a variable so that means you know you can you can actually pass the function to to a method you can pass it as parameters to methods you can pass it you can return it as values from method you know that's what it means so by first-class citizens I mean that you can you can actually you know pass functions as as variables to admit that so it just be is like a particular variable so you can pass it as parameters to functions you can return it as values and then you can assign it they can assign it to another variable and then call it okay so we're going to see all these things you know these are very exciting things about functional programming and I'm going to teach you how you can do that okay and also Scala has a concept of closures so closures we are going to see later it is very similar to Python and Ruby concepts of closures so people anyone here know what closure is if you don't know it doesn't matter you know I'm going to teach you later what closures are okay so that's about scholar now let me tell you how basically scholar works okay so one of the major reasons was in the developers were looking for flexibility in languages to improve their productivity because all the languages learner they were becoming very heavy in terms of in terms of the compiler in terms of the code size so they were looking for a lot of flexibility in terms of data typing in terms of other operations and that's how in the scripting languages like Python Ruby then Ruby closure all these things in a become very popular and obviously you know based on you know what we have learned from scripting languages the other day you know more optimizations were done in existing languages so in Scala basically Scala is an alien language so all scholarworks is that any other Scala code which you give it to a scholarship Empire and that generates in a byte code and this bytecode is the format is exactly the same as the Java bytecode so this bytecode what you see here it is basically Java bytecode and then you can load this our bytecode on any JVM and you can just run intense in as any Java program so basically just remember that in any Scala code whatever you write finally you know it is when you compile it it breaks down into Java bytecode which basically runs on any njv okay so it's actually 100 percent helium compatible so that's how Scala you know works so whatever we have done so far any any questions you have any doubts you would like to ask them okay and these are in other frameworks which I told you in which are very popular and one is the one called play clear framework if you so web development we already has part four in memory cluster computing then we have a color which is activist concurrency framework and the other modules of neo4j which have solar rappers as well as a something called scanning which is a MapReduce framework on scholar so there are in a lot of things coming up and even if you have in the latest versions of Abacha Kafka that also has color in ApS you know - for developers so here's Ian Gosling is saying that you know if you were to pick any other language any language other than Java it would be Scala so looks like he also likes Scala okay so let's quickly now that time has come to write code okay this particular BM which I'm using this VM is already there in your LMS you can download this VM and install it in on a virtual box or on a on a VM player and this particular VM already has bark do you know Scala and all the IDs everything is installed there so you can just download and start playing around with it okay so let me you know Scala basically comes comes with something called the scholar shell okay so if you people who are worked on bite and any anybody who has worked on right then okay they're always asking me is this VM different from the Hadoop VM it may be different because it had to be m ro you know it has a bunch of other stuff involved also liking so obviously what whatever course whatever the teacher any Hadoop cause those things is you know installed but it's better to install this because it just has a hug on this part and Kafka and these things so it's not very heavy okay so choice is yours just checking out in that particular VM if if this part is already installed then you don't need to work then you can just continue working with the VM because you need to get the same focus anybody worked on a bite in anybody knows fight in here okay basically see the thing is that in a Python gives something called a shell okay so there is a shell called whiting shell well actually you can do you know quick stuff in a like in 1 + 2 3 so there's a bite in shell ok so it takes you to there and you can do quick computations you can write code test code and test yourself and once you're comfortable you can actually write it in the source file save it and then run it at this Python program so similarly you know color also gives you a scholarship so this is the typical scholarship and these type of shells are called repl shells okay just remember that these are called in re PL shells cooking repl means if you read you evaluate print you in computer science repl means read evaluate print loop usually means that in a you type the program the expression is evaluated and output is printed and this loop continues so that's what the repl means and here you know we have an RPN shell for Scala and so this div potential we have and here you can write in a code you can quickly test your code you can save the code in and then once you're comfortable then you can start writing it in the IDE we usually you know in the production code initially write coding the ID but when you are doing quick proof of concept or if you just want to showcase something or if you just want to see it for yourself you know just want to do a quick test then you can do B then Michelle is very very helpful now initial image you can do a lot of stuff ok yes raw eclipse can be used for scholar so you can use Eclipse IDE let's color that's absolutely fine then I'll tell you how you know being I have to using Scala Eclipse ID so this would be skull ID and you can just type help and see all the commands which are available here ok and we able to use the shell for a while before we go to the IDE now remember one thing is that I I told you that you know Scala is not an interpreted language so Scala is basically a compiled language so that means you know unlike Python or Ruby you know you there is no interpreter for scholar so this color language is completely compiled languages like Java or C++ so what happens is in whatever code you write in the shell and that gets in a converted to a class file and that code is executed ok so don't think that in this color shell is like an interpreter it's not an interpreter so whatever code you're typing and she'll have the background an intermediate temporary class is generated and that code is executed okay and you can see this color version here so whatever color you are using so we're using two 10.4 so you can see this version here now you can you can use this color you know as you know as an interpreter as well and I think out the later so so you know you can do things like let me show it to you so you can go to this color so you can just say you know ten plus two it prints twelve here and then you can say you can do complicated in arithmetic on to nine you know into 4 into 3 plus to solve this single so you can do and now what I'm going to teach you is I'm going to teach you how you can declare variables in Scala okay now there are this little bit tricky but just be attention to me and and see what I'm doing so when you create you know there are two ways you can create how very booths you can declare variables in Scala one is through using the keyword well and one is through using the keyword back okay when is the keyword well you actually be clear and immutable babe so mutable variable means that it cannot be changed so just remember that environment it's immutable variable so which means you know it can be changed so we're you know there are two ways you can declare a variable is that you can declare it as well which is called value or as a variable var ver which means it's a mutable really can be changed later okay so that's how you declare these are the two key words you use so if I say declare X and this is how you declare a variable so so whether you are right you can see that you can view it as as a constant and the way we declare it is now you can write Val here then you can give X here that's a variable name then you have to give a semicolon okay it's all you have to give a Columbia and then if you give the data type int int so integer int or float you write float you put a string you write string and then you declare and you have to give the value here okay you have to initialize it the value so then it becomes an immutable variable white immutable because now if you try to change the value of 10 to 11 is actually going to give you an error okay so just a reassignment to Val so that means in the valleys constant you cannot change on the other hand you can if you declare it as a bag then basically in it you can you can make it middle so that means you know this is quick immutable you can change the values you can do whatever you want okay so these are the two ways you can declare our variables like in color and also remember that you know you declare the data type so you know you can declare datatype make string and you can write so this is declare that string similarly then you can declare float so this makes it know it's all the same so you can declare it as a float it can declare it as double you can declare this as boolean so this becomes true so either various things you can do okay with this and these are the variables with you we should okay here if you can declare variables in scholar and what you can do with it so immutable immutable types is this clear to you what is the difference between bad and battle now there is another another interesting concept in Scala and which he pollution is a the stand is that NAU even if you don't declare the variable type if you just write it like this then basically Scala what further if you in the figures are the datatype so since you're only rewriting true here so Scala know that this this is a boolean type and it converts X to boolean so if you do like this it says that X is booking so please don't think that you know Scala if you don't like the variable type it does not become like dynamically typed a lot of people think that you know just because I didn't write the variable type the datatype for the variable it becomes like dynamic type it is not animal type it is called type inference in color so that means based on what the value you are initializing with you know it takes a smart guess okay so if you write something like this so it knows ten is an integer all of it it will make it as you know integer so if you write ten point nine they don't actually make a double because you did not say anything so since there's a floating point number so it converts it to our double but if you physically want it as float then you have to you know write it as float okay so then it becomes float so if you physically want to float you have to make it float or if you don't give any datatype then by default it becomes double so this is called type inference ginger in scala and please remember this okay okay this is let me introduce you to any and it's going to ask you a lot of questions from that time so you just make sure you oblige sure so here's the next is here's the first question and he's asking you and he's asking that is the scala repl acts like this color interpreter is this true or false can you tell her whether which is a 42 correct answer this is going to protect as a repl actors call a interpreter yes you're right the answer is false very good well done class so the answer is false because color is not interpreter Scala is a compiled language so even if it looks like an interpreter internally the code is converted to a class file and and the code is execute so the answer is false okay you give answer the next question caller supports primitives and Apple practices true or false what do you think yes you're absolutely right Scala does not support primitive and upper classes so the answer is false okay so so now we saw in oh how we can do variables and how we can declare mutable and immutable variables so that's exactly what's written here if you want to pre read only constants you can declare it as a valve this basically means if you have Java as similar to Java final variables similarly if you have our let's say to read right that means it's mutable and you can you know assign you assign values to it and obviously I told you about type inference so that means even if you don't give any type it's going to figure out in what it is and it's going to make a guess okay so just remember that Scala is statically typed language okay okay so it's basically statically typed language now you know let's talk about our expressions and blocks okay so there's another very important thing which you know you keep in mind so say for example in ayodhya 12 x equals to 10 in these curly braces I can I can declare actually values like you know a equals 100 and I can write well B equals 200 and then I can say a plus B okay I can do this now if I do this a a plus B then the value of x actually becomes 300 okay so so here what happens is it and you can write expressions inside you know curly braces and just remember in Scala so the last expression whatever you're writing the last expression that thing is you know assigned to to the video so that means if you have something in a blog so if you have something in a blog so whatever is the last expression you're writing so the last expression is basically returned from the block and here since we're assigning it to variable X so you know this becomes kind of return value for this work just remember this is a very important property of scholar you can actually use it in several places which I'll show you later and when you're writing you know instructions in the same line you can actually use the semicolon otherwise in Scala you know the deadline delimiter is basically a new line so that means in Scala you don't have to give semicolon like C++ or Java or C if you give a new line so that becomes a new instruction but if you're writing the instructions on the same line you can actually use a semicolon so I can write the same you know instruction without using the semicolon so I can actually write something like this like new curly brace I can say Val 8 equals 100 I can say Val B equals to 200 and then I can say a plus B I can do the same thing okay this and this is exactly the same I just wrote it in separate lines so that's why I did not have to give semicolon whatever as you know in Ex I wrote everything in the same line so that's why I give a millimeter to the instructions so which is a semicolon and obviously the last expression in it becomes the return type of this particular block so that's how mythical Escada works and this concert is very important for functions and methods which we are going to see later ok clear all of you with this with this piece of code ok good now let me show you something called lazy evaluation ok this is a very very important concept in school again and now you'll understand in a way scholar was used in spark now you know the right spot let's say for example I declared variable eggs in it goes to 1000 ok now the moment I declare it you know the memory is initialized and now we can see that X is equals 200 so now if I say X into 2 it prints 200 okay now say for example let's understand the business use case that how in what is the problem with this kind of stuff now say for example if this X was a list ok this ex-soldier list of say 1 million numbers and I want to multiply each number by 2 so if I just declared this X list equals 2 in 1 million numbers is going to take a lot of memory right and so the thing is that you know up front you know when i declare the variable it's going to take a lot of memories even if i don't use it but if i have a requirement that i just want to use it when I want to use it so that means it's kind of load on demand or just in time so then I can actually make it as easy okay so I can write lazy here so you see the difference between this declaration and distillation here when I declared it it straightaway shows 100 but here it doesn't show hundred you know it just shows lazy so that means actually no memory has been allocated to this particular variable and these are very very useful you know when you are doing very large computations that means a front you know you don't want to commit to any memory you only commit to memory when there is actually some computation you're doing so that means when I actually do an X into 2 that is when you know the ex is going to get initialized 200 and and the value is going to be computer okay so there's a very important feature of Scala and many intervention languages has this feature it's called lazy loading or on-demand load loading and in the difficult are usually when you're doing large computation so when you don't want to a front in allocate the memory but you just want to do it on demand and these are very very useful for cluster computing as well because in plus the computing you know if you have various computers in your network and just imagine you know if you just want to in a load say for example a list with you know 1 million values and you want to send it across in all the clusters it actually you know takes a lot of memory lots of in our network in which happens so rather than that you know what you can do is you can just declare it here and and it will be actually sent when you do some computation on it ok so it's like that so it's always on demand so 11 you are you know you're doing the computation that's when you know the value is initialized and center of the computers and the computation starts so that way you save a lot of memory you save a lot of network bandwidth and you can do a lot of optimizations in this one set ok lazy 12 a few and that's one of the reasons why you know Scala was chosen you know to write this cluster computing plane works part it is that way you know it could optimize because otherwise in if we just declare upfront you know these memories allocated and that becomes a big problem ok ok Praveen is asking me that can you give me an example with multiple values of X yeah sure I can give you but the thing is you know you still haven't learned collections so I just give the example but maybe later on you will understand what I am trying to write here so I can just declare a list you know a list of say you know want to say I can say 1 to 100,000 and I can see so here are generated a listing of one 200,000 and so is leaving one 200,000 and generating the list it's a list of integers with so many values here okay I'll front you know so many 100,000 integers allocate but I can optimize it in I I can just optimize it and and now say for example and if I want to you know find the sum of the list so I can say don't worry about the syntax you that's what you're going to learn later now you see that in if I want to you know finding you know sum of all the integers in the list you know I can just do this and this is the number so you have hundred thousand numbers here but just imagine in a few millions and millions a number it will be very inefficient and you in a straight away I look at a lot of memory to it so what I can do is you know I can just declare it as lazy so then it becomes is that when you actually do some computation so when you actually do a computation that's when it will be loaded so now if I do in if I just want to find the sum of all the numbers in that case you know now you see that you know that times actual memory is allocated okay yep ready ok Xin USA is asking me this definition will store in memory rather than process immediately as you are actually verso basically the definition is stored in the memory it is not if the instruction is not immediately executed you know it is just or the instruction is executed only when the first time when you reference the variable through some computation so when you actually do some computation that's when the value gets referenced so this is a very important concept lazy just remember this because when you're actually doing SPARC architecture you will see that this is this has been this one um be in cornerstones of the SPARC implementation so maybe you know you can just have a look at this example and if you have any questions for me nasty I'll just give you you know just 30 seconds or maybe one minute just take and just recollect whatever we have done in the last one R and T in a few if you have any questions about chicken ask me okay Irene is asking me that could you access the value without any computation or will you can do that the moment you access the value it's actually you're accessing it okay so in that case you know it's the immediate in the value would be in a loaded into the memory so the moment you actually in access the value the definition becomes a manifestation clear Irwin okay so go Vanguard if you have joined late don't worry the recording will be available in the LMS so you can just check the recording and you want to understand what this is syntax reduce I'm using don't worry I haven't explained it to anyone because it's just an example I was giving but we are going to learn about the syntax when we are doing functional programming so not only move it's not but this reduce just takes a some you know to find out the some of the list don't worry about the syntax but there's what the reduce function does okay and you can use lazy for you know for file loading anything you know so if you're loading a file you can for any kind of operations okay okay are always asking me is this reduce function similar to the MapReduce it's not similar but the conceptually they are the same because when you do in the reduction phase in a MapReduce whatever happens in this function also the same thing happens but conceptually they are similar but and they are not the same okay okay Praveen is asking me if you want to apply some logic on individual members of the list so if I remember it may load the list right yes you're right Praveen if you want to individually apply some computation to a cell in a particular list he cannot do that the moment you access the list the whole list will be loaded into the memory okay right so now we are going to see some if-else statement this is an I'm not gonna spend too much time on this because I think all of you know this nothing is you know you can you can desert if Akali fence structure so if you can and a boolean you can give a double ampersand for hand or two bytes for a logical or and you can give in a boolean operations like this okay we will is asking is this lazy concept similar to the big concept absolutely similar a similar it's the same concept you know which which we are going to use in spark and they also in spark also there's a concept of tag so the dag actually is are not nothing but an easy initialization so Vemula it's the same thing okay go do you know that at least that we you you can really be able to relate ok so let's see if PES part and now we will look into some important stuff called loops ok how we have a loop a particular invite relations so say for example in I declare a variable in an STR called test ok so here we have a test and just remember that in every every iterator or every collection has an implicit function call for each ok so so the for each what it does is you know basically inside the for each you know you can actually give a punch so what happens is this is what in a function programming is all about I told you that in functional programming functions are first class citizens so there's a function called println okay so this println basically just like system dot out dot println in java so it prints on the console and you know you can pass this function as a parameter to another function call for each and so this fourpence forage is going to do is it's going to print every element of this collection using this println function okay so that will be for each you know does and and i told you that you know for each you know is available to any collection in scholar and string is also a collection because you can see a string you can view a string as a sequence of characters or at the list of characters so you know you can apply this for each loop to in to any collection so that's what you know first-class citizens in action so that means you know you can pass this for hvcu takes a function and you can pass any function you know here which basically and here is the print function which is prints without a newline so the test is printed you're going to see the more of this for each loop you know when we are doing lists when we are doing you know when we are doing any kind of collections little later okay but just remember this particular function for each I hope it's clear right what for each is doing and you are able to understand this okay now we are going to see the for loop the for loop in interval a little bit different from the for loop you know in C C++ or Java so the for loop is written like this and this is the general syntax so here you write the argument and and there then you give an operator like this and then you give the collection here okay and then you write whatever port you want to write so this is a typical where you want to declare a for loop so here say for example you know I I can write something like this so here this is what this what for loop does is just a attention is that I have a list in from one to five so if you just write one to five on D so it will generate a range of numbers from one to five so those are going to collection it's part of the collection so what it does is it I takes each element and this buddy collaborator it assigns each variable of each element from this collection to a high and this is printed okay so so now you have from one to in a five you can print each of the numbers so similarly you know and by defaulting on the numbers increases from you know by one so now if you just want to run the loop or in the opposite way if you just want to say you know five to one so that means instead of drinking one two three four five you want to print five four three two one so this is not going to work because by default in on the loop increases by one so then you have to say by minus one okay so this basically means that the loop counter is going to decree with my minus one so it's this remember this particular syntax of the for loop okay error here you know raw is asking me what are this rest nine and you know then I'm seeing ratifying what are these stuff so these are temporary variables which are created okay so so say for example if I write this I told you that you know this code is converted to byte code and it's loaded in a class and executed so the return value is stored in a temporary variable called result is basically result in not result and it's a serial number one two three four five like that it's a running counter okay and then so that's what it so temporary it's a temporary variable in which variable to store clearer okay shelia is asking me can i have any other increment here why not you know you can you can give minus one you can give - to give anything you want so this just prints the odd numbers okay so basically you know it just strings the odd number similarly if you want to print the odd number in the reverse way so you can just say in one to five by you can say to safely print here's a leader so you can do buy anything and I just like a four loop counter okay shanira's is asking that just a quick question can I use minus one for each no in the for each I told you that before each you cannot pass other than a function you cannot anything okay so the for it only takes a function and a foreign to know we usually use for collections which I'm going to show you later but usually for the general purpose for loop this is what we typically use okay okay before I went to we went to a break Vimala was asking me that for she was asking me this okay so let me write it down she's asking me that what happens here okay so why is nothing been printed so what do you think what is happening here others would like to try what the Millers question is that you know this particular thing doesn't print anything so what's going on here yes sir oh you are right we will any any guesses does this make any sense at all because you are you know you're reducing the counter from you know five to one but you are incrementing it is this possible like it's kind of contrary condition so I just tell you it's like it's like telling you that can you climb from you know from first story to the third story by climbing down okay so this statement right cell doesn't make any sense got it right it's not infinite loop it doesn't make sense I track telling you that you November you go from first flow to third floor by stepping down I don't know how is that possible you know if you only can you can only go by stepping up right by stepping down you cannot go from the first row to the third floor it's like that okay nothing will happen actually because the condition is purely false you know so it cannot do that you know because ranges from five four three two one and you're trying to increment so obviously this whole condition is false and that's why this is never printed okay so give them less so the kind of false fault condition okay okay right so that's you know that's about so no pressure and we won't have any exception you know there's not an exception condition is just that the condition is false so obviously it's not evaluated okay so that's why you know it never happens okay and you know you can do in a loop you know this this one level loop you can also do two level loops like this so then you can you can run a loop from I say you know one to five and you can run J from say 1 to 4 and you can print you can print something like this okay Topol i j again and here's one syntaxin 'you should pay attention to see inside the string okay inside the string I'm referencing you know I and J so you can reference in a variables inside a string but you have to just prefix it with the dollar okay so if you prefix it with the dollar so Scala knows that you know this is actually a variable which needs to be substituted at runtime and how it is to be substituted as a string so that's why I put s here okay has a string if you want to interpret it as an integer so you can give a D here also no Big D so you know but the D is not going to work because you're putting it in the inside a string okay so that's why in a year - you know to put it s in s so that means you know just convert this integer to a string and substitute it in this particular string okay so if you want to get embed variables inside strings you have to put a dollar before the variable name and images outside the codes put an S so which means that you know convert these variables to strings and then I met them yeah so that's how you you do this okay so that's all you do in order to to level and even go up to three level four levels like that there's another important condition you know which which I would like to tell you and these are typically bad conditions okay so now if this is this loop clear to all of you this particular loop clear to all of you can I have a good poll on whether this key or not okay so this is how I end up in my particular loop now I can write the code like this also now the thing is that what if I just want to print only when I equals to equals to J okay so so I can write the loop something like this okay so I can write that in a for loop then you know I can write an if statement you know if I equals the equals to J then I can say you know print this okay and so then it will just print for I equal to equal to J okay now this particular inner loop is little bit inefficient in the sense that you know the iteration basically you know happens here because why the iteration happens is because you know for every you know iteration it basically goes inside the loop that this checking and based on whether the checking is true or false you know it does the computation but inspire in scholar you know you have a between a very nice feature and the feature is that you can directly write the if you know if you have a guard condition these are all God conditions unique and very active in in the for itself and then you can just say in a println you can just do this okay this is this is very very efficient and then this particular piece of code because here in the iteration itself you know you know it can do a check and based on whether the condition is true or false it proceeds with the loop because you can actually put a bad condition here and these are very important in when you are doing any kind of processing in your in your business logic so say for example you know if you have things like if you write a loop like you know for say for example for every customer okay from the customer list you are taking and suppose you're doing some processing okay you are you're doing some in the processing of the customer and you only check that you know you only enter the loop you can say that if you know customer dot is active so you can't do something like this in your life if the customer is active only that means him and he's not hibernating somewhere in Siberia so that's the case then then only you proceed with the processing otherwise you know you can just keep on going through the customer list okay so all sorts of you know in a predicate you can add here so this one business use case in practical application I showed you which is very very useful you know in linear processing clear all of you did you did you all understand this I I want you to understand you know this thing very clearly because I'll be using this inspark a lot and it's important that you're understand this as well as the lazy thing that the lazy is very very important that's the cornerstone of the SPARC architecture and I want all of you to be equally competent on this ok Praveen is asking me does this process have any meanings having if n for no ifs basically means that you know if the condition is true then only you know the chain of the code inside the for loop will be executed as it skipped okay Praveen okay what do you mean by Praveen any naming i/o you mean is does it have any name is that what you mean pravinia it's called a dot condition okay it's called a for loop with the gaud condition I said that you know when I was introducing you to this it's called a for loop with a guard condition okay universe is asking me okay no problem Praveen you know it's quite possible because you don't see me you know you're hearing my voice it's quite possible even without things no problem the universe is asking little bit on a little bit more on is active no exactly means that you know I'm just checking whether the customer is active or not so say for example I I just want to send some bunch of offers in or some free gifts to my active customers so so if only the customer is active then only and I do the processing from the customer list as I escape okay so that's what it's just a simple example I wanted to give you so instead of is active you can have any other things like whether is the customer a premium customer or is the customer you know saying in some particular part of the world okay yes shinu SH right I just write it down it in case there's a problem with my accent and people don't understand it's called a for loop you know with a god condition okay so that's when you write a for you know with an F inside okay yeah yes Geneva that's right so now it's better to write it down at times because you know English has many in a homonym so a lot of words sound similar so it's better to write it down so that we all are in the same page okay okay yep so once again let me see if I've answered all the questions okay now Erwin is asking that can you use this if in for each so yes we can use this in for each I'll let I'll teach you later I mean when we are doing functional programming because now in our IDE a lot of syntax which you may not understand plus I want to give you some more fundamentals on functional programming before I jump into you know on the usage so I don't want to rush into that you know so we'll do that in in the next module for now and I'll just stick to this example okay but I'm going to make it more complicated later on and I'm going to tell you exactly how you should do it in production code okay that's what you should do so but since today is the first thing I'm just taking a little bit easy okay okay a Rafaela is asking me that so just to clarify if I put that condition inside the for loop it's always more efficient yes or firelight's right if you put this inside the for so that's called a predicate if you put that very good inside it's always much more efficient you know then putting the predicate outside the fog okay okay I did I get your name right a fellow or you want me to call you MA or fella or just let me know okay Inderjit is asking me can me can you give us one more example with regard condition one more condition with the god condition these are typically in your typical checks you know which you want to put inside as I'll give you another example very very simple example though so say for example if you just want to print here maybe even numbers from the list so say for example you have this for loop in running from 1 to 20 and you just want to say that in a if I in a modulo 2 equals 0 so that means only if the number is you know even then only you know you print the number else you don't print the number okay so that's basically an example for even this is an example for odd clear in digit yes Geneva says I'll tell you now you know which is more efficient I'll I'll tell you like here I told you what is more efficient so as I go along and I'll tell you what is the code you should write and what is the code you should not write okay so so that I will go as I go along and so now let's understand one more thing okay so here since already give this example here so here what we are doing here is you know we are collecting a in a we're just printing a list of you know we are printing all the even numbers from which occurs between 1 to 20 inclusive okay so this is what we are printing if my requirement is that you know I I want to collect it in a list so instead of printing because he in actual program you don't print read you you collect the results somewhere and then you do some further processing on that so instead of printing because printing is pretty silly you actually don't do that in your life coding so maybe if what if you want to collect all the numbers here you want to just collect it in a list and send it back so for that you know we have something called a keyword called yield okay so you can use the keyword field so in ield what happens is ad is a keyword and so here basically the output of every volume by duration is stored in a list okay and and it is basically sent as a vector so you can actually collect this in the inside in a vector and now you have X in a which is so instead of printing you know you can just you know collected in a bank vector and then you can do some functional computation get run so remember this you know if you in case you want to collect some operations somewhere so you can actually do this so say for example the previous example I gave you about the customer thing looking so we were doing that you know if you know if the customer for you know for the customer list so if another customer is active then I can end another customer okay so then basically I will have you know so then what I can do is I can actually have a list of active customers okay if you want to list of active customers you can do like this so you can go through the list of all the customers and only if the customer is active then you keep storing it in our vector and and that you assign so then finally you have a vector of a list of active customs okay so that's a practical application of typically of using an eel okay so just remember that in the yield you know the results of the for loop so just to summarize in ield you know the result of each for loop sorry iteration is stored in the list again this is a vector to be precise and is very useful I I showed you when typical example where you can use this here all of you what with what ield is what it does okay good so all of you are clear with this now let's you know understand functions okay so now we are going to talk about functions in Scala how you define functions so from the morning in a way we have seen variables key words like Val which means an immutable variable okay Geneva yes you're right yield is only usually applied with the for loop so obviously know since you're collecting the results in an iteration so so obviously know it goes in it goes hand in hand okay from the morning we solve Val which means if you want to create immutable variables if you want to create mutable variables you use var now we are going to see how we can define functions okay so here is the standard you know syntax for functions so you write def then you have the function name then you have another function parameters and and then you have the return type and then you write equals and then you give the braces okay and this is where you write your code so this is the standard in a typical way we write in the function so we read the function in the function parameters and the return type so let's look at an example so I'm writing a you know function to calculate the area of a rectangle so I can say length you know which is an integer well let's keep it float okay so this is my link and this is my bread B which is again a float and you know it returns into the float the results and I can write linked into bread okay so that's my function so if I want to write in a function to calculate the area of rectangle so I say def the name of the function area rect then I give the parameter list so I write the parameter name then give a colon and then give the data type and then then with the commas you know I keep on writing and finally the parameter list is over and then I give a colon and then I justify the return type here then I write an equals sign and then within braces I write the code now I totally know in the beginning of the lecture that you know anything written inside the braces you know the last expression here it's only go it's only it's the only expression here but usually the last X question becomes the return type so here basically you know this becomes a return type so actually in Scala you know you don't have to write something like this you don't have to do this unlike Java or C blessed with the last expression by default becomes the return type becomes the return statement so the length integrate is defined okay so so this is the one which we have here let's taste it you know in the console oh this was float sorry so then I write linked into bread so my function is defined so I have a fun you know area function call area rect and the parameters is linked in bread which is of type float and the return type is float okay so now I can call this function Ling area rect and then I can pass 10 a 9 it sends 9 organs a 9.5 and arrogancy 10.23 so so this is taking float on us to float so this is a and here I'm writing F because you know by default it tends it as a double but since my function is only taking float here so I write f so f basically means that treat this number as a float okay so so this is my function which I've written here and you can write various function so you can you can write even so this particular function is returning a float okay so you can also modify this particular function to not return anything okay so so say for example you don't want to return anything in that case in a you write unit so when you write unit it means that in your function doesn't return anything it's like a void function okay so you can see in del n if you just want to print in the area so then you have a function called a redirect and now if you called so it just drinks you know the number so here the function is not returning anything it's just sprinting so then the unit basically means acting and the function doesn't return anything so it's a void return type okay clear all of you with this unit stuff how to define a function okay now that you know all of you know that can you quickly tell me can you write the code for me if I want to check a function say whether the number is even or not so if I want to write a function call is even number so I just want to check whether the number is even or not can you tell me how the function is going to look and here I'm going to pass you know a number so I'm gonna pass and it's going to return a true or false whether you know whether it has it is give a number or not so can it tell me how I'm going to do this can you write the code for me let's see but whatever you've learned today okay let's see what what things we have here brilliant Ramola brilliant okay Rena let's see we know what you're doing we don't need that if Rena so without the if it's fine well done Rena yes your code is going to look something like this well let's see what Praveen is doing no Praveen this code is not going to compile okay I'll write the code in I have all sorts of stuff here so this is the best you know we're checking right so I'm going to write it as is even okay and this is the number and there's a boolean and just here I write number modulo 2 because 2 goes to 0 because this expression is the boolean expression right there's a boolean this will evaluate to true or to false okay so in that case inner you you obviously have there's a boolean outcome so obviously now this will be the true or false and that's the best way to check well done for the people who had written the code like this is the most efficient and the best code which you can write ok so just write this number modulo 2 equals 2 goes to 0 because this by itself is a boolean expression so it can either evaluate to true or false okay sure ledger what have you done yes you're right earlier it's the same stuff which you've written okay okay now Praveen is asking me that if I want to if how I'm going to do it okay so the thing is that it is the best stuff but if you are if you want to use if so I said no I want to use it even though it's not the best so in that case then you have to write like this okay you can write it number equals equals to zero then you stay true else you say false and then you close the number okay and then you say oh boy okay thanks for okay Praveen so what you don't need to write like this of this so you actually the best way you can write it okay so but it's okay now it's good to learn do those okay so one thing you notice here that we don't have to write return here because that's really not required so so that's one of the fuzzy things about this language okay good so I think all of you have learned how to write function and this is all what is there you know in in functions if you understood this basic part you know that the basic thing is that you know how you declare a function with a parameter list what is the return value and how you're going to return the value I think you've learned that you know there's nothing more to it now it depends on what kind of functions you're writing and what you're doing there okay but otherwise it's absolutely fine okay now one more thing which we are going to learn here is about arguments okay so let's go back to our function in which we wrote here oh yeah probably I'm going to tell you that little later okay so let's go back to our occasion Ivana is asking me what is the issue with my syntax okay one sec let's see what you've done not surely whether this stuff is not going to work right will it will it work what do you think no I think the best thing is you know just check it out in the just check it out in the console because you know I don't want to sit invalidates in tanks with is that that's what really wastes my time so but if you have any fundamental doubts you can ask me and I'll be more than glad to help you okay good so let's go back to in addi area thing and I'm going to teach you something called default arguments okay so I'll just give the function definition simple so that we can focus more on the learning and rather than get stuck with differ types so here's the function I have in an area wrecked so I can call this to this art works now you can keep default arguments okay so here by default arguments I mean that you can actually set say you know birth to ten okay if you do that then when you call length you know so you just have to pass the value of length L as 10 because bread by default will be in a set 200 okay so so that you can do and in case you want to overwrite the value of bread you can give 12 here so instead of 10 12 will be used so now it's 120 now there are a couple of rooms you know in there couple of rules in in setting default arguments the thing is that you cannot set default arguments like this okay and this is more or less in a true for all languages so you cannot do like this if you do like this you know there will be a lot of confusion so say for example if you define a function where in the first argument the length we are defined defaulted to 0 and then the bread you know you haven't defaulted to anything now when you try to call it it basically complains because it really can't figure out in what is this 10 should it assign the 10 to the length which already has a default value of 10 or should it assign in a children assign 10 to B which is the bread so then the compiler complains one way you can solve it is that you know you can write B equals to 10 okay then in that case it will work because it knows that L already has a value 10 we should be 10 then okay so you can do that and you can act your code but if you don't do this then it's going to complain but this kind of coding is actually a bad practice I don't recommend and what color best practices recommends is that when you give default arguments make sure that you know you give the default arguments at the end okay so don't give it at the beginning but give it at the end okay so that you know you don't you know planned up in writing things like this okay but having said that you know you can have a default argument in the beginning and you can resolve this by actually you know telling which argument is what but that's not a good practice okay even though the compiler allows it's not a good practice good practice the best practice is that you define the default arguments at the end of the parameter list okay and then once you define like this then you know you can call area rect then dips works it works perfectly let's start now you know Scala has a rich collection of it has a very rich library of collections so the collections you know which we are going to primarily focus on and which are very important for you are arrays array buffers Maps tuples and lists and in most of your career or in context or programming image you're going to use either of the five and they are very very important so I'll tell you how to used so first thing is in a way would use as an array or now an added is of fix I slept in so you cannot change the size of net and this is how you are in a declare an attic so before we can use the class let me show you the class first so if you go to those color Docs and type Mary so you'll see that this V and you've got in this color package these are the method you know it has okay so you can go through the methods later on and figure out what are the things you need so let's use the Annie so you declare the area's new then you write a day and the thing is that you know whichever package in a vision is in this color package so whichever class is in the instance color package you don't have to important so it's just like the java dot lang classes in the Java doc language they implicitly in available to all the classes so similarly Scala in whatever classes come on this color they're all available to the compiler the theorem that important to be good so we ready and then what you have to do is you have to specify what the array stores because you know in in color just like Java or C++ you can only store homogeneous objects okay so in this area we are going to store integers so you mentioned in square brackets integers and then you have to mention the size of the array okay so so this is an array of integers and it's going to store 10 integers okay and all the elements are initialized to 0 and you can access the array you know with they are all the 0 based index ok so you can see that the arrays are zero based index and this is all you access be an in with the normal brackets and you know you can set the value to the array like this ARR 1 sorry equals 200 and now if you print the array you can see hundred 200 like that okay and if you want to print each element of the array so you can say for each println which you have already learnt in the first module sorry in this module so it's going to print all the elements appearing scare how you or declare an array the similarly if you want to declare or you know an array of strings say declare an array of strings and you see scenario 3 strings declared to null and you can initialize them to test and is another fourth route okay yeah and the thing one way of declaring an array in a new array give the data type and the number of elements you want in the array or you can start again with acuity re so rather than giving anything you can just right at it and the very latest an array and you know give the elements inside it though will naturally we want to doing such things in programming because the data is usually interpreted not by a program obviously you'll never hard code the data but in case you are testing something you know just small things you are testing you can also declare like this okay so and then you like this as well okay so both are valid but usually this is a statically declaring the area which you're really doing the program other than testing stuff most of practical issues you'd be declaring it like this okay now I would like to ask you one quick question have you understood this whatever I've done it'll clear any questions you want to ask me okay now I'm going to ask you one question and the question is that you know you you see that the way I have declared the array we can take libya's well ARR equals two new array of strings element 3 now this ARR is a well okay so that means it's immutable okay every right VRR is immutable and if that's the case then I'm able to add you know values ruler to that integer stuff okay it declared integer Ally as a Val and then I'm able to add elements to this you know because yesterday we learned that well you know it was immutable so that's the case then how am i adding elements to this particular thing any ideas any hope you're always saying that the number of elements in the array is immutable no that is by definition you know an area by definition and the site is fixed so that's not a big deal but what I'm saying is that you know I'm declaring this as well how come I'm able to sign it okay pravin is saying that after initialization you have assigned a value so if you redo it may that's writer write won't throw error you know I can I can again you do this stuff I just work so then you are it's not the size of the area which is by definition size of the array once you declare it immutable I'm saying that Y is the ARR reference now you know it's still kind of you know I can change values entry okay thank you all for your answers let me give you the answer here the trick is that you know when you declare objects as VL as as well as basically the object reference we continue to okay that doesn't mean the object is in YouTube so you can have you can change the object value you can you know you can change the state of the object but the reference remains the same okay so you won't be able to change the reference so that means the error will always point to this particular value you know this particular hurry okay so I can give you the example here to say for example you have this area here now you cannot do this so error will always point to this particular area of 1 2 3 4 5 not actually now do this okay you cannot write air and now you know under to it if you try to do this exclude give an error so that means this particular assignment this reference remains constant gear out of you so remember that you know many poor cases of objects and containers so the reference it'll always point to this particular either you cannot change the reference but having said that you can change the value that's that's very much much more ok so just remember the fundamental difference with in general objects an array so for example if I write x is equal to 10 so here what happens is in X is always pointing I always having a reference to them you cannot you know change the reference of X from 10 to something else and for this for this also you know this error is always pointing to this collection area okay so you cannot change the reference however you can change the contents of the adding you know that is quite possible no problem with that ok yes now you're right so individual elements can be changed by in a by accessing the index ok talk about anything that how do i define an array with a set of where the values are constant see praveen if you want to declare an array with the values as constants then obviously up to individually now you have to insert in a value which are immutable ok so but generally by definition you know it is not possible in a days you know that's always college designed because I really don't get a use case that what I you want to do such a thing because you know in a collection you're going to store value so why do you want to store constant you know stuff in the collection in that case you can just keep that in a variable right so Scala actually does not allow this even Java does not allow this sense because there is not real good use case for this in business ok so so that's about you know and it's to remember this is a very important thing so now we go to something called area buffers so the only difference between a 10 and a buffers is that it has a variable length so it can actually grow okay so let's again take an example I can declare an array and you have to mention what is what you're planning to store there and you don't have to mention anything in the racket the size so basically you you get an area buffer what happened a debuff and not found so I have to import import color collections not beautiful not add a buffer okay okay so you can put this color collection you don't lie buffer you can declare this array buffer okay so we have this item over here and elements to the sorry bathroom like this so hundred has been added now 200 has been added and even also you know add like this as well you know you can say 300 400 so you can do like this so now you have an array of okay Charlotte is asking me a question I didn't understand what's the question okay and you can also you know so we saw that this is how you can add you know any elements to add a buffer and also you know you can if you want you can straightaway add you know an add a also to the area buffer sorry plus so now you can see if you want to add individual elements in you can you can use the plus equal to operator and if you want to append collections in it to the existing area buffer you can use plus plus okay okay surely guys I'm just trying to our understand what sirisha is asking me disco-pop well here okay silage I think that she's trying to understand services trying to understand the scope of Val and bears in terms of Mary see the things that you know when you make an area pal can you hear all of you can you give Mia okay so when you make it a bath so that means it will be constant the reference is constant and when you make it back so that means you know the reference can be reassigned to another inner to another area or something like that okay okay you want an example actually I explained it to you another way you are still confused we have this area and we have this area which is at a buffer now the thing is that you know if I if I declare another you know I cannot do this because I won't be able to reassign it you know I cannot write a de buffer so that won't be possible you know I cannot do this the tears in reassignment is not possible but if I declared this error as well then a reassignment would have been possible okay and Ajit is asking me that creep please explain explain result five and that is a dark 15 yeah what we'll do is it well let's quickly see the e in an array before class so if you see the area before class even co-operators okay so you will see the pathless operator so what the plus plus operator does is basically you know it creates a new collection containing both elements so if you look into this definition it says that and it creates a new collection containing both the elements from this collection and the provided collection so that means what I am doing here when I'm doing a plus plus so basically it creates a new era buffer and you know with the old values as well as the values which I'm providing here here in digit so similarly when you do a plus so that means you know what happens is you can append you can append in an existing collection to the error so let's look at the plus now so if you use a plus it says that in append a single element to this buffer and returns the identity of the buffer okay and similarly know like just apply just like plus plus you also have minus - okay so this basically what it does is it just a complement of that so you know whatever you know elements are there any that will be removed okay whatever is in the traversal so I'll give you an example here so here we added you know this particular thing 600 ton hundred eight hundred so this is my new add a buffer which I have I can I can right now if you want to get rid of 700 800 you can give it like this so now you see that you know from the old daddy 700 800 has been removed okay so I think that sort of Carl was asking that is it possible to subtract to remove values from a new buffer so exactly this is how we do it okay care carne and similarly now you have the - is upgrade also I think if you want to remove individual elements from these again and then you have a bunch of other utility matters like if you want to append to the string append to the collection if you want to clear the collection so let's look at couple of other operations which are there so on operation we have in yes universe I'm just coming to your questions only so this is for random stuff you know and the same some cooperation just are in a you have something called a dream end so trim and means in I'd will and these are all applicable to both added an edible possibly it's not only applicable to adage is they're applicable at above us as well okay so to tell you that in a when you call cream end so this removes the last two elements okay so it goes it goes there and calls the last two elements and it's just like this and you can you can say ARR dot cream and so now if you print error you will see that 500 and 600 has been removed okay so these two elements have been removed okay so similarly or something called insert okay so insert what it does is in the you can you add the element at the second index so in the insert the first parameter is basically the index and the second parameter is the value so when you say to nine so at the second index element man is going to be inserted and that was your questions universal say you were asking me that how would I like to remove the third element so that's how you use a removed parameter okay similarly if you say insert and you give to and then followed by a list so basically this inserts you know all these numbers in the second index similarly if you say remove for two so here two is the index so it removes an element from the second index and it also has remove another overloaded operator where you can specify the index and also how many elements you want it to be removed so you can say that I know you can just take the list and you know from the second index remove three elements okay so there are a lot of utility methods here so I suggest that you can you take a look at them okay indulge it I haven't gone to that so just huh so any any questions on the common operation so I suggest you know you have a look at this color docs and go through it okay now let's look at some transformation I've already shown you the transformations here so you know the thing is that you know you we declare so you remember decided we had here so that that couple of more elements so this is the area we have now okay and now you know if I want to print gentleman on the ice or I can do like this okay or I can do like this as well okay so I can say the number I can see a RR and then I can see printer lint I can do this is a but this is more preferred okay so in production I suggest you do this this is not visible this is not how you do it this is the way recommended way of doing in case you want to just print out the odd numbers so now you just want to check the odd numbers again you can give the guard condition what I taught you yesterday okay so you can say if in a N what if you do not equal to zero so you can just drink the odd numbers here okay so that's exactly what this example is doing now the thing is if you want to collect you know the numbers then you can use ield okay so so there's another way of doing it but you know I won't teach you these things because this actually you won't be doing it like this you know you know if you want to extract order even numbers from from a list this is not how you code okay this is very silly way of doing it into color ideally in practical cases and you'll be using filter functions you know I'm going to teach you all these things in the next module so so ideally you will do like this okay so so you will actually write code like this okay instead of code like this that's not how you do it okay this this is actual color in doing it but don't worry about it and are these things I'll be teaching in the next module filter are produced and other other utility functions okay good so so that's about you know area buffers now let's go to other common operations you know it has in a utility methods like some max so you can you can actually generally you know get a summary so here the air are so you can call the error code sound it gives you the sum of the collection then call max sorry you can call max which is the max number and the VIN number okay so all these things automatically available to any numerical action you can use them okay now let's look at maps so masa like you know hash tables so they're like key value pairs so in color a map is a collection of you know pairs key value pairs so I'm sure you've used inner hash tables or match in your career they are you know by adding in the initial program these are needed and the pair has you know two values done naturally at the same time okay so let's let's declare a map okay so the thing is madame equals to map so here you specify the key to say for example I am specifying the roll numbers and the name of students so so here what I did was in I declare the map and here is the key okay the key is 1 so basically the key can be any data type okay so here it's integer so which is which may be the roll number of the students kind of values the name and you give an arrow here so it's sent accesskey arrow value and it can be of any data type so I created a map basically where the key is integer and the value is string okay and this is what my map is and it's an immutable map okay so that means you cannot change the map okay you you won't be able to do you won't be able to change the map so the thing is that say for example you know you want to you know delete you know chutki you won't be able to do that today because it says it's an immutable map so if you want to create a mutable map so this is how you do it so you do an import scholar drop collection dot mutable dot map okay and then you are going to use this map here so now it becomes a mutable map okay so there are two map classes you know in in color there is a mutable and there is an immutable okay and every collection becomes a spares so there is a mutable collection an immutable collection okay so there is a map here and if you see the map here so there is an immutable map here in the immutable package similarly there is immutable members so they come in pairs you know mutable and immutable and you can use the one which you want so now what I've done is and I have created a mutable map of Jenny and Benny and this is the map I have and if you want to access the elements of the map so you can do like this so you can say m and you can give the key it prints penny and if you give em - it prints Jenny and if you give some rubbish which has some key which doesn't exist still give you or tons of errors and I'm saying that key not found so there's another way of doing it so you can call em door get in a one-state basically returns as an option so you won't be learning options in this course it's a pretty advanced concept but just remember that you know there's a method called get in which returns the value another utility method I'm telling you the important methods which you'll be using in the map so it's called get orange okay so what it'll do is it will try to look for the key and the key is missing then either you can give an alternate message like you know key not thought okay so here obviously one is existing so it says Lenny would say for example if you use 100 so then it is key not font okay and you can give any message here you know sorry any messages okay so you can see data missing okay or you can actually give minus hundred of anything you want okay you can do anything you want here so that's basically an important method okay now you cannot put multiple keys or a file at so you can only put a single key there okay and and if you want to delete keys so if you do like this so you can give the King name so then it's added okay and if you want to add a key so then you can again say you know add the key back so now this hour and in the key back okay so no I was just waiting for Shelly's I was just waiting for Venkata to ask the question he has a question okay banker is asking me that can we use get orals without a key no ven could it really doesn't make sense because when you are trying to get something you can only get from a map based on some you know based on some key okay so if you don't have the key that how are you going to get it got it so now you cannot do that benkard so when you when you mention when you use a get Orleans API you have to you have to use know the key so let's look at the documentation you know so that you don't have any doubts so if you go to get Orioles she would have better get our heads okay so the signature clearly says that you know you you give the key here okay so you have the key here and and also you give the default okay so if the keys in case not found then the computation it clearly says that in a computation that ins a default case in case no key is found okay there's a couple of interesting questions one question from row he's asking it in Val and we're in a really makes any it doesn't make any makes any difference until we really import our mutable and immutable collection see the thing is that in a you to understand the difference between mutable and immutable collections and Vallon back so when you actually declare them as bad so that means the references can be changed okay when you declare them as mutable when you import them as mutable or immutable that means the values can be changed okay so if you use a mutable map that means the values inside the map can be changed but if you but you can still make that as a bad okay as I'm doing here so so here you see that you know I have declared a map you know it's a mutable map so that means I can change the values but I can't change the reference because I cleared it as well okay so so there's a distinction between a bell that as well as and on the other hand mutable and immutable pura so I'm glad you asked this question so mutable basically makes it you know whether you can add inner values you know to the collection or not and battlin bar basically differentials whether you can change the reference or not I can type clear all of you so just remember that you know okay we have one more question from Ganesh that's also very interesting question Ganesh is saying that how do i instead between keys so so suppose there is a key one here okay so there's a key one and the key to as in this case you know Benny and Jenny to students Ganesh is asking me that and how can I insert in something in between now I would like to ask you you know I just like to put the question back to you and ask you whether it's really possible so is it possible to remove elements from a sorry is it possible to do this kind of operation at or what do you think okay well done Praveen world and universe so I think you gotta try it see the thing is in a couple of things you have to get in place okay the thing is firstly when you talk about the hash tables okay so I just mentioned what I am doing when you talk about the hash tables on a map okay it's basically on this in computer science okay it's always unordered okay so it is never ordered solid unordered that means it's on soccer and white is unsorted because if you look at the functioning of a hash table they use something or the hash function you know to determine you know the location of the key and value okay so where actually your key value pair is going to be stored in the data structure you really don't know you don't have any clue on that that's if you get them in by the hash function so it's what ball so left key one is here key one is somewhere here and key one is somewhere here key 2 can be here and I have to give one there can be key hundred okay so the order definitely we don't know what is the order it will depends on how the hashing is done okay so I won't get into the details but just remember that in hash tables map they're always unordered and so obviously if that's the case then really it doesn't make sense to insert between keys or before keys because the ordering there doesn't matter you know it's it's purely random access clear Ganesh okay so Allah is asking that can we remove elements from any mutable map okay what is the definition of immutable immutable map basically means that you cannot change the map you cannot change the elements of the map once it has been in a once it has been created so that's the case then obviously we won't be able to change it now let me ask you a couple of more questions okay now that we are just a minute let me see anything else is there in the map no writing out only everything about math before I go to the next stop a couple of things I want to ask you okay this is related to programming and you will be using this a lot when you're doing it some serious color programming is that when you know should you use mutable versus immutable collections is not only map you know it can be it can be anything like Arab array or sorry buffer or list or vector anything so when should you do that the thing is if I just remember this that if you're writing an APA say for example you know if you if you're writing a function say you know get customer records okay and this is maybe you marathon you know talented collection that immutable map you know you can you can have the customer ID which you know which can be an integer and you can have you know the customer itself the customer object okay so so you can actually create something like this okay I just hope and so that you know what are the data rate so basically you have in a something like a map like this okay happens is not NAU you are sending an immutable map with customer IDs and the customer objects now here the advantage of this kind of an API is that you know when you send it to the user the user won't be able to change in any records so it's more like a constant thing which he gets an e cannot only query and figure out who is doing what rather than so you know rather than changing stuff okay so so when you're designing ApS which are tons in values to people so it's a good choice to make it immutable so if you are sending in the form for collection weather then that makes it more robust and flexible and you don't have to worry about any mutation and the client-side okay yes you're right Rafael a sort of fella saying that they may use mutable if you are intending to change the contents and immutable if the contents are not expected to change your arrogant revilers so that's exactly what I what I explained now shall Ajay saying that when you know the elements might change for example the orders of customer and then you can go for mutable but customer name can be immutable absolutely right so these are the examples you know various examples I'm glad in all of you really internalize this and even Carl is saying that you should use immutable collections when it is fixed so so typically the thing is you know you should go for in immutable types when you're returning data to the client and the client can only take the data and maybe show it in a web page or do some processing with that in that case you know you don't have any fear that your data can get corrupted or changed by the customer okay so that's that's the advantage of this there's an L so that's one advantage okay so that's one advantage of a mutable immutable so basically it's data integrity okay that's one at what is the second in their advantage which surprisingly no one said there's actually synchronization okay so synchronization overhead is reduced see the thing is that you know we why do we synchronize we synchronize because it's quite possible that you know two threads can change the state you know awful awful data of an object so we need to synchronize to make sure that one only one thread you know and read the object at a time but the thing is that either the data is read-only so if you have a read-only data so it there it really doesn't matter to synchronize okay so because it's read-only so no matter good friends read or you know 300 threads read that particular data the cannot be changed by anyone so you don't need to synchronize with this everybody is going to read the same stuff it's just like the newspaper ten people reading the newspaper you don't have to synchronize because nobody can change the news okay so it's just printed and whatever is printed people read here are looking so just remember that you know the utility of that's basically the use of immutable so what's the data integrity and second is the segmentation overhead is minimized clear all of you any any questions on this one ever hears canister right for the nation saying that we are using mutable as less as possible exactly I mean we don't use mutable much unless you really have a good use case to do it okay so now we come to the next data structure called doubles so apples actually in time more practical scenarios in it you won't be using couples a lot because there's something called case classes in color so we'll be using the case classes a lot instead of tablets let us go to know what apples are and but for all practical purpose we won't be using tuples but okay so double the modulus forms I know because we saw that you know any restoring things in a list you can only store in a homogeneous inner entities you cannot you won't be able to store a tree any stuff but in Chapel W Jinnah you can store more than two values or potentially different types okay so that's the advantage and these are pretty useless you know in most of the practical cases because we use something called case classes in Scala just to you know just to visualize this is to understand so say for example in you are reading in a table you're leading a row from a database table so it can have an employee name and employee ID and employee address so that is impossible you read that in a double ok so so let's understand you know this particular thing for example we I declare double Val D curve so I can I can write like you know say the employee ID 100 employee name is a John and the address is you know anyway say you know crunch so now I've created a couple of integer string and string and if you want to access the tupple this is how you do it so it's always you give it underscore first you have to give a dot and then you have to give it underscore and the index doesn't start from one it's that sorry it doesn't start from zero it starts from one so it's a funny way of accessing and they do not know whiny and this is how it's done so underscore one gives the first element and underscore two gives second an underscore three is the third element okay and typically the use cases are say for example you're reading a table from a database and as and when you read the rows you know you can keep the rows in a double and and then you can process so basically you can either you can create in a row of you know a collection of tuples and then turn it across but usually you know what I have seen and obviously if you can do operations like instead of 100 you can make it 200 and obviously you cannot do 200 because you have to give this T as well so if you make this D as in a wire then you'll be able to you know and you'll be able to sorry what happened oh sorry nothing is when you when you create a couple the reference can be changed but in the values are constant so you cannot change couples that's why definition so that means you know once the value has been assigned you cannot you won't be able to change the values of the double it's very similar to Python tuples and in right and also you know once you create a dropper you won't be able to change it so here it's also the same thing one thing is what you can do is these are very useful you know for sending data across across processes or across you know across boundaries I hope you enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply to them at the earliest do look out for more videos in our playlist and subscribe to our Erica channel to learn more happy learning
Info
Channel: edureka!
Views: 228,201
Rating: 4.793704 out of 5
Keywords: yt:cc=on, scala tutorial, scala tutorial for beginners, scala programming, scala programming tutorial, what is scala programming, scala language, scala programming language, introduction to scala, scala tutorial for spark, scala apache spark, scala training, learn scala, learn scala programming, spark tutorial, scala edureka, edureka, spark training
Id: xNAD6cBKyaA
Channel Id: undefined
Length: 98min 26sec (5906 seconds)
Published: Mon Jan 16 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.