Elixir is a dynamic, functional language that specializes in writing distributed and scalable applications. This course, Getting Started with Elixir, focuses on getting you up and running with Elixir. First, you'll learn the basic syntax of Elixir and what makes it different from other languages. Next, you'll discover how to build your own modules and write code in a functional programming style, You'll wrap up the course by writing and testing an application. By the end of this course, you'll have a good understanding of basic Elixir concepts and you'll be equipped to write fault-tolerant applications and libraries that can be used by other developers.
Course Overview Hi everyone. My name is Nate Taylor, and welcome to my course, Getting Started with Elixir. I'm a software engineer at Aviture in Omaha, Nebraska. Elixir is a relatively new dynamic functional language. While it's new, it is built on top of the Erlang VM, which is used across the world for distributed and highly available applications by companies such as Amazon, Facebook, and Yahoo. Elixir itself is used by companies such as Lexmark, Pinterest, and Bleacher Report. This course is designed as an introduction to Elixir. No prior experience with Elixir or functional programming is required before starting. Some of the major topics that we'll cover include the Basic Elixir Syntax, Pattern Matching and why it's important, the Elixir Ecosystem, as well as creating an Elixir application. By the end of this course you'll have a foundational understanding of Elixir, which you will use to create an application that will schedule tweets through the Twitter API. Before beginning the course you should be familiar with general programming concepts such as data types, functions, and control flow. For example, you should know what a for loop is. I hope you'll join me on this journey to learn Elixir with the Getting Started with Elixir course, at Pluralsight.
Modules and Functions Up until this point, all the work that we've been doing with Elixir has been inside iex. This has let us see some basic functionality such as numbers and atoms, as well as pattern matching. But apart from experimenting and testing, it hasn't been very useful to us. We certainly shouldn't expect our users to only interact with iex. After all, we'll likely be writing software for users that aren't developers, and as such, they're not going to want to spend a lot of time entering various commands in a CLI. So it's time to start combining some of what we learned in the last module into bigger blocks of code. That's exactly what we're going to do in this module. In Elixir, the basic building block is a function. Functions transform data, taking the input, performing an action, and returning some output. We saw this in the last module when we used some standard functions such as hd or elem or List. insert_at. All of those take inputs, perform an operation, and return outputs. If we want to build an application in Elixir we'll have to start writing functions, but in order for our code to be maintainable, we need to be able to group similar functions together, and this is where modules come in. For example in the last module, when we used the insert_at function, we prefaced it with List. That's because insert_at is a function inside the list module. In fact, with Elixir the only way to have a named function is to place it inside of a module. In this module we're going to look at how to create modules and functions. We'll look at how we can reference other modules in a custom module and we'll examine pattern matching in more detail. Specifically, how we can capitalize on pattern matching with our functions and how doing so can reduce the complexity of the code. By the end of this module you'll be equipped to create your own Elixir modules, as well as writing your own named functions, and we'll then leverage that skill in the next modules as we take on more advanced concepts.
Control Flow If you've been around programming for a while, you probably realize that so far we've only dealt with some very specific situations. The functions we've written don't really make any decisions. They take some data and return a value. While we did handle the case of an empty list, we haven't yet had the need to make any other branching logic decisions, but what if we need to handle multiple cases? For example, earlier in the course we talked about how reading a file would return either ok with the content or error with the error data. If we were to read a file, we would need to handle the branching logic of what to do when a file successfully is read versus what to do when a file is not read. Elixir has three primary ways to handle the various branches. Those are if or unless, cond, and case. Each of these have their own advantages and disadvantages and we'll look at each one in this module. We still haven't talked about any way to iterate over data. At the end of the last clip we used map and reduce, both of which worked on an entire list. If we were to look under the hood of Elixir and how it implements map, what would we see? It might surprise you that it would not be a for loop. In fact, Elixir doesn't have a for loop keyword. It also doesn't have a while loop. Instead, Elixir accomplishes this functionality with recursion. Recursion is used extensively in Elixir. After letting that sink in, I can almost hear your heartbeat speeding up, but don't worry. As we cover recursion in this model, we'll actually see how Elixir handles it and how we can take advantage of its abundance for our own purposes. After this module we'll be done covering the basics of the Elixir syntax, and we'll be just a short distance away from creating our own application.
Elixir Ecosystem At this point in the course we've covered a lot of the basics in terms of syntax, as well as some of the basics of functional programming such as immutability and recursion. We've got a lot of building blocks for creating an application. Before we get into our application, however, let's talk about some of the ecosystem around Elixir. In this module we're going to examine two key aspects of the ecosystem, and they are mix and hex. Mix is the Elixir build tool, but it actually provides more functionality than just compiling an application once it's written. Some frameworks in Elixir such as Phoenix, the web framework, rely on mix to do more than others. In this module we'll cover the basics of mix, and at the end of the module we'll use mix to kick off a brand new project. Hex is a package manager for Elixir, which will allow us to bring in packages written by other people and use them in our application. In this module we'll give an overview of hex. In the next module we'll use it to install modules as we write our application.
Creating an Application We've covered enough syntax and other basics that we're ready to create an application, and that's exactly what we're going to do in this module. We actually started in the last module by performing mix new Pluralsight_tweet --sup. This created an empty project that we're going to work on for this module. We're going to write an application that will read in a file of text, parse it, format it accordingly, and then tweet it out. It will do all of this on a schedule. For example, the first of the month. We can think of our application like this. We have a part of our application that is going to handle the reading of a file. We'll take the output of the file reader and we'll send that to some code that will tweet out our message. All of this is performed on a schedule. It's sometimes helpful to visualize Elixir applications like this, which show the separation boundaries which could be turned into processes, if need be. Elixir utilizes Erlang processes, which are different from OS processes. Erlang processes are very lightweight. They start with a low memory footprint of approximately 2K. In fact, it's not uncommon to have tens or hundreds of thousands of processes running simultaneously in Elixir. We're going to take advantage of hex for this application, so we'll bring in a module to send out the tweet and a module to handle the schedule. By the end of this module you'll have a working application.
Testing Elixir The purpose of this course has been to introduce you to Elixir and help you get your first application up and running. As such, the focus has been on things that will move us in that direction, even if it means possibly ignoring some best practices. One of the best practices in writing software is doing test-driven development. There are other courses in the Pluralsight library that deal specifically TDD, so we won't repeat the material here. While we didn't use TDD to create our application, it would be irresponsible to do an entire course on Elixir and not talk about testing the code, therefore we'll cover that material now. We won't be adding any new functionality. We're simply going to make sure that what we have performs like what we'd expect. In this module we're going to cover ExUnit, which is the basic unit testing library that comes with Elixir, and since our application depends on both the file system and on the Twitter API, we'll look at how we can write tests without constantly reading files from the file system. We'll wrap the module up by looking at something special in Elixir, and that is writing documentation that can also perform tests. When we wrap up with this module you'll be equipped to start writing tests in your Elixir application.