Intro to Elixir | Lessons 10: Enum and Anonymous Functions

Transcript

English (Auto-generated)

Hello. Friends. Um Today we're going to learn about a very powerful library in a lecture called in um So within um it's a built in library with elixirs I'll put a little link for you here, you can find documentation and the minimum library here. So why shouldn't I just put it here for now? That's not a specific function. So you know, is a library that works on innumerable. We'll talk about protocols in a in a later lesson but think about it this way for now it works on lists and maps from the things we learn. There's also a data structure called a range we haven't covered yet. Um We will encounter it later but for now at least lists and maps are the two innumerable that we could use the library with. Now if you recall when we used let's say when we worked with list, we had the list library that we could use and those functions apply only to lists in um maps. We looked also at the map library and it has a set of functions that work on maps only now is the in um has a lot more functions very powerful and they work on list and maps, not all the functions do. Some functions work only on one versus the other, but many of them work on lists and maps. So show you an example here. Um Let's say I do not count a very simple method here that is let's say I give it a list and then it gives you 33 elements if I apply in um on and that let's say you have to Oh in on the account, what mistake did I make here? Well I put see here there should be some value. So um it gave me back to because it has it has two key value pairs, so not just in on the account, worked on both the lists and the month. Um I would say, you know, in many cases I would look in the library if I need a particular function to do a particular thing as you can tell here there's there's so many functions that do all kinds of kind of things. Let's try some other one here. So fetch. So a list uh fetching a number from the list. So you know that fetch. So this gives me a two pole that got that returns the item with index two of the list which is three. So it gave me Okay. And then three, by the way, as I mentioned earlier, you will notice this pattern a lot in elixir where return values are a tuple, The first one tells you whether it succeeded or not and the second element is What you expect to return. So let's say here I try something like 15 which doesn't exist and I get a single atom return which says error. So here you could either do pattern matching or you could um you know do conditional as you will learn later to check the output result of this function, return and then do something accordingly. Um awesome. So this is another example here filter. Um Again, I'm not going to go through all the functions I put in some homework for you that you could go and explore different functions from the imam library yourself. But one thing I want to introduce here is what you might see right there, which is um anonymous functions. Um so I'm going to go and write some notes here so I could find more information about it. Very powerful library that you can use with lists ah and maps from what we've learned so far. Okay, um yeah, again, you know, anytime you need an operation I would recommend you just go to in um um it's hard to go a day quoting elixir without actually utilizing this library. Now we've learned to do functions. We have learned to define functions within libraries. I'm sorry. In modules for example, um I have uh let's say death module helper methods, Let's say I define a function here. I call it at five. That takes an element acts and then what it does this basically returns X plus five. Okay, this is an example of a function we learned how to define within a module. Let me put it here. And if you recall here, we could call this method by putting the module name dot the function name and then give it the number, let's say five here and then I get five plus five, which is 10. If I give it 10 I'm going to get 15 because then plus five is 15 now. Um in some cases you need to define anonymous functions um and or set a function to a variable. You could do so an elixir using this syntax let's say I want to define a function, think of it on the fly to do add. So I can do something like this, I can say add five equals you could do you use this syntax which is F N and then you give it the parameter which is let's say like politics and then use this um you know, you could call it thin arrow, like dash and then greater than sign and then put the function definition right here and then put end. So that defines the anonymous function. So let me put it here for you and then to call it, you will have to use the dots in tax. How's it? So you can just I mean intuitively you would think that you're able to call it something like at five and then give it a 10. But actually, or like I say 19, if you try that, it will go an error. It says undefined function at nine before you do it. You put the dot syntax right here. So basically you execute this variable is think of it as a as a function and then you pass it this parameter 19. So you would actually have to use the starts in tax like this. Um you can also simplify this by using the distant tax by the way like notice here you know, even though you could have multi lines I mean I would recommend reserving this for functions that are like one or a few lines. If a function is big I would recommend still putting it in a module and I will show you basically how we could use all these different types of syntax with elixir. So if I go here and I could say I had five This I call it version two. This one here maybe a little bit you know it might take you a little bit of time to kind of digest this type of syntax but I think with time you'll get used to it and you actually like it because it makes you called Super concise. So to start the function definition you put this and percent signs and then you open these brackets and you actually put the definition right there. But there's also special syntax here which is referring to the parameter. So I say here I'm defining a function that takes the first parameter and this is defined by 1% one And then plus five. Okay so this is this does exactly what this does. Okay which is defining a function that takes in the first parameter And then you add 5 to it. If you're taking more than one parameters then you do something like this to. Okay, so you do the dysfunction can take as many parameters as you want. So let me show you this here to add five was too And then I put. let's say I put 12 and then you get 17. So exactly the same results. I noticed how this is um a very concise way to do what we did right here and and and also concise for what we did here, which is putting everything in a module on a function definition. Now again, we could use all these three variations in elixir and in different parts of your app you're actually going to use all these different variations. So let me show you here as an example. So a common function that use a lot is mapped and you've probably encountered it in a previous programming language used which is in this case let's say we're mapping a list. What we do is we return a list but then we apply a function to each element of the list. So in this case here again, just reviewing the syntax, we just did we pass in an anonymous function that takes in an X and multiplies each X by two. So we re journalist with each element multiplied by two. So in this case here we get 246 for a list of 1, 2, 3. Now let me apply the same thing here to what I did in this parties. So I'll start by doing in on that map you say I take 123 and then I could do function X. I just use the X-plus five as an example. And so what happens here? I basically get a new list with each element has five attitude. So I'll copy this code right here. So here are ways we can apply functions with names by the way these are don't just have to be within arms. Uh and actually anonymous functions have nothing to do with the names. I'm just introducing them with enemies because they're widely used within arms. So just for practical reasons, I think they're, you know, it's a good idea to teach them together so you can do something like this. Okay, so let's say here I want to use this module function. You can actually use this module function but also there's special syntax for it. The way I do it I do plan on that map. 123 and then I have to pass in the function and the way you have to pass in the function, you have to use this 1% sign and then you do helper methods dot at five and you have to tell it the charity. Remember how we could define functions in elixir that can take different arguments. That's why you have to tell elixir that I'm pointing to the method that takes in a single argument, which is the, you know, just but it's the only one we have but assume we have like when the text in X. Y. And then it adds xy together and adds five to them just for $40. Um argument's sake. Then you have to pass in the one that has to but then you have to give it the second argument. So again I'm using this percent sign, I give it the name of the function and then I pass in this forward slash one to basically tell it that we are actually wanting to point to the function of our 81. Uh so which means that it's going to apply that function to each element of the map. Alright so let's try another one here. So let's say I go again in on that map 123 And then I go at 5 to again I get the same results, the one that you're like if you really if that is really what you want to do in practical elixir, the one you're most likely to do is this one right here Which is the one we I saw earlier the concise method. So this one right here, awesome. So this is what I wanted to cover today. The imam library is very rich and actually I'll spend a number of lessons just doing a variety of exercises. I know when I first started learning elixir, it took me a while to get my head around all these different ways you could apply functions to say, you know, or, you know, two different libraries, so we'll spend a good amount of time practicing different things. Thank you for tuning in and see you in the next lesson.
333 Views 0 Likes 0 Comments

In the lesson, we learn about the powerful Enum library and we learn how to define and use anonymous functions.

Comment
Leave a comment (supports markdown format)