Intro to Elixir - Lessons 11 | The Awesome |> Operator


English (Auto-generated)

for continuing this journey with me to learn about elixir. Today, we're going to learn about the awesome pipe operator um in many electrical lessons and tutorials. This is introduced quite early on because it's the kind of operator that you're going to use on a daily basis in your elixir day to day coding. The reason I took some time to introduce it is because I was hoping to cover as many of the building blocks within X, sir, uh and then get to a point where we can make more interesting use of this operator. Think of this operator is a way to wire a lot of the things you've learned together in a very interesting way. So we've learned about different data structure, We learned about pattern matching, we've learned about functions and most importantly we learned about your mutability. So let me start by introducing today the pipe pipe operator. So let's say I would like to do something like, you know, let's say something quite simple. Let's say I have a string, my string says something like hello, but maybe let's say, you know, I'm taking user input and they could sometimes add trailing or starting spaces. So one way to remove the beginning and ending spaces is by doing trim on the string library so I can do string that trim string, you know what I'll do? I'll just call it as to avoid naming confusion. So I do string dot trim s and that gives me basically a new string that has the beginning and training spaces all removed. Now what would be interesting is to also be able, let's say I want to also capitalize it. So I can do something like string dot I mean what you what you would do normally you have multiple ways to do it. So you could do something like here. Let's say S. Equals space space. Hello. And then Space You could do something like you know s. one equals string. Let's say if you want to apply multiple functions on a value such as a string, you can follow multiple approaches. So in this case here this approach I'm showing you here is you can simply store the results. So let's say I do trim so I get here basically or I can call it here trim desk and this is the trim this and then I could say something like um you know cap trimmed act as I could say well take the trimmed act and to induce string that capitalizes and then trimmed F. So this way here um I'll just oops just paste in the code here. Um What mistake did I make here? Alright sorry my bad. So in this case here this has to take the S. As the first argument. So again I'll re execute this code so I have asked and then I set a variable trimmed S. Which applies string that trim to the S. So it removed the beginning and trailing spaces and then I do capped trimmed s capitalize the term S. If I want to do even say something you know further let's say I want to do like you know there's there's a lot of functions that we could do um something like I don't know what we could do some pick some simple message that we could um you could apply here possibly reverse so I could do something reverse trimmed S equals string dot reverse capt from death again. I'll repeat all this code for you here go back to my ex. Alright and then I get the reversed trimmed as I misnamed that variable. Alright so as you can tell here this this is getting pretty cumbersome. So another approach let's call it approach to so let let me just call this Approach one is to basically use variables and just keep doing that in some cases. By the way this could be useful as as we will see later uh for for different reasons but if that's all we need to do here another approach is to call the function on the return value. So we can do something like I can just use reverse trimmed value equals string rivers. And then I open bracket, I simply put here script that capitalized open bracket and then string the trim close the bracket, close bracket close bracket. Uh So that accomplishes the same thing by calling the function on the return value remember here. So I just have to make sure that this returns strength for the reverse and the same thing. The internal of this one here. I just have to make sure it returns the strength now these cases this is true. So if I do this here, I get the exact same result. I'm not sure about you here but I find this a little bit hard to read because you're getting, you know, a little bit um you know nested, I would say like a nested function calls, I think that's probably the best way to describe this. And I tend to find this pretty pretty hard to read The approach three which is the preferred approach, an elixir is to use the awesome operator which is the pipe operator. And the way you do it here is you basically say well I want to do straight dot let me define my string here every time. In case we wanna copy paste the code. So string dot trim and then s and then pass it as the first value. Using this pipe operator to string dot capitalize and then pass the result of this as the return value to my last message call which is string doc rivers. I tend to find this a lot easier to read. And actually even this first you the first element is actually preferred that it is a value. So I put s and then passes as the first argument for uh string dot trim which passed as the first parameter for string that capitalized and then past that as the first a value as the first parameter for string that reverse and then you end up getting the same outcome, the same result. This is a lot easier to read in my opinion and I think in the opinions of many of the elixir debts and in many cases you could actually write it like that, you could write it on multiple lines, which make it easier and easier to read. So when you look at this and be like okay I'm taking us, I'm doing a trim on it, then I'm doing to capitalize on it. Then I'm doing reverse on. Okay to help you visualize this a little bit let me basically kind of sketch a little bit what this may look like. So if you have a function here that takes in let's say. And by the way the functions could take different I picked, I intentionally picked easy functions here because because I just wanted to um illustrate this But let's see here, I have basically function one function to and then function three and they could take different parameters. So let's say this, this one takes to this one takes one and this one this it takes three, they don't all have to take the same amount of parameters. And then uh let's say here's the key thing with the pipe operator, sorry with the pipe operator. The key thing here is you have to know that the return of one function should be the input of the first argument. Even if there's multiple arguments, the key thing here is the return of the function should always be the first argument for the next function. So in our case this was trend. Again, it doesn't make sense in this particular drawing because they all take a single argument but just to illustrate trim and then here capitalized and then the last one here was reversed and then you can take basically the final return value here. This is the actual one that you mostly need. And also remember that for the first function you need to make sure you pass in the first input doesn't always have to be one but so here first input to the function. Okay, so the whole idea is I'm taking a value passing it as the first argument and then the output of this will be the first argument of this and then the first argument of this, the return of this will be the first argument of that. So I get the output at the end. This kind of thing opens up a lot of opportunities for us because now we could basically put a lot of the things together and as long as we know what is being returned, then we are quite sure that well, you know like um we could do something with it. So I mean let's let's let me give you another example. So let's see here, I'll call this example to so let's say I have a map, I call it my map. And let's say this map is basically analysts temperatures. I'll make something up. I'll say something like Vancouver 15. I will say something like um Toronto 25 and then let's see Halifax 20, something like that. So cool. Yeah, I go to I X. So what I can do if you recall like for example with my map, I could use the map library and I could get something, let's say I want to get the keys here so I can do something like this. I could say well I'll take my map and then I will pass this as the first argument to map dot keys. Now, what do I get here? I get all the kids. Okay, so what are all these keys in this case? These keys are? Sorry the the keys function returns a list and this list basically have a In this case three elements. Each element is an atom. Okay, so what else can I do here? Well let's say I want to take those atoms and I want to convert them to strings. So let's see if we can do this from the atom library. So atom 0.2. Here you go to string but because we want to apply to each one of those elements, would you happen to remember? How can I take, how can I apply a function to each element of a particular. Mr I'll give you a couple of seconds to remember if the first thing that comes to mind is the imam library then you're correct. Remember here I've given you a hint, last lesson is that I always think of the name Library and then the map was in the email um library in this particular case, but think of the name. Library is one of the first go to when you speak of functions to do so in this case here, you know, knowing that the keys is um returns the list. So let's go ahead here. And if you recall here, you know in a list but I should say in um elixir because Enaam exists in different programming languages. So if you remember a map here, the map message, it takes an innumerable which can be a list and then we can apply a function to it. Okay, so if you recall we could do something like this, not the keys. And then here I could actually do in um now remember here the output from keys which is a list can be the first argument for in on that map which is this one right here and let me apply a function of it of some sort. So function and let's say I call it city see here and then I apply adam that two string, see and then I closed that I've missed the end here and now I've converted each atom. Two strength. Okay, um if you recall I will actually copy this for you in your notes. So you could have access to it later. Now remember here if we're just applying a simple method to each element of the map, there is a nicer shortcut for it. So I'll duplicate this line here. But then what I can do here, I could say something. Well I have referenced the adam dot map and because it is the first argument that I do just one like this. So oops, here you go. So again, as I mentioned, last lesson may take a little bit of time to get your head around this intact and it certainly took for me. But these two are equivalent. This line on this line are equivalent. So if all you're doing is taking a function of the single argument and applying a single function on it which is to string here. You can simply put this m percent sign here and reference those arguments by doing 1% 1234 but in this case there's only one. So I just put 1% 1. Now let's say I want to capitalize each one of them. Well simply again change another function. So um maybe I'll start to put those on separate lines so they're easier to read. So I'm taking the keys of this map um doing this year and then let's say I want to capitalize here as you might have guessed I simply do string that capitalize each one of these. Oops. Unfortunately in I X, they have to be on a single line. I'll format it nicer a little bit later, but just so we can see the results right away. So we're getting here Halifax, Toronto Vancouver, uh and it's basically doing, doing doing it like that. So as you can tell here, you can keep changing functions as you wish to do as you want to apply more and more things to it. So this is what makes this very, very powerful. So I will put here and I can call this something like capitalist cities equals. It's generally good to align it like this even though you don't have to. So again here, um you're starting here to get the real feel of functional programming at the core functional programming. It's really having functions that call functions, that call functions, that call functions. Um because we don't have the concept of objects. I mean, if you were coming from, let's say, an object oriented programming, you're probably used to doing something like this, like in ruby, you could simply do like string s dot let's say capitalize capitalized dot trim dot reverse something like this, right? You're used to doing something like this in in programming languages, in javascript, you could probably do something of that sort, for instance, in functional programming. The way we do things generally is you have assisting elixir, I should say, is is you have module and then functions and then you pass arguments to this which makes this operator extremely helpful because all this does it makes it convenient to pass in arguments to functions and then the output of this. You pass it to another function and so on. So back to the picture here, you will see this in every program you will write is you're basically the functions that return values that gets as an input for a second function. The return value that gets an input for a third function for function and so on until you get the desired output awesome. So hopefully you've gotten a good idea about the pipe operator or this awesome operator in elixir and then um how to encourage you to do the homework which again just practicing with different types of data structures to use this operator. Indeed, what we will do for the next number of lessons will go through many drills to just practice putting all the stuff we've learned together and wire them using the pipe operator to do some interesting stuff with the lecture. Thank you for joining in. And I'll see you in the next lesson
48 Views 0 Likes 0 Comments

We learn how to use the awesome `|>` operator to chain functions together.

Leave a comment (supports markdown format)