Elixir Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to my elixir video tutorial in this one to tour I'm going to cover pretty much what you would learn about a lick sir and a standard 300 page book I'm going to cover the core syntax in a language from which everything else is built in the description underneath the video you'll find timestamps - all of the different parts of what I cover in this tutorial so you don't need to watch it all at one time you could watch it and then come back to it and take your time also in the description is a link to all of the code in this video as well as a cheat sheet and it is heavily commented and you should get it and take notes as you go through it to properly learn this language and also inside of there is a link to a tutorial on how to install elixir on both Mac OS and on windows and I have a lot to do so let's get into it okay so I'm using atom just like I showed you how to use whenever I showed you how to install elixir and I have either the terminal or the command line if you're on Windows everything works exactly the same and I have a file here I called it a lick sir touch dot e^x I don't know if I put this over there you can see elixir TX and I'm just going to start writing code so the very first thing you're going to do is you're going to create your module which is going to hold all of your functions and to do so you just go define module and see everything popped in there I'm just going to call this M just to keep this nice and short now functions that you're going to define are going to start off with DES and then you're going to have your function name now you normally don't have a main function in elixir I'm setting it up this way however to make it easy to work with now what I want to do is I want to ask the user for their name and then I'm going to output it and how we receive input from our user as we go iö gets and then you can put either parentheses inside here or not put parentheses I'll use parentheses sometimes I'll use them sometimes I want so I'm going to say what is your name and put a question mark in a space and then outside of this I am then going to trim off the newline and to do that you just go like this and you go string and trim and I'll get rid of that newline so now we have there am and it's stored inside of the variable name and basically variables are going to start with lowercase letters or underscores and then they're going to contain uppercase letters underscores or numbers so if I want to now output this information I go IO puts and here I won't use parentheses and I'll go hello then I can use string interpolation or just it just provides a way for me to take the variable name and insert it inside of the string that I'm going to output on the screen I'm going to put name inside there and as you can see there are no semicolons no periods no nothing and if we save this now we're going to go over into the terminal or command line I'm going to type in ie X and you can see everything loaded up there nicely for me also look - well I'll cover this you can if you want to abort or get out of this you just tick control C and then a and you're out of it obviously you want to make sure you're in the directory where you have your file so there that is and you would type in dir on Windows if you're using Windows instead and what I want to do here is clear that out again so I'm going to go ie X so we can execute this then we're going to put in a sea with a quotes around the elixir cut part here and then we're going to come in and go module name followed by the function we want to execute which is main it says what is your name and I type in Derek and then it says hello Derek okay so really nice simple little application we can run here so now let's go and do something a little bit more complicated I'm going to call another function I'm gonna call this a data stuff and call this function and then we're going to create it down below let's get rid of this so we don't junk up our screen with all this extra code so come down here and create a new one and what we're going to talk about now are the different data types now basically you're going to use the different data types such as integers floats atoms ranges binaries lists maps to bulls and a couple other different ones I'm going to cover all of them in its tutorial and once you set a variables value that value cannot change so let's first stop start with an int well first off let's create our function so I'm going to go define and I'm going to call this data stuff and do and then inside of it we'll go and create an integer I'll call this my n is equal to one two three and if you want to find out what type of data it is you just go IO and puts this is going to output it and we'll say integer and then we will go and check if it is an integer and throw my int inside of there and I'm just going to compile this it gave me a silly warning don't worry about it right now and then I'm going to come in and execute it and you can see integer true comes back right like that let's go and do a similar thing with floats and it's also important to understand that with integers there is no maximum integer size so let's create a float here instead and 3.14159 and we'll check if this is a floats and you can do this with all the different data types here I'll just go is float and I'll go my float and execute this guy will save it first and put her here compile and run and you can see that float true comes back you're also going to be able to work with atoms and an atom's name is going to be the same as its value so let's just come in here and let's just create and add them directly inside of here so I'm going to say atom and I'll say is atom guessing you probably was guessing and this is an atom right here so Pittsburgh is an example of an atom the name for it is exactly the same as the value and I'm going to show you a whole bunch of examples here that are going to make all of this make a lot more sense let's come in here and do that and you can see atom true comes back also it's important to know that if you want to use spaces inside of your atoms you're going to put a colon like this and then double quotes and let's say you want to use New York there you go once again the name is the same as the value and likewise you're going to be able to define ranges and here I'll just define one but I'll show you later on how to use them so you go to 210 is going to be equal to and you go one and then two dots and ten alright so that's the basics of the most basic different data types that are available to you and now I'm going to talk about strings all right so I'm going to change this function named do stuff and do stuff just to keep it generic now basically strings are going to be defined with double quotes so wills go and create one call this my string is equal to and my sentence and there you go you got yourself a string now you're going to be able to get the length or number of characters inside of your string just by using length so I'm going to go I owe puts and I'll put length inside of here and then I'll go and get that length and to do that you have to type in string followed by length and then you throw in whatever your string name is and I'm going to execute these I'm going to do a bunch of these in animal executed all ones we're also going to be able to compare strings or actually I'm going to show you how to combine strings first so let's go and let's create another string let's call this longer string Antibes on it we're going to go my string and this is how we combine or concatenate strings put a space inside of there two more of those brackets and then I'm going to say is longer there you go now you have a longer string let's also come in here and let's compare string so we'll say that the throw let's see if these are equal to each other aimes you can do all sorts of things inside of here with string interpolation you can execute different things and so forth so I'm going to have an uppercase a version of egg and then I'm going to use three equals what that does is it checks to see if the value as well as the datatype or equal whenever you use three equals you just have to it's going to check if the value is equal and ignore the datatype so I'm going to use lowercase egg here in that situation and we'll run this and let's check out what's going on that the duh once again don't pay attention to little warnings and I'm getting and you can see here the length comes back is 11 and the equality comes back is false why because that's an uppercase e this is lowercase e all right we're also going to be able to come in here and check if a string contains another string so let's output this on screen again so I'm going to say my is my contained inside of here and let's go and verify and check that we're going to use string again and we're going to say contains and you're going to put a question mark here at the end and you're going to say does my string contain the word my and we'll execute that in a second here and you'll be able to see exactly how that works for us you could also return the very first character inside of here so we'll say first character is what we're looking for and we'll say called a string and we'll say first and my string once again we're going to be able to get a character at a specified index so we'll say what is that index for to get that we go string at and then you pass in the string you want to search through and the index specifically that you're looking for you're going to be able to get sub strings so go to substring and then inside of this again we're going to use the string library here or string module and its name is slice and to get it you go my string the string you want to work with the index you want to start at and the number of characters after which that you would like to work with and come in here and we'll execute it and you could see is mine the string true yes it is what is the very first character M what is that index for E and you can see here we started at the third index n this is zero-based so this is going to be zero one two three and then we want to get eight characters one two three four five six seven eight okay so pretty neat stuff we're also going to be able to come in here and split a string into a list and what we're going to use here is inspect and what inspect does is it prints the internal representation of a value so what we're going to do is we are going to split the long string with at the point in which there are spaces inside of it and we're going to convert it into a list more unlist later on but inspect is going to allow us to sort of see what that internal representation of a list looks like we're going to go a longer string and then we're going to define what is going to split all of those guys and second here get a couple more things going and then look at that output you're going to be able to reverse strings so let's go and reverse this guy I'm going to go string and reverse and we'll throw a longer string inside of there we're going to also be able to use uppercase so let's do that this is going to be just all we need to do is come in here and get this and change this to up case we're also going to make all the characters lowercase and to do that we type in down case we're going to be able to capitalize just the very first character and have everything else be lowercase so capitalized and that gets you that and also something else that's kind of neat to be able to do is to know that you're going to be able to pipe a values from one thing to another so let's say I want to multiply four times 10 and then I want to pipe it to a different function we'll do more examples of this later on but basically go like this and let's say I wanted to perform this this Megan you know this code I want to execute this code and I want to pass it to puts so that it outputs it on the screen I can do that and this very functional language and we'll run it and it will execute it and you can see all the different things we did here this is the string the list representation inside of elixir you can see that was printed because we used inspect you can see here we went and reverse everything uppercase lowercase capitalized and then 40 comes back because we went and perform this calculation and passed it to puts okay so that's a one down a lot of the things we can do with strings now I want to show you briefly with some of the things we can do mathematically and there you go I went and put these in here a little bit to save some time we're going to be able to add values of course subtract multiply divide this is going to be division integer division so it's going to get rid of any decimals and REM is the same as modulus it's going to basically give you the remainder of a division so if we come in here and we execute that and we run it you're going to see all of the output right there for the math functions and there are tons of math functions logs and power functions and E and all of different cosine signs and all those other different things I'll leave that to you to go and look up on your own and now what I want to do is jump in and talk about how we can compare values using a lick sir all right so what we're going to do here is we're going to well I'll show you whenever we use just double equal signs and what we're going to do is compare integers to floats and whoops let's get rid of that guy right whoops all right and then what we're going to do without they're like this and then let's compare these different values to each other so I'm going to say for equal to 4.0 and we're going to be able to see the difference between equals and three equals all right so they're like this and it will throw three equals inside of there and then we're going to do a similar thing with not equals not equals has a similar way of doing comparisons so throw that inside of there and then we will do another one not equals with the two equal signs and we'll say that let's come over here compile it and let's run it and you can see right here whenever we use just the two equals sign this comes back as true why it's comparing the values and not the data types and here we are going to be whoops I'm win 10 I have to go in here and do this obviously let's go like that and for that new line inside of there there we go now we're going to get the proper output that we're expecting got ahead of myself a little bit let's run it all right so now you can see we were here just comparing the values and not paying attention the data types that comes back is true here we were comparing the values and the data types well this is an integer this is a float that's what comes back is false here we are just comparing again the values and this is not equals to and here we were comparing the data type as well as the values and that's the reason why that comes back is true all right so pretty neat stuff let's do some more advanced stuff we can also come in here of course and check for greater than and less than and equal to and all those other different things let's change this a little bit let's change this to a 5 and to a 4 and we'll check if this is greater than so come in again 5 greater than 4 and go and check a whole bunch of different things so ducted uh and greater than or equal to of course sub greater than or equal to and here we'll check less than and less than and here we'll check less than or equal to and less than or equal to the disses mobile here executes its run its and there you can see exactly how all those work and I'm doing some more examples here of course let's go get rid of all that we'll talk about logical operators I'm going to create a function here called age sixteen and what I'm going to check for is if this person is going to be able to vote and drive so I'll say votes and drive and inside of here we'll get rid of this and we're going to say if age is greater than or equal to sixteen and I know this is bad logic what I'm doing here I'm just trying to demonstrate our logical operators work and the logical operator we're using here is and so I'm going to go in I'm going to go age is greater than or equal to eighteen and let's do another one it's similar sort of function so we'll go like this and here what we're going to do is we'll do exactly the same thing and it will put or inside of there or and we'll say votes or drive okay so what I'm doing is just providing two ways of checking different values this is going to come back as true if this is true and this is true and this is going to come back as true if either one of them are true so we're going to man here and we'll execute it and we'll run it and you can see indeed how the output works out for those guys and of course the other logical operator that is going to be available to us is not and basically ought not is just going to take whatever is true and turn it into false and whatever is false and turn it into true the and there you can see turned the true into a false alright so there is a whole bunch of different things in regards to how we can compare values now I want to talk about decision making now there are many different ways that we can perform certain actions depending upon different comparisons and so forth and so on I'm going to show you a whole bunch of them here so of course we have if so what we're going to do here is I'm going to say if age is greater than or equal to 18 well then do whatever is inside of these brackets and what we specifically want them to do is to go and output can vote on the screen now let's say we want to check another condition how can we do that well we can just come in here and we can come back out to the side and we'll say else if that's not true in that situation what are we going to do we are going to output that they can't vote all right so that's the ifs and else statements and exactly how they work let's do a couple more here there's also unless within less the code is going to execute unless the age is going to be equal to 18 and we'll do this paste this inside of here and in this situation I'll just say you are not 18 and we'll come in and we'll do an else statements and in this situation we'll say you are 18 and let's do another one we'll do the conditional basically the conditionals going to work like else if and it's very important to understand however that it is only going to hit or produce output on the very first match so what we're going to do here is we're going to say condition du and then we're going to list a whole bunch of different conditions we want to check for so if the age is greater than or equal to 14 then we want to execute specific code and the specific code we want to say is in this situation I'm going to say you can wait let's put that in there and then what's throw it in another condition so we'll say age is greater than or equal to 16 in this situation we'll say you can drive you can drive and then we'll throw in another condition and this is going to be age is greater than or equal to 18 just know that when it hits one of these it so it's going to output this so whenever it hits this guy is going to say you can wait even though the age is set as 16 so putting these in the proper order is very very important so actually I should have put the 18 up here in data but I'm just going to do it this way so yeah let's just come in here and do it this way you can vote and then if you want to have a default action that is always going to be true you just put true inside of there exactly like that and then we'll say default inside of here so we'll say defaults and while we go and run this because we have a whole bunch of different things to check out it throws up all kinds of warnings so don't don't worry about it and execute and you'll see right here for this if-else statement it's going to say you can't vote the age of 16 all right down here for the unless unless it is 18 you're going to see you're not 18 here you're going to see you can wait and the reason why is because this is 16 like I said in actuality let's fix it let's just do it the right way throw that there and then we'll throw I did it that way just so you can see that it will foul up if you don't put these in the proper order so let's go and get rid of that tip data and now we can run it again and you'll see that it does it the right way and you can see here it comes back as you can drive all right so there's even more I'm going to show you more so let's go in here let's get rid of this many different ways to perform actions depending upon conditions there is also going to be the case statements which are going to work like switch in other programming languages or here what we're going to do is we're going to say case two you could have a variable inside of there but I'm just going to put the value of two inside of there just keep it simple here I'm going to say if that value is equal to one in that situation let's go get rid of that I'm going to say entered one and if otherwise it is 2 I'm going to come in and I'm going to say enter 2 and then once again if you want to have a default what we're going to do is just put an underscore inside of there and then we'll go and put something inside of here like default default okay so there are case statements and the very final one is the ternary operator I'm going to show you how to setup a ternary operator inside of a lik sir so what we're going to do is I'm going to say ternary and then inside of here I'm going to say if age is greater than 18 and then I'm going to put a column inside of there then do and you're going to define what you want to be done and otherwise I can put an else statement directly inside of here in that situation I'm going to put can't vote inside of there as the other option that's available to us and we'll come in here and we'll run it and we'll execute it and you can see the ternary operator worked can't vote comes back and you also saw entered to comes back with the case statement okay so a whole bunch of different decision-making tools available for us inside of elixir and now I'm going to take a look at the tuples now normally tuples have two to four values inside of them and they can hold any value however they are not for enumerated through and what I mean the numerating through I mean that you're not supposed to cycle through tuples or go and check for values one by one by one here I'll just show you examples so I'm going to come in here and I'm going to create my stats is equal to I'm going to say 175 and 6.25 and then I'm going to throw an atom inside of here that is Derk now I'm going to output some information on the screen let's say well first off let's check if this is a tuple and to do so we go to pull and then I'm going to check if it's a tuple how do you do that see if you remember is to pull pretty easy to remember and I'm gonna save my stats inside of there and we'll do a couple more things you can also append values to a tuple but remember every time you go and make a change you have to change it and save it to a new variable so I have to go my stats because once a variable receives a value it stays its permanent for forever so you can add these functions you can you can go and add on or append values however you need to create a new storage area for it so I'm going to go my stats and let's say I want to throw 42 into that tuple as well well with append we can do that you can also retrieve values by index so let's say I want to go and get age from it I can get age and to do so you go elements and you pass in my stats to look let's go and throw this in here like that my stats too and I want to get whatever is in the third index again this is going to be 0 1 2 3 this is the one that I append it on there we're going to be able to get the tuple size let's go and get our tuple size size and here you just go to pull once again size you pass in whatever tuple you want to work with inside of there you're also going to be able to come in and delete an index inside of a tuple but once again you're going to have to store it in a new snute on data area so my stats 3 is going to be equal to we're going to say to pull delete at and you're going to pass in whatever tuple you want to work with so this is going to be my stats 2 and I'm going to say that I want to delete the whatever is at the zero index and we can also go and insert at an index we're going to do almost exactly the same thing once again go to create a new storage area so I'm going to go my stats for and it's going to be instead of delete at it's going to be insert at and I'm going to go and have my stats let's change this to my stats 3 and what you need to do here is to find the index you want to work with I'm going to say it's the zero index and then I'm going to say the data that I actually want to input inside of it and we're also going to be able to create a tuple with the values duplicated a certain number of times so let's go and create something called many zeros and do this we'll go to pull and duplicate zero and zero five times so it's going to create a tuple that's going to have five zeros inside of it and finally you're also going to be able to use tuples for pattern matching so I'm going to be able to do something like type in weight and height and name and pull out that data some ago one seventy five and six point two five and Derrick the atom and then we can go don't want to do that I want to get this guy right here yeah just grab that throw that inside of there and we'll change this from size to weights and then we'll just throw a weight inside of there just so you can see how that works so there's weight and we can come over and we can execute it and duck the death and here you can see whoop spelled tuple wrong that's alright you can see up here let's go get rid of that right there of it matters all right so here we're checking is it a tuple yes it comes back it's true here we are appending the age of 42 to it we're then checking the age see it 42 comes back we are then checking the size of our tuple you can see there's four pieces of data inside of it and then down here we went and pulled or assigned a 175 to the weights and then output that on the screen okay so a whole bunch of things we can do with tuples and now we're going to talk about lists all right so lists are going to contain lists of data of any different data type so what I'm going to do is I'm going to create two lists here and you just go and surround the data or whatever values you want inside brackets so I'm going to go list one and list two four five and six and if you'd like to combine two different lists you can do so just go list one then you go put two plus signs and list two there you go now you have a bigger list you could also go and tract a list from the list so we'll go list three and put two minus signs and list one you can verify if an item is inside or list so let's check if there is a six in list for you can separate the head which is the very first item inside a list from the tail which are all the other different items just by going like this so it's a head and tail put some spaces inside there so it's a little bit easier to read and we'll say we're going to perform this operation on list three and let's go in here and let's output the head for this guy so go ahead or the very first item in the list that's what the head is you're going to using this a lot so let's go and get that guy and then let's go and I didn't cover this yet so I'm going to use it you can also output data using right and how right works is it doesn't put a new line after it outputs the data like puts does so that's also something nice to know you can also come in and use inspect to output tail which is going to show a representation of the list inside of here let's go run that and here you can see we come back as true and where that comes from is we checked if there was a six and list for we went and grabbed the very first item out of the list that's the one tail is all the other different items that we have inside there and then here we printed out the tail on the screen I've been using inspect here and want to point something out it is possible you know like I said inspectors going to print the internal representation of a value but it's important to understand that it may give you odd results so let's come in here we'll throw 97 and 98 inside of there and let's see what happens so basically we run it and you're going to see that we get to a B okay it printed character codes inside of there if you want to guarantee that is going to print the list and not character codes we're going to do is put a comments are there character lists and then we say as lists if you do that then you to worry about it outputting bad data and you can see that it printed 97-98 the a B part there is just going to be basically what happens with elixir like our length is whatever the last value inside of the function is is going to pronounce green okay so there's that alright just in case you come across some problems within specs and you want to fix it you're also going to be with lists be able to enumerate over lists and to do so we go Enuma and each I'm going to say tail FN item and we can come in here and output the individual items as we come across them item and then close that off with ends if we run that you're going to see it goes and prints out all those different items let's go and create another list of words so I'm just going to call this words I'm gonna say random words in a list and then let's enumerate through these again cycle through them so I'm gonna go words is going to be what we're cycling through and then we go FN and word and then what I want to do with them as I cycle through them so let's go and output them so I'm just go word and then end that and you can see it goes and outputs all those ok so you can cycle through numbers words doesn't really matter other things kind of neat is we're going to be able to use recursion to loop as well so I'm going to create a function and it's going to be called display list and I'm going to pass it my words list and have it cycle through all this and whenever we're talking about recursion what we basically just mean is we're going to be calling the function from inside of itself so let's go I'm going to show you a whole bunch of examples of recursion I'm just going to give you a brief example here just to show you how to use recursion to cycle through a list so this is display list to make sure you do this you create this new function outside of that area and this guy is going to be passed in the words the whole entire list and what it's going to do is it's going to actually break the head and I'll part right here inside of there I'm going to go do an IO puts and word so it's going to break off the head and it's going to out put it on the screen and then it's going to call itself to display a list again pass in word it's going to break off the head but I output it and so forth and so on one thing is you're going to have to whenever you're using recursion always have an ending a situation in which it no longer calls itself and we're going to be able we're going to give this the same name it's going to be like if the display list is called with a an ability to break off the word like we did here and I put it well then we're going to call this function if however it has ever passed an empty list well then in that situation we're going to use this display list and under that situation what we're going to do is nothing so how do we do nothing well to do nothing you're just going to put a calm inside of there and then we're going to go into and we're going to go nil all right so here we're calling display list passing it inside there and we're going to cycle through a break off the heads pronoun tada rid of that let's run it whoops gave me an error with it's what's the problem here it's saying there's an error on 45 unexpected token and AH whenever I originally created the display list down here the second one it threw another end inside there so we got rid of that and run it and now you can see it also is another way for us to print out all of those different words in the list and we'll do more for Kuragin as a tutorial continues now we're also going to be able to come in here and delete given items from a list so I'm just going to go I Oh puts and show you what it looks like so we'll say display list and then I'm going to go list delete words and I'm going to delete random out of our list and then pass it in to display it on the screen you'll see how that changed and here you can see it doesn't show random in that situation okay so good stuff you can also delete an item at a specific index and to do that you go delete at once again we're going to pass our list inside of here but I'm then going to pass in an index and here I'll say that I want to delete whatever is in the one index and there you can see random analysts say it got rid of the words we can also insert an item at a list and to do that we go insert at words and we'll change this to four and that's going to be at the end of the list and then we'll say we want to put the word gay or something like that and run that and you can see yay shows up there we're also going to be able to get the very first item inside of our list so let's change this a little bit just get rid of that altogether type in list and first and what we're specifically searching for we can also get the last item inside of there so we're going to do pretty much exactly the same thing right there and we'll change this to blast instead of first and the final thing is you can actually create a list of key value tuples so to do that you can say something like name and then Derek put a coma height and 6.25 and look 6.25 and whatever okay so you can go and experiment with that on your own so there's a rundown of lists now I'm going to jump over and talk about maps now a map is going to hold a collection of key value pairs so let's say we want to do something like create a map called capitols what we're going to do here is put a percent sign and then we're going to define our keys and our values so I'm going to say that Alabama is going to be my key and the value assigned to that key is going to be Montgomery okay which is the capital of Alabama and we can go down here to different lines it doesn't matter you can indent whatever everything still works let's go and create another one we'll say Alaska and we'll say Juneau and why don't we go and create another one we'll put the column inside of there and then we'll go Arizona and then let's go and create a value assigned for that and that's going to be Phoenix alright so there you go you just created on map now you're going to be able to retrieve values using a key so we'll say let's say we want to get the capital of Alaska and out put that on our screen we'll say is well we can just go capitals like that and then pass in whatever your key is like do some more stuff let's say you can also use atoms as keys which is nice to know now let's go and copy this and just use atoms instead of strings so we will be able to come in here and we'll change this to capitals too and let's change this to Alabama and then what we're going to do is we're going to put a colon inside of there get rid of this guy all together and we would do that for all of these different guys and let's go and just do it so we'll say Alaska that in there and Arizona : inside of there and now what we'll do is we'll actually access these in a slightly different way so here you go puts and we'll say capital of Arizona is and we'll go and get it like I said you're going to do this in a slightly different way you're going to go capitals too and then follow that with the key so dot Arizona and also you'll be able to insert key values inside of here but once again you have to create brand-new maps so we'll call this capital story and to do so you go dictionary put new and capitals and let's say you want to throw Arkansas inside of here you can do that like this and then follow it up with whatever the value is a little rock alright so got that so out of there and welcome and run it and there we go and you can see how all that information printed out on our screen okay so pretty neat stuff so there's a rundown of maps and now I want to talk about pattern matching now I already talked about pattern matching a little bit I just want to give a brief example here so basically with pattern matching we're going to be able retrieve data from different types of data structures so let's say we want to go lengths like this and then with we're going to be able to sign those to whatever is inside of the data structure which in this situation is a list and then we're going to be able to automatically go and print out the design information just by referencing the variable that we put it inside of you saw this before but not with lists it's also important to know that you're going to be able to work with more complicated systems and what we're going to do whenever we don't care about the data is we're going to put an underscore inside of it and actually what we're going to do here is try to match up a list inside of a list so we're going to have a list that is also going to have a list inside of it so how we match those different guys is we just basically copy exactly what's there so let's say we don't care about what the first item in the list is well there that is but we do care about that item so we throw that inside of there and what we're going to be able to do is go and get that value so we'll just say get numb and come in here and we run it you're going to be able to see there's width as the value of 30 and get numb is 40 which is what it grabbed there okay so neat stuff more with pattern matching as the tutorial continues but I want to also talk about anonymous functions right now now basically an anonymous function has no name and it can be passed to another function which is extremely useful so let's go and create an anonymous function and we'll have it assigned to the variable get some and get some then could be passed into a function and what it's going to receive is an x and y value and what it's going to do is it's going to add x and Y to gather all right that's all yeah now you got an anonymous function now what you're going to be able to do is throw it around just like it is any type of variable so what we're going to do here is we're going to go I uh puts I'm going to say 5 plus 5 is equal to and then we'll be able to reference that we'll go get some and then you're going to put a dot like that and then you're going to pass in whatever the parameters are you want pass to that function oops two of these extra things slipped inside of there so get rid of that and there you go and we're going to do a couple more here so you can see them all X at the same time there is also a shorthand of the above so let's go and create another one of these let's call it get less and what you're going to be able to do here I'm going to model it in much the same way you're going to be able to go like that and then one and negative throw another and sign inside of there and two and that is exactly the same as if you typed all of this stuff out and basically if you wanted to subtract another value you just put in there and go three okay but I just want to keep this - so this is just going to track those values and just provides another different way to see the same thing and a shorthand notation it might not be so obvious so in this situation what we're going to do is we're going to go seven minus six and we'll go call get less and then we're going to pass seven and six inside of it so there seven and there six and we'll save that and let's do another one here I think you can see what's going on here you can also have functions except different numbers of parameters which is very very useful so let's say that we wanted to have a function called add some however it is going to receive an unknown number of attributes passed inside of it so what we want to do is we want to have this work so what I want to do is come down here and type and and what I'm going to define is with the function add some what are we going to do if we receive two values well in that situation what we're going to do is we're going to call IO puts and we are going to add those values together so we're going to go and throw X inside of here plus and then we'll throw a Y inside of there like that is equal to and X plus y alright and in the situation in which we receive three values well we're going to have a solution for that as well we're going to have XY and Z and we're just going to change this to plus and throw the Z inside of there and then let's throw the Z inside of there and we can come down here and we will be able to work with those so to call those functions you just go some like that and then a period and then you're going to throw in whatever your attributes are and then likewise you're going to do exactly the same thing but here we're going to call add some and we're all the only thing when it needs change is the additional value we throw inside of there and you can also set the fault for your parameters let's go and save this and let's execute it and there you can see how all of those out put it on the screen and everything works just perfectly fine one other thing I just want to demonstrate here real quick is you can also set defaults for your parameters for all of your different functions you work with so I'm going to go I'm an output here first a function called do it and I then have to create do it and we'll come down here we'll just create do it so we go define and do it and if you want to have default values of all you're going to do is go X which is going to be your attribute that's passed inside of here two backslashes in a one that's going to give X a default value of one it's going throw another one inside of here let's go and give Y default value of one and then we'll go do and then in this situation we go X plus y and we'll come over here and we will execute this guy and there you can see the two comes back all right so a rundown of how we can use default values how we can use anonymous functions and a whole bunch of our different things and now I'd like to talk about recursion all right so as you saw previously recursion is just a situation in which a function is going to call itself so how we normally demonstrate recursion is through the use of factorials so here what I'm going to do is I'm going to call a factorial function that we're going to create here in a second so we go to factorial of 3 and then we're going to call our function which we're going to create here in a moment I already showed you how to use recursion to loop through different lists and things like that but let's go and create some more things so how we're going to demonstrate let's go and put the factorial function right here we're going to create a new function obviously and we're going to it's factorial and it's we're going to receive a number and throw the do inside of there and what it's going to do is we have to have a situation in which we're going to end the calling of our function and to do that I'm going to say if the number is less than or equal to 1 well then I'm going to return a value of 1 otherwise go through an else inside of there I'm going to calculate our factorial so we'll go results is equal to num times and then we'll call factorial which is the function call in itself and that's going to be num minus 1 as you can see eventually num is going to get a value of 1 because we were subtracting it no matter how big we may get and then if we want to return a value we just put the value itself inside of there that's it very very simple factorial now let's run it and then I'll explain exactly what's going on so this let's run that and there you can see factorial of 3 is going to be 24 oh I died put 4 there and input by put this right here let's save it and let's run again all right so where we go and factorial 3 is going to be equal to 6 now let's do 4 instead because I think will just give us a little bit better idea what's going on throw it in run it and run it ok so how did it calculate that well basically what happens here is we call our factorial function and we have a value of 4 inside of here so you get a value of 4 right here times factorial 3 this is it so 4 comes in the 4 goes down to here and we multiply that times factorial of 3 what's that what then happens will we jump down to this line we take 3 times factorial 2 factorial 2 jumps us down here 2 times the factorial of 1 well there's a value of 1 it says if number is less than or equal to 1 return a 1 okay so this is 1 so here what we're calculating is 2 times 1 2 times 1 is equal to 2 this 2 jumps up here jumps there 3 times 2 is equal to 6 we take this 6 throw it up here then we take 4 times 6 is equal to 24 and that is an example of how we can calculate a factorial using recursion and now what I'd like to do is is demonstrate how looping works inside of a lik sir okay so with most languages we loop by incrementing or decrementing a valuable however since all variables are immutable inside of a lik cert we're going to use recursion to loop and I'm going to give you a demonstration how this works so I put and I'm going to say some and what I want to do is I want to sum all of the different values that are inside of a list how do we do that well we can show you right here so I'm going to go and get one two and three and using recursion Aleks or I'm going to show you how to easily sum all of these values and then after that I'd also like to show you how to create a loop in lick sir so I'm just going to call this function loop that is my name it is a built-in thing inside of elixir and now I'm going to jump down here and create both those functions so let's come down inside of here outside of our main little function and we'll go and sum all the values so what we need to do any time we use recursion is we need a situation in which we will no longer continue calling a function and we're going to define that if our sum function receives an empty list in that situation it is going to cease to call itself and instead it's going to return a value of zero now we will create our sum function that is going to work for all their situations and we're going to say if we get a list inside of here that is not empty we want to get to the head and the tail and then what we want to do with it is we want to go to do and we want to say H plus and then we want to call the function back again passing in the tail and we'll continue doing that until the list is empty and in that situation once we will return a zero ok so you see up here we passed in the values of one two and three come up here well get me in here ignore all the red stuff just jump up here and says there's an error at ten oh it's giving it all right so let's go and create our loop function or better yet did I tell you what comments are that's how you use comments you throw in a hash symbol all right so let's give in here run it again and there you can see the sum is equal to six okay so the sum functions work all right pretty neat stuff so now what we're going to do is have a little loop work there and come down here and we'll leave some in there looks nice now to create a loop what we're going to do once again is we are going to say when we are going to leave or loop recursion calling functions and when that's going to happen is when a value of zero gets passed inside of here I don't care about what the other thing is some throwing underscore inside of there and I'm going to go do and then whenever this I'm going to return and nil now I'm going to create the meet of our loop function that is going to loop through what it's going to do is it's going to receive a max value and a minimum value we're going to be doing some really neat things with this later on whenever we talk about concurrency all right so then what I'm going to do inside here is I'm going to say if max ever reaches a situation in which it is a lesser than men well in that situation I am going to come in here and I'm going to say loop call the loop function and pass in a zero that's going to call this guy and that's going to end everything it doesn't matter what I put inside here but I'm gonna put min inside of there just to do something otherwise I'm going to say else and in that situation I'm going to output whatever is inside of there so this function is basically going to receive a maximum value and a minimum value and it's going to little go and count through those until there's nothing more to do so I'm gonna get numb and you're gonna be able to cycle through the lists or do whatever you'd like I'm going to talk about more about a numeral so here in a moment so I'm going to go max and then I'm going to call the loop function again and take max minus 1 and pass in min once again so what's this saying it's saying it once account from 1 to 5 maximum number minimum number so let's save that over here and we'll run it and there you can see it goes and starts out of 5 and decrements down until it gets a value of 1 and then it leaves ok so an example of how we can a loop with a lick sir and now what I want to do is talk more about enumerables ok so now let's talk about enumerables and what we mean by enumerable is a data type that is going to allow its items to be counted off so what we're going to do here is we're going to check if all values are even inside of a list sounds like something would be pretty complicated but it's pretty not propagated so what we're going to do is I'm going to go even lists and lists are going to be a situation a data type that is going to be able to be cycled through so to do so what we need to do is go eeen um all make sure you put a dot inside of there all in question mark and then we're going to throw in a list it's going to be one two and three and then after that this lets go down in the next line we need to define what we're going to do inside of here so you know FN there we go N and for each of these different items inside of our list we're going to go with the remainder N and two so we're going to take the value of each of the list values and we're going to use the remainder function on them versus two and we're going to check if they are equal to zero if they are if this condition right here is true then they're going to go in our new list and print them out if not they're going to just simply be disregarded and then what we need to do here is throw an end in there to end or function it's a pretty simple straightforward to run it and you can see even list comes back as false and what we're doing here is just checking whether all of the values inside of our list are even or they're not we'll get into how we can actually output the actual items we could also let's go and check let's do a copy of this now let's just change it so let's check if any of them are even okay all we needed to do there was just change that to any and we can run this and there you can see it comes back is true we could then go in and print out each of those different items if we would like so it's going to do this and when you go a gnome each and we will throw in our list one two and three and then we define what we want to do with them we go and say each of the items inside of our list we you want to come in here and output so we're going to call puts so we'll just pass that over into the puts function and end and then throw end inside of there that's how easy it is to cycle through all those different guys and print them out on the screen you can see nice neat let's say we wanted to double each value in a list we could do that as well let's go double list is equal to we're going to call enum and map and pass in one two and three our list that we're going to work with here jump outside of there we're going to define F n and each of the individual items in our list and then we're going to define what exactly we want to do with each of those items inside of our list and let's say let's do something even more complicated let's say we wanted to reduce down to a single value so in this situation we want to add all of these values together we can do that so let's say when let's call this some values equal to e num and reduce and we'll pass in the same function here again one two and three after that we then want to define what's going on so we're going to sum all the values and store them in the variable sum as we cycle through so we'll go n plus sum and end you can see just how quickly you can do things here an elixir that are kind of complicated in other languages and let's go an output or some and that's going to be some values and then let's also come in here and you'd also be able to use a function called unique in the enumerable module that's going to spit back a list that is only going to contain one of or no duplicates inside the list so we'll go you know and unique and then we'll throw some duplications inside of there just so you can see what that looks like one two and two all right and we do this and we'll do this and we'll come in and we'll run it and you can see right here we were able to print out all this different items that's done with this guy you can see we were able to cycle through and some all those different values together or actually I didn't print that out let's go and print that out so I don't want to use puts here I want to use inspect or yes inspect with this one so I'll go I Oh inspect and double list and we'll run out again and there you can say went and doubled all the values in the list you can see reduced was used here to add all those values and condense them down into one value and you can see unique was used here to get rid of any duplication inside of our list okay so there is an example of enumerables and some of the neat things you do with them I'm going to show you even more neat things whenever we talk about the list comprehensions okay list comprehensions are going to provide us with an additional way to perform actions on lists so let's say we want to do what we did previously we want to double every list item so we're going to go and create a double list and to do so we're going to go for and and then we'll throw in whatever our list is obviously the list could be a variable name and we go do and then we're going to say what we want to do with each of the items we want to multiply times two that's just what we want to do all right we can go and I'll put this on our screen and there's our double list let's go and do a couple left let's go and see what that looks like and there you can see doubled all that for us pretty neat let's say we wanted to create a list of even values do that and somewhat easier way so we'll go in and then we're going to pass in the let's get one two three and four this is going to spit out only evens so here's going to be our condition we're going to say the remainder of n each of the list items divided by two is going to be equal to zero so if it meets that condition it goes in our new list if it doesn't well it doesn't so we'll do like that and we'll go I own spooked again and we'll spit that guy out and that's going to be our even list there is and we can run it and we can see there's our evens only and I think that's actually probably enough list comprehensions just play around with it you'll get it and they are very very awful and very very useful all right let's talk about something else that's useful exception handling now exception handling is just going to allow us to handle errors and what I'm going to do here is I'm going to do a very very simple example you can go on your own and experiment and test some other things out now anytime you think you're going to do something it could potentially cause an error what you're going to use is a try block inside of it and I'm going to store the error inside of here so that we can output some information about what went wrong I'm going to force a division by zero now that is going to cause an error so if I want to rescue myself from that potential error what I'm going to do is throw rescue inside of there and I'm going to put the exception that is going to be triggered whenever that occurs I leave that to you to go and look up all those different potential exceptions and what I'm going to do here is I'm going to say that if we hit this I want to go and put an error on the screen but not crash the rest of my program so I'm going to do that and then we're going to end we're going to get out of there and then I am going to output that error message on our screen and then we'll move on in our program won't crash so just output that to do here we'll run it and there you can see can't divide by zero and however we would be able to continue going on and outputting additional information okay I know very very brief explanation exception handling deserves more time but I'm trying to keep this short and I want to jump over now and talk about concurrency now concurrency is extremely powerful basically it is going to allow us to run multiple blocks of code at once and how you do it is you call a function called spawn which is going to start a new process that runs the code specified at an undefined time so what I'm going to do is I'm going to use our loop function down here to create two separate processes that are run at the same exact time and output information on our screen and this is how easy it is to do to create a new process you just go spawn I'm going to go function like that and then we go draw an arrow and I'm going to define what I want to occur in this process on account from fifty to one and then that is going to end that now if I want to create a whole new process I can create thousands of processes and elixir is very very good at splitting up this time so I'm going to go and spawn another process this guy is instead going to count from 100 down to 50 circular we are going to be able to see how these are going to work so I'm going to save that and let's come over here okay let's run it and you're going to see now what happens is each of these processes gets a paid number or a process ID so we're calling this guy 50 it's counting from 50 to 1 this one's counting from 1 to 50 and you can see how they jump back and forth sharing time each giving each other time to do all of their work all on the screen so these processes are working at the same time and taking turns and doing everything that you could ever want them to do without causing any problems Lix ER is fantastic doing this we're also going to be able to send messages between processes so what I want to do here is I want to actually send a match a message to this process to this do stuff process how can I do that we could send you know messages done anything so what I'm going to do here is I'm going to say send self so I'm sending this message to myself and what I want to do here is I want to pass in French and add them and I'm going to pass in the word bulb you're going to see what happens here I'm going to be able in all my processes I'm going to be able to receive different messages so we can communicate back and forth between processes and I could do an entire tutorial on how we can do awesome things with this but like I said this is just a very very brief explanation so I'm going to say if they receive the atom of German in that situation what we're going to do is we're going to go I owe puts and we are going to go gutentag and we are going to output whatever they passed in for our name and now what I want to do is I want to and this is exactly how it works you have a normally a key here and then values that are passed inside so let's go and do a couple more of these we'll say if they pass in French inside of there we're going to do the same thing except we're going to change this to Bonjour and there that is and if they pass in English we are going to respond with hello so there's hello whoops Bell hello right and there that is and let's do one more thing here before we finish up the entire tutorial if there is no matching message what we're going to be able to do is issue a timeout let's say we wanted to do a timeout for say 500 milliseconds we could do that and we just want to come down here or go before the ends and outside of here let's go down the next line and I'm going to say after 500 milliseconds if there is no response well in that situation I'm going to say puts and whoops time up and that's going to handle that condition and what you didn't understand what was going on all right so let's run that whoops hit run button run that and run that and where to go oh I know it went and actually executed ahead of time so here you can see Bonjour Bob comes through we actually passed a message inside of there and randomly you know this could be down here a couple more lines or whatever but you can see exactly how concurrency works and I think that is a pretty good jumping-off point for you to go in there and explore with elixir more about concurrency and all of the other different things that I covered in this tutorial so hopefully you guys enjoyed that that is a lot of information and like always please leave your questions and comments below otherwise till next time
Info
Channel: Derek Banas
Views: 154,012
Rating: 4.8831263 out of 5
Keywords: Elixir Tutorial, Elixir, Elixir Programming, Learn Elixir
Id: pBNOavRoNL0
Channel Id: undefined
Length: 66min 1sec (3961 seconds)
Published: Wed Apr 19 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.