Intro to Elixir | Lessons 12: Drills and Tips - Part 1

Elixir
Transcript

English (Auto-generated)

Hello Friends. Thank you for continuing the series on elixir and for today and for the next number of lessons we're going to do a number of drills and what we're doing those drills that will be giving you some tips and techniques that you could use in your elixir programming. Um We've learned a lot of things so far actually. And what's interesting now is that is to learn how to put it all together to solve particular problems. So today's problem is, you know, something that will actually need in a lot of apps, which is um um for example, given a string you want to title is the string, which means you want to capitalize the first letter of every word in, in in the in a given string. So um this question we, you know, we're asked to basically build a function within a module that accomplishes this. So let's dive straight in and look into writing a possible citizen. One of the things I want to mention is that, you know, the solutions I'm gonna give, you aren't necessarily the most efficient or or the most performant um that said, you know, the goal here is to learn about elixir to get comfortable using elixir primarily using the pipe operator and using the minimum library because these are the two you're going to be using a lot in your elixir coding. So um I'm gonna write it in this uh notes and defiles. I'll go here uh solution? All right. So if you as you might remember, would you find a module in elixir by using the module and then I'll call it string helpers and real life elixir, by the way, you would put modules in their own file that have the you know, thought the ex extension, we'll talk a little bit more about that later. I'm just writing it here for the simplicity of having everything combined with with our notes. And then let's go ahead and basically define our function here. We can call it satellites and um we can do something like uh takes in a string and then do Alright, so how do we solve this particular problem? Well, you know, when we have a string, I mean this isn't really unique to elixir but usually when you have a string and you want to deal with words in a string, it's quite common to split that string to take it and take every word and put it in some kind of a um um a data structure that have contiguous ordered elements. I mean the one that should come to mind right away, there should be at least um we talked that like, lists are preferred in elixir because they used link list data structure under the hood which is efficient to use in functional programming languages. So um we prefer that using two poles which are really used for storing small amounts of data, usually a few a few items. So um if you wanna know, like Okay, so I do hear strings and excerpts um if you are willing to work with strings, remember, strings are not innumerable. So which means we cannot really use the imam library, which is usually the good go to library for us when we're dealing with with elixir innumerable, such as lists or two poles um Mhm lists and maps and and ranges for example, not to polls. Um So for those ones we we we we revert to the library, but for other ones we actually go to the data type library itself. So in this case it's it's a string and you know what I usually do actually, I sometimes just browse these functions right here. I mean of course you can search google but I like to get comfortable uh reading the documentation and like I said before, Alexa has really well written well documented um documentation. So if you notice here there is this split function in the string library in elixir which has defied divides a string into parts based on a pattern. So you give it a string, you give it a pattern and then it divides that part, that that string into into the parts. So most languages have this method, this is why I'm familiar with it, but if you're not familiar you could try to search um They are all trying to get the method also if you remember if you're an I e x, you could do a string and then you hit dot and then you hit the tab key and then it will print out all these methods. All these functions sorry for you that you could um, you know, in many cases guess right. What what it does in this case, for example, you could guess what split does. You could guess what you're reverse starts with replace stars and so on. So in this case here we could do string and then remember we could use the pipe operator here. The reason why I'm starting with this because I know I'm gonna end up calling a number of methods. If the only thing you're doing is spreading, no need to use the pipe operator. If you're calling a single function like string split, uh you know, pass in the string then you wouldn't really need the pipe operator. It's kind of an overkill. Honestly, if you're just calling a single method, but once you start to call at least two or more, then it makes more sense to use the pipe operator. So the only reason I'm doing it because I know I'm gonna do something more than split for this particular problem. So let's go ahead and try it actually. So I'm gonna store this string example that we used into a variable here, I'll just call it string and then I'll try spring string that split and ask string to it. And as you can tell here by default, it splits the string by spaces. It's actually the most common thing. Um you could pass to it let's say I want to split it by the letter E. And then it treats E as if is the space per se. And then notice here how like this he becomes kind of like the space so it's split the string based on those letter is not as common use case. Now you could you could also you know do whatever patterns you want. And I believe you could even pass in um like a rejects regular expressions uh in this pattern if you want to need some more uh sophisticated kind of pattern. But in our case just calling string the split and pass in the string which is again equivalent from what you might recall to do in string and then the pipe operator like this and that will basically take the string and split it into a list of of um to double check it's a list. You could always just call is list. It gives me true. So we are getting a list from this split message right here. So we we have a list of individual words from that strength awesome. So what is the next step for us here? Well now that we have a list of words so I'll copy it here for your just for your reference, we want to go through every one of those elements and we want to capitalize it. Okay so we know or we might remember I should say from the from the strings lesson that we could do string that capitalized which makes the first letter uppercase and lowers the rest of the string. Um For example we could do hello and then it returns hello. Um even if you have like another capital letter here, it will actually locate it. So it will capitalize the first one and then low cases the rest. So we could just capitalize but it doesn't work on the list. So because string library really works on strings and not on um an entire list. So what we need to do here. So this is a pattern when you want to apply a method to every element in a particular list um all of which the same type in this particular case which makes it easier. I mean even if they're not the same type as you might recall, we could use pattern matching to do different things depending on the type but in this particular case they're all of the same type. So what we can do, we could use the map function. And you might be familiar with the map, you could use it with a raise or different things in different languages. It is common to use Map and map comes in the name library. If you remember in um we could use it with lists, maps ranges. So we could use map. So in um a lecture. So if you recall here just for your memory, the not function comes in different flavors but the most basic one is that you give it the first argument as a list as an example or a keyword list in this case. And then the second argument you give it as a function which is what you want to do through every element of the list. So in this case we can have a function that takes in. I'll call it a word to make it easier to know what this is because we're really just going through every one of those strings which are words in our case. And then what we need to do is we need to capitalize it so I can call here string that capitalize that word. And then and okay so let me try to copy this code here. Um Unfortunately and I. X. Has to be on the same line unless I imported. So I'm just gonna have it like this for the sake of pertinent for you right here. So as you can tell here we split the string and then we applied string capitalize on every element of this list. So we have a list of words that have been capitalized. So it really accomplished what we want. The final step in here as you might expect is we need to actually take this list of um words and make a single string out of um again here when you want to apply a function to a to a list. The first place I would look at in this kind of situation is yeah. So in um um have the method called um or is it or is it here you go, join. So given an innumerable, innumerable, you could use Joyner as a separator. So you could take a list of element and then you could join them using a separator. That separator can be whatever you want. If you don't put anything, the separator will basically um so let me just strive for you here. So I'll just you know, I'm not join, it will just be an empty string. So it will it will say well hello and then not know space. So it basically just turned our list into a string but there's no spaces between each word. So we have to pass in the separator which I can put as a space and that so I can just go here and I put you know, join and then join everything with. So let me actually copy the entire module right here. And I would basically in this module here. Now when you copy the entire module, it works. But if you just copy a snippet of code, sometimes it doesn't, you know copy paste nicely into I. X. And let's actually try this method. So as you can tell here this method this I shouldn't use the word method. This function actually returns the string which each word capitalized. So Hello world, how are you? Capital H? Capital W. Capital H. Capital a capital you awesome. Um yeah, that basically that solves our problem right there and you know like I'll just remove this comment here. Hopefully you're familiar with it right now. So we took a string, we split it. We mapped every word that we capitalized, every word in that split string which is which is a a list and then we take that list and then we join it back together with a space to make a string all over again. One small improvement I will do if you recall here if we have just a simple way to like we have a very simple function here in this in um map. We could simplify it using the and presents impact. I'll keep both for you. But I will do here map if you remember to open the ampersand syntax we could just do something like this. And then what I can do is I could say well a string that capitalize But then capacity the first argument I received. You just put 10% 1. Okay, so this actually accomplishes the same thing as this. I remember when I first learned elixir, which used to be a little harder to read. That's why I'll keep before you. But this is again very common. A lot of people use it so it's good to get comfortable with that syntax. This is why I'm keeping voice for you for now. But yeah, they both have the same outcome. They both accomplished the same thing. Again, we've got this warning saying redefining module. Uh that is completely okay because all we're doing is we're pasting the same module again in real life projects. You probably wouldn't need to do that because you load every module once awesome. And we noticed we get exactly the same results. Um later we learned how to write unit tests. So you know, you could re factor and run the test and make sure they still run, but for now you can just attempt to run the function again, awesome. So hopefully you're getting more and more comfortable using the pipe operator, the venom library and so on. And yeah, we'll do some more drills in the next lessons. Hopefully it will get you stronger and stronger with elixir. Thank you for joining in and see you next lesson.
124 Views 0 Likes 0 Comments

In this lesson we will build a function to titleize a string using the different techniques we learned.

Comment
Leave a comment (supports markdown format)