Elixir – Errors Handling ”; Previous Next Elixir has three error mechanisms: errors, throws and exits. Let us explore each mechanism in detail. Error Errors (or exceptions) are used when exceptional things happen in the code. A sample error can be retrieved by trying to add a number into a string − IO.puts(1 + “Hello”) When the above program is run, it produces the following error − ** (ArithmeticError) bad argument in arithmetic expression :erlang.+(1, “Hello”) This was a sample inbuilt error. Raising Errors We can raise errors using the raise functions. Let us consider an example to understand the same − #Runtime Error with just a message raise “oops” # ** (RuntimeError) oops Other errors can be raised with raise/2 passing the error name and a list of keyword arguments #Other error type with a message raise ArgumentError, message: “invalid argument foo” You can also define your own errors and raise those. Consider the following example − defmodule MyError do defexception message: “default message” end raise MyError # Raises error with default message raise MyError, message: “custom message” # Raises error with custom message Rescuing Errors We do not want our programs to abruptly quit but rather the errors need to be handled carefully. For this we use error handling. We rescue errors using the try/rescue construct. Let us consider the following example to understand the same − Live Demo err = try do raise “oops” rescue e in RuntimeError -> e end IO.puts(err.message) When the above program is run, it produces the following result − oops We have handled errors in the rescue statement using pattern matching. If we do not have any use of the error, and just want to use it for identification purposes, we can also use the form − Live Demo err = try do 1 + “Hello” rescue RuntimeError -> “You”ve got a runtime error!” ArithmeticError -> “You”ve got a Argument error!” end IO.puts(err) When running above program, it produces the following result − You”ve got a Argument error! NOTE − Most functions in the Elixir standard library are implemented twice, once returning tuples and the other time raising errors. For example, the File.read and the File.read! functions. The first one returned a tuple if the file was read successfully and if an error was encountered, this tuple was used to give the reason for the error. The second one raised an error if an error was encountered. If we use the first function approach, then we need to use case for pattern matching the error and take action according to that. In the second case, we use the try rescue approach for error prone code and handle errors accordingly. Throws In Elixir, a value can be thrown and later be caught. Throw and Catch are reserved for situations where it is not possible to retrieve a value unless by using throw and catch. The instances are quite uncommon in practice except when interfacing with libraries. For example, let us now assume that the Enum module did not provide any API for finding a value and that we needed to find the first multiple of 13 in a list of numbers − Live Demo val = try do Enum.each 20..100, fn(x) -> if rem(x, 13) == 0, do: throw(x) end “Got nothing” catch x -> “Got #{x}” end IO.puts(val) When the above program is run, it produces the following result − Got 26 Exit When a process dies of “natural causes” (for example, unhandled exceptions), it sends an exit signal. A process can also die by explicitly sending an exit signal. Let us consider the following example − spawn_link fn -> exit(1) end In the example above, the linked process died by sending an exit signal with value of 1. Note that exit can also be “caught” using try/catch. For example − Live Demo val = try do exit “I am exiting” catch :exit, _ -> “not really” end IO.puts(val) When the above program is run, it produces the following result − not really After Sometimes it is necessary to ensure that a resource is cleaned up after some action that can potentially raise an error. The try/after construct allows you to do that. For example, we can open a file and use an after clause to close it–even if something goes wrong. {:ok, file} = File.open “sample”, [:utf8, :write] try do IO.write file, “olá” raise “oops, something went wrong” after File.close(file) end When we run this program, it will give us an error. But the after statement will ensure that the file descriptor is closed upon any such event. Print Page Previous Next Advertisements ”;
Category: elixir
Elixir – Processes
Elixir – Processes ”; Previous Next In Elixir, all code runs inside processes. Processes are isolated from each other, run concurrent to one another and communicate via message passing. Elixir’s processes should not be confused with operating system processes. Processes in Elixir are extremely lightweight in terms of memory and CPU (unlike threads in many other programming languages). Because of this, it is not uncommon to have tens or even hundreds of thousands of processes running simultaneously. In this chapter, we will learn about the basic constructs for spawning new processes, as well as sending and receiving messages between different processes. The Spawn Function The easiest way to create a new process is to use the spawn function. The spawn accepts a function that will be run in the new process. For example − pid = spawn(fn -> 2 * 2 end) Process.alive?(pid) When the above program is run, it produces the following result − false The return value of the spawn function is a PID. This is a unique identifier for the process and so if you run the code above your PID, it will be different. As you can see in this example, the process is dead when we check to see if it alive. This is because the process will exit as soon as it has finished running the given function. As already mentioned, all Elixir codes run inside processes. If you run the self function you will see the PID for your current session − pid = self Process.alive?(pid) When the above program is run, it produces following result − true Message Passing We can send messages to a process with send and receive them with receive. Let us pass a message to the current process and receive it on the same. Live Demo send(self(), {:hello, “Hi people”}) receive do {:hello, msg} -> IO.puts(msg) {:another_case, msg} -> IO.puts(“This one won”t match!”) end When the above program is run, it produces the following result − Hi people We sent a message to the current process using the send function and passed it to the PID of self. Then we handled the incoming message using the receive function. When a message is sent to a process, the message is stored in the process mailbox. The receive block goes through the current process mailbox searching for a message that matches any of the given patterns. The receive block supports guards and many clauses, such as case. If there is no message in the mailbox matching any of the patterns, the current process will wait until a matching message arrives. A timeout can also be specified. For example, receive do {:hello, msg} -> msg after 1_000 -> “nothing after 1s” end When the above program is run, it produces the following result − nothing after 1s NOTE − A timeout of 0 can be given when you already expect the message to be in the mailbox. Links The most common form of spawning in Elixir is actually via spawn_link function. Before taking a look at an example with spawn_link, let us understand what happens when a process fails. spawn fn -> raise “oops” end When the above program is run, it produces the following error − [error] Process #PID<0.58.00> raised an exception ** (RuntimeError) oops :erlang.apply/2 It logged an error but the spawning process is still running. This is because processes are isolated. If we want the failure in one process to propagate to another one, we need to link them. This can be done with the spawn_link function. Let us consider an example to understand the same − spawn_link fn -> raise “oops” end When the above program is run, it produces the following error − ** (EXIT from #PID<0.41.0>) an exception was raised: ** (RuntimeError) oops :erlang.apply/2 If you are running this in iex shell then the shell handles this error and does not exit. But if you run by first making a script file and then using elixir <file-name>.exs, the parent process will also be brought down due to this failure. Processes and links play an important role when building fault-tolerant systems. In Elixir applications, we often link our processes to supervisors which will detect when a process dies and start a new process in its place. This is only possible because processes are isolated and don’t share anything by default. And since processes are isolated, there is no way a failure in a process will crash or corrupt the state of another. While other languages will require us to catch/handle exceptions; in Elixir, we are actually fine with letting processes fail because we expect supervisors to properly restart our systems. State If you are building an application that requires state, for example, to keep your application configuration, or you need to parse a file and keep it in memory, where would you store it? Elixir”s process functionality can come in handy when doing such things. We can write processes that loop infinitely, maintain state, and send and receive messages. As an example, let us write a module that starts new processes that work as a key-value store in a file named kv.exs. defmodule KV do def start_link do Task.start_link(fn -> loop(%{}) end) end defp loop(map) do receive do {:get, key, caller} -> send caller, Map.get(map, key) loop(map) {:put, key, value} -> loop(Map.put(map, key, value)) end end end Note that the start_link function starts a new process that runs the loop function, starting with an empty map. The loop function then waits for messages and performs the appropriate action for each message. In the case of a :get message, it sends a message back to the caller and calls loop again, to wait for a new message. While the :put message actually invokes loop with a new version of the map, with the given key and value stored. Let us now run the following − iex kv.exs Now you should be in your iex shell. To test out our module, try the following
Elixir – Data Types
Elixir – Data Types ”; Previous Next For using any language, you need to understand the basic data types the language supports. In this chapter, we will discuss 7 basic data types supported by the elixir language: integers, floats, Booleans, atoms, strings, lists and tuples. Numerical Types Elixir, like any other programming language, supports both integers and floats. If you open your elixir shell and input any integer or float as input, it”ll return its value. For example, 42 When the above program is run, it produces the following result − 42 You can also define numbers in octal, hex and binary bases. Octal To define a number in octal base, prefix it with ”0o”. For example, 0o52 in octal is equivalent to 42 in decimal. Hexadecimal To define a number in decimal base, prefix it with ”0x”. For example, 0xF1 in hex is equivalent to 241 in decimal. Binary To define a number in binary base, prefix it with ”0b”. For example, 0b1101 in binary is equivalent to 13 in decimal. Elixir supports 64bit double precision for floating point numbers. And they can also be defined using an exponentiation style. For example, 10145230000 can be written as 1.014523e10 Atoms Atoms are constants whose name is their value. They can be created using the color(:) symbol. For example, :hello Booleans Elixir supports true and false as Booleans. Both these values are in fact attached to atoms :true and :false respectively. Strings Strings in Elixir are inserted between double quotes, and they are encoded in UTF-8. They can span multiple lines and contain interpolations. To define a string simply enter it in double quotes − “Hello world” To define multiline strings, we use a syntax similar to python with triple double quotes − “”” Hello World! “”” We”ll learn about strings, binaries and char lists(similar to strings) in depth in the strings chapter. Binaries Binaries are sequences of bytes enclosed in << >> separated with a comma. For example, << 65, 68, 75>> Binaries are mostly used to handle bits and bytes related data, if you have any. They can, by default, store 0 to 255 in each value. This size limit can be increased by using the size function that says how many bits it should take to store that value. For example, <<65, 255, 289::size(15)>> Lists Elixir uses square brackets to specify a list of values. Values can be of any type. For example, [1, “Hello”, :an_atom, true] Lists come with inbuilt functions for head and tail of the list named hd and tl which return the head and tail of the list respectively. Sometimes when you create a list, it”ll return a char list. This is because when elixir sees a list of printable ASCII characters, it prints it as a char list. Please note that strings and char lists are not equal. We”ll discuss lists further in later chapters. Tuples Elixir uses curly brackets to define tuples. Like lists, tuples can hold any value. { 1, “Hello”, :an_atom, true A question arises here, – why provide both lists and tuples when they both work in the same way? Well they have different implementations. Lists are actually stored as linked lists, so insertions, deletions are very fast in lists. Tuples on the other hand, are stored in contiguous memory block, which make accessing them faster but adds an additional cost on insertions and deletions. Print Page Previous Next Advertisements ”;
Elixir – Modules
Elixir – Modules ”; Previous Next In Elixir, we group several functions into modules. We have already used different modules in the previous chapters such as the String module, Bitwise module, Tuple module, etc. In order to create our own modules in Elixir, we use the defmodule macro. We use the def macro to define functions in that module − defmodule Math do def sum(a, b) do a + b end end In the following sections, our examples are going to get longer in size, and it can be tricky to type them all in the shell. We need to learn how to compile Elixir code and also how to run Elixir scripts. Compilation It is always convenient to write modules into files so they can be compiled and reused. Let us assume we have a file named math.ex with the following content − defmodule Math do def sum(a, b) do a + b end end We can compile the files using the command −elixirc : $ elixirc math.ex This will generate a file named Elixir.Math.beam containing the bytecode for the defined module. If we start iex again, our module definition will be available (provided that iex is started in the same directory the bytecode file is in). For example, IO.puts(Math.sum(1, 2)) The above program will generate the following result − 3 Scripted Mode In addition to the Elixir file extension .ex, Elixir also supports .exs files for scripting. Elixir treats both files exactly the same way, the only difference is in the objective. .ex files are meant to be compiled while .exs files are used for scripting. When executed, both extensions compile and load their modules into memory, although only .ex files write their bytecode to disk in the format of .beam files. For example, if we wanted to run the Math.sum in the same file, we can use the .exs in following way − Math.exs Live Demo defmodule Math do def sum(a, b) do a + b end end IO.puts(Math.sum(1, 2)) We can run it using the Elixir command − $ elixir math.exs The above program will generate the following result − 3 The file will be compiled in memory and executed, printing “3” as the result. No bytecode file will be created. Module Nesting Modules can be nested in Elixir. This feature of the language helps us organize our code in a better way. To create nested modules, we use the following syntax − defmodule Foo do #Foo module code here defmodule Bar do #Bar module code here end end The example given above will define two modules: Foo and Foo.Bar. The second can be accessed as Bar inside Foo as long as they are in the same lexical scope. If, later, the Bar module is moved outside the Foo module definition, it must be referenced by its full name (Foo.Bar) or an alias must be set using the alias directive discussed in the alias chapter. Note − In Elixir, there is no need to define the Foo module in order to define the Foo.Bar module, as the language translates all module names to atoms. You can define arbitrarilynested modules without defining any module in the chain. For example, you can define Foo.Bar.Baz without defining Foo or Foo.Bar. Print Page Previous Next Advertisements ”;
Elixir – Sigils
Elixir – Sigils ”; Previous Next In this chapter, we are going to explore sigils, the mechanisms provided by the language for working with textual representations. Sigils start with the tilde (~) character which is followed by a letter (which identifies the sigil) and then a delimiter; optionally, modifiers can be added after the final delimiter. Regex Regexes in Elixir are sigils. We have seen their use in the String chapter. Let us again take an example to see how we can use regex in Elixir. Live Demo # A regular expression that matches strings which contain “foo” or # “bar”: regex = ~r/foo|bar/ IO.puts(“foo” =~ regex) IO.puts(“baz” =~ regex) When the above program is run, it produces the following result − true false Sigils support 8 different delimiters − ~r/hello/ ~r|hello| ~r”hello” ~r”hello” ~r(hello) ~r[hello] ~r{hello} ~r<hello> The reason behind supporting different delimiters is that different delimiters can be more suited for different sigils. For example, using parentheses for regular expressions may be a confusing choice as they can get mixed with the parentheses inside the regex. However, parentheses can be handy for other sigils, as we will see in the next section. Elixir supports Perl compatible regexes and also support modifiers. You can read up more about the use of regexes here. Strings, Char lists and Word lists Other than regexes, Elixir has 3 more inbuilt sigils. Let us have a look at the sigils. Strings The ~s sigil is used to generate strings, like double quotes are. The ~s sigil is useful, for example, when a string contains both double and single quotes − new_string = ~s(this is a string with “double” quotes, not ”single” ones) IO.puts(new_string) This sigil generates strings. When the above program is run, it produces the following result − “this is a string with “double” quotes, not ”single” ones” Char Lists The ~c sigil is used to generate char lists − Live Demo new_char_list = ~c(this is a char list containing ”single quotes”) IO.puts(new_char_list) When the above program is run, it produces the following result − this is a char list containing ”single quotes” Word Lists The ~w sigil is used to generate lists of words (words are just regular strings). Inside the ~w sigil, words are separated by whitespace. Live Demo new_word_list = ~w(foo bar bat) IO.puts(new_word_list) When the above program is run, it produces the following result − foobarbat The ~w sigil also accepts the c, s and a modifiers (for char lists, strings and atoms, respectively), which specify the data type of the elements of the resulting list − new_atom_list = ~w(foo bar bat)a IO.puts(new_atom_list) When the above program is run, it produces the following result − [:foo, :bar, :bat] Interpolation and Escaping in Sigils Besides lowercase sigils, Elixir supports uppercase sigils to deal with escaping characters and interpolation. While both ~s and ~S will return strings, the former allows escape codes and interpolation while the latter does not. Let us consider an example to understand this − ~s(String with escape codes x26 #{“inter” <> “polation”}) # “String with escape codes & interpolation” ~S(String without escape codes x26 without #{interpolation}) # “String without escape codes \x26 without #{interpolation}” Custom Sigils We can easily create our own custom sigils. In this example, we will create a sigil to convert a string to uppercase. defmodule CustomSigil do def sigil_u(string, []), do: String.upcase(string) end import CustomSigil IO.puts(~u/tutorials point/) When we run the above code, it produces the following result − TUTORIALS POINT First we define a module called CustomSigil and within that module, we created a function called sigil_u. As there is no existing ~u sigil in the existing sigil space, we will use it. The _u indicates that we wish use u as the character after the tilde. The function definition must take two arguments, an input and a list. Print Page Previous Next Advertisements ”;
Elixir – Variables
Elixir – Variables ”; Previous Next A variable provides us with named storage that our programs can manipulate. Each variable in Elixir has a specific type, which determines the size and layout of the variable”s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. Types of Variables Elixir supports the following basic types of variables. Integer These are used for Integers. They are of size 32bit on a 32bit architecture and 64 bits on a 64-bit architecture. Integers are always signed in elixir. If an integer starts to expand in size above its limit, elixir convers it in a Big Integer which takes up memory in range 3 to n words whichever can fit it in memory. Floats Floats have a 64-bit precision in elixir. They are also like integers in terms of memory. When defining a float, exponential notation can be used. Boolean They can take up 2 values which is either true or false. Strings Strings are utf-8 encoded in elixir. They have a strings module which provides a lot of functionality to the programmer to manipulate strings. Anonymous Functions/Lambdas These are functions that can be defined and assigned to a variable, which can then be used to call this function. Collections There are a lot of collection types available in Elixir. Some of them are Lists, Tuples, Maps, Binaries, etc. These will be discussed in subsequent chapters. Variable Declaration A variable declaration tells the interpreter where and how much to create the storage for the variable. Elixir does not allow us to just declare a variable. A variable must be declared and assigned a value at the same time. For example, to create a variable named life and assign it a value 42, we do the following − life = 42 This will bind the variable life to value 42. If we want to reassign this variable a new value, we can do this by using the same syntax as above, i.e., life = “Hello world” Variable Naming Naming variables follow a snake_case convention in Elixir, i.e., all variables must start with a lowercase letter, followed by 0 or more letters(both upper and lower case), followed at the end by an optional ”?” OR ”!”. Variable names can also be started with a leading underscore but that must be used only when ignoring the variable, i.e., that variable will not be used again but is needed to be assigned to something. Printing Variables In the interactive shell, variables will print if you just enter the variable name. For example, if you create a variable − life = 42 And enter ”life” in your shell, you”ll get the output as − 42 But if you want to output a variable to the console (When running an external script from a file), you need to provide the variable as input to IO.puts function − Live Demo life = 42 IO.puts life or Live Demo life = 42 IO.puts(life) This will give you the following output − 42 Print Page Previous Next Advertisements ”;
Elixir – Home
Elixir Tutorial PDF Version Quick Guide Resources Job Search Discussion Elixir is a dynamic, functional language designed for building scalable and maintainable applications. It is built on top of Erlang. Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain. Audience This tutorial is created for software programmers who aim to learn the fundamentals of Elixir programming language from scratch. This tutorial will give you a basic foundation to start programming in Elixir programming language. Prerequisites Before proceeding with this tutorial, you should have a basic understanding of Computer Programming terminologies and exposure to any other programming language. Some familiarity with functional programming will help you in learning Elixir. Print Page Previous Next Advertisements ”;
Elixir – Basic Syntax
Elixir – Basic Syntax ”; Previous Next We will start with the customary ”Hello World” program. To start the Elixir interactive shell, enter the following command. iex After the shell starts, use the IO.puts function to “put” the string on the console output. Enter the following in your Elixir shell − Live Demo IO.puts “Hello world” In this tutorial, we will use the Elixir script mode where we will keep the Elixir code in a file with the extension .ex. Let us now keep the above code in the test.ex file. In the succeeding step, we will execute it using elixirc− Live Demo IO.puts “Hello world” Let us now try to run the above program as follows − $elixirc test.ex The above program generates the following result − Hello World Here we are calling a function IO.puts to generate a string to our console as output. This function can also be called the way we do in C, C++, Java, etc., providing arguments in parentheses following the function name − IO.puts(“Hello world”) Comments Single line comments start with a ”#” symbol. There”s no multi-line comment, but you can stack multiple comments. For example − #This is a comment in Elixir Line Endings There are no required line endings like ”;” in Elixir. However, we can have multiple statements in the same line, using ”;”. For example, Live Demo IO.puts(“Hello”); IO.puts(“World!”) The above program generates the following result − Hello World! Identifiers Identifiers like variables, function names are used to identify a variable, function, etc. In Elixir, you can name your identifiers starting with a lower case alphabet with numbers, underscores and upper case letters thereafter. This naming convention is commonly known as snake_case. For example, following are some valid identifiers in Elixir − var1 variable_2 one_M0r3_variable Please note that variables can also be named with a leading underscore. A value that is not meant to be used must be assigned to _ or to a variable starting with underscore − _some_random_value = 42 Also elixir relies on underscores to make functions private to modules. If you name a function with a leading underscore in a module, and import that module, this function will not be imported. There are many more intricacies related to function naming in Elixir which we will discuss in coming chapters. Reserved Words Following words are reserved and cannot be used as variables, module or function names. after and catch do inbits inlist nil else end not or false fn in rescue true when xor __MODULE__ __FILE__ __DIR__ __ENV__ __CALLER__ Print Page Previous Next Advertisements ”;
Elixir – Operators
Elixir – Operators ”; Previous Next An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. There are a LOT of operators provided by elixir. They are divided in the following categories − Arithmetic operators Comparison operators Boolean operators Misc operators Arithmetic Operators The following table shows all the arithmetic operators supported by Elixir language. Assume variable A holds 10 and variable B holds 20, then − Show Examples Operator Description Example + Adds 2 numbers. A + B will give 30 – Subtracts second number from first. A-B will give -10 * Multiplies two numbers. A*B will give 200 / Divides first number from second. This casts the numbers in floats and gives a float result A/B will give 0.5. div This function is used to get the quotient on division. div(10,20) will give 0 rem This function is used to get the remainder on division. rem(A, B) will give 10 Comparison Operators The comparison operators in Elixir are mostly common to those provided in most other languages. The following table sums up comparison operators in Elixir. Assume variable A holds 10 and variable B holds 20, then − Show Examples Operator Description Example == Checks if value on left is equal to value on right(Type casts values if they are not the same type). A == B will give false != Checks if value on left is not equal to value on right. A != B will give true === Checks if type of value on left equals type of value on right, if yes then check the same for value. A === B will give false !== Same as above but checks for inequality instead of equality. A !== B will give true > Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true. A > B will give false < Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true. A < B will give true >= Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true. A >= B will give false <= Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true. A <= B will give true Logical operators Elixir provides 6 logical operators: and, or, not, &&, || and !. The first three, and or not are strict Boolean operators, meaning that they expect their first argument to be a Boolean. Non Boolean argument will raise an error. While the next three, &&, || and ! are non strict, do not require us to have the first value strictly as a boolean. They work in the same way as their strict counterparts. Assume variable A holds true and variable B holds 20, then − Show Examples Operator Description Example and Checks if both values provided are truthy, if yes then returns the value of second variable. (Logical and). A and B will give 20 or Checks if either value provided is truthy. Returns whichever value is truthy. Else returns false. (Logical or). A or B will give true not Unary operator which inverts the value of given input. not A will give false && Non-strict and. Works same as and but does not expect first argument to be a Boolean. B && A will give 20 || Non-strict or. Works same as or but does not expect first argument to be a Boolean. B || A will give true ! Non-strict not. Works same as not but does not expect the argument to be a Boolean. !A will give false NOTE −and, or, && and || || are short circuit operators. This means that if the first argument of and is false, then it will not further check for the second one. And if the first argument of or is true, then it will not check for the second one. For example, false and raise(“An error”) #This won”t raise an error as raise function wont get executed because of short #circuiting nature of and operator Bitwise Operators Bitwise operators work on bits and perform bit by bit operation. Elixir provides bitwise modules as part of the package Bitwise, so in order to use these, you need to use the bitwise module. To use it, enter the following command in your shell − use Bitwise Assume A to be 5 and B to be 6 for the following examples − Show Examples Operator Description Example &&& Bitwise and operator copies a bit to result if it exists in both operands. A &&& B will give 4 ||| Bitwise or operator copies a bit to result if it exists in either operand. A ||| B will give 7 >>> Bitwise right shift operator shifts first operand bits to the right by the number specified in second operand. A >>> B will give 0 <<< Bitwise left shift operator shifts first operand bits to the left by the number specified in second operand. A <<< B will give 320 ^^^ Bitwise XOR operator copies a bit to result only if it is different on both operands. A ^^^ B will give 3 ~~~ Unary bitwise not inverts the bits on the given number. ~~~A will give -6 Misc Operators Other than the above operators, Elixir also provides a range of other operators like Concatenation Operator, Match Operator, Pin Operator, Pipe Operator, String Match Operator, Code Point Operator, Capture Operator, Ternary Operator that make it quite a powerful language. Show Examples Print Page Previous Next Advertisements ”;
Elixir – Environment
Elixir – Environment ”; Previous Next In order to run Elixir, you need to set it up locally on your system. To install Elixir, you will first require Erlang. On some platforms, Elixir packages come with Erlang in them. Installing Elixir Let us now understand the installation of Elixir in different Operating Systems. Windows Setup To install Elixir on windows, download installer from https://elixir-lang.org/install.html#windows and simply click Next to proceed through all steps. You will have it on your local system. If you have any problems while installing it, you can check this page for more info. Mac Setup If you have Homebrew installed, make sure that it is the latest version. For updating, use the following command − brew update Now, install Elixir using the command given below − brew install elixir Ubuntu/Debian Setup The steps to install Elixir in an Ubuntu/Debian setup is as follows − Add Erlang Solutions repo − wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb && sudo dpkg -i erlang-solutions_1.0_all.deb sudo apt-get update Install the Erlang/OTP platform and all of its applications − sudo apt-get install esl-erlang Install Elixir − sudo apt-get install elixir Other Linux Distros If you have any other Linux distribution, please visit this page to set up elixir on your local system. Testing the Setup To test the Elixir setup on your system, open your terminal and enter iex in it. It will open the interactive elixir shell like the following − Erlang/OTP 19 [erts-8.0] [source-6dc93c1] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] Interactive Elixir (1.3.1) – press Ctrl+C to exit (type h() ENTER for help) iex(1)> Elixir is now successfully set up on your system. Print Page Previous Next Advertisements ”;