DEV Community

Cover image for Elixir LIbraries
Cody Daigle
Cody Daigle

Posted on

Elixir LIbraries


One of my favorite things about software development is that you can never grow bored or know too much. There is always something that pulls me in deeper and deeper and lately that has been Elixir. I have written two other blogs going over the strengths of Elixir and how Erlang’s Virtual Machine(The BEAM) works well with Elixir. Elixir is made up of six applications and I’m continuing this journey with a breakdown of what Elixir is and it's three libraries.

What is Elixir?

A dynamic, functional programming language designed for building scalable, maintainable applications, leveraging The BEAM, which is known for its fault-tolerant, concurrent, and low latency systems. Needless to say, it's a go-to choice for a large range of applications that aim to benefit from its productive tooling and extensible design.

Web Development

due to it being a powerful tool for building web applications, especially ones such as chat applications, online games, applications that require live updates, and other real-time features.

Distributed

Elixir runs on Erlang’s virtual machine, which was specifically designed for building not only fault-tolerant systems but to also be distributed, which is perfect for applications that require distribution across multiple nodes

Concurrency

First-class support for concurrency making it a excel with applications that require high levels of concurrency like mulit-user applications or applications with high simultaneous connections.

Embedded Systems

Can be used to write firmware for devices and its concurrency model and fault-tolerance fits in nicely with this domain.

Data Processing

Handles large volumes of data and concurrency support making it solid for data processing tasks.

Telecommunications

Elixir is a descendant of Erlang, which was originally designed for telecommunication systems, making it fit in naturally with these applications/systems.

Blockchain

Continuing on the scalability and fault-tolerance, Elixir is also used for developing blockchain technologies.

Apsen

Elixir's Standard Library

The standard library in Elixir provides a set of modules and functions that are available out of the box for Elixir developers. It’s loaded with a wide range of functionalities that aid in alleviating common programming tasks.

Enum: Provides a set of algorithms that enumerate over enumerables based on the Enumerable protocol.
map, reduce, and filter

String: Provides functions for working with strings, like string manipulation, in Elixir.
length, replace, split

List: This module provides functions for working with lists.
delete, flatten, zip

Map: This module provides functions for working with maps.
get, put, keys

IO: This module provides functions for input/output operations.
puts, gets, inspect

File: This module provides functions for working with files.
read, write, ls

Process: This module provides functions for working with processes.
send, receive, alive?

GenServer (Generic Server Process): This module abstracts client-server interactions, making the creation of Elixir servers easier by automatically implementing a standard set of interface functions, including tracing and error reporting. With that part of the load taken care of all that is required from the developers is to implement the functionality they desire. Neat.

Agent: This module provides a basic server implementation that allows state to be retrieved and updated.

Task: This module makes running functions asynchronously and retrieving their results easy.

These modules and their functions form the backbone of many Elixir applications, which provide crucial functionalities for tasks like data manipulation, concurrent programming, file I/O, and more.

Pudding

EEx: Elixir's Templating Library

Embedded Elixir(EEx) is a templating library, but still a part of the standard library. In Web Development it’s often used to generate dynamic HTML content allowing you to embed Elixir code in the HTML templates to create parts of the page according to data dynamically. You can generate dynamic HTML content and Elixir code dynamically, which is handy for those metaprogramming moments. Configuration files can also be generated dynamically based on either environment variables or conditions.

Here's a basic example of how EEx is used in an Elixir application:

# Define a template with placeholders
template = "Hello, <%= name %>!"

# Use EEx to evaluate the template with given bindings
message = EEx.eval_string(template, [name: "World"])

# Outputs: Hello, World!
IO.puts message  
Enter fullscreen mode Exit fullscreen mode

Here, <%= name %> is a placeholder with embedded Elixir code. When EEx.eval_string is called, the placeholder is then replaced with the value of name from the given bindings.

ExUnit: Elixir's Unit Test Library

ExUnit is Elixir's built-in testing framework which provides the ability to write unit tests and various features for teardown of tests*,* setup, and assertions. Testing is essential in software development for multiple reasons. Including correctness to ensure your code behaves as expected. Prevents regression by making sure, after modifying your code, you don’t break functionality that already exists. Aids in the design of your software, in turn helping you write more maintainable, modular code. And possibly the most important, in my opinion, is that it serves as documentation, showing those that look at your code, how your code is suppose to work.

defmodule MyApp.CalculatorTest do
  use ExUnit.Case

  test "add/2 returns the sum of its arguments" do
    assert MyApp.Calculator.add(1, 2) == 3
  end
end
Enter fullscreen mode Exit fullscreen mode

We have our test module MyApp.CalculatorTest that uses ExUnit.Case. Inside, we define our test with the test macro. The test checks that the add/2 function of the MyApp.Calculator module returns the correct sum. To run the tests, you would typically use the mix test command in your terminal.

mix test

Enter fullscreen mode Exit fullscreen mode

This will run all test files in the project’s test directory. This also allows you to specify individual test files or directories to run a subset of your tests, for example:

mix test test/my_app/calculator_test.exs
Enter fullscreen mode Exit fullscreen mode

This will only run the tests defined in calculator_test.exs.

In conclusion, I'd like to say the benefits you gain when using Elixir, and all it has to offer, is fantastic. It makes me excited to keep working with it and learning so much more. I hope you learned as much as I did!

Top comments (0)