10 Killer Elixir Tips #4

Apr 14, 2017 · 6 min read

The fourth part of Pro Elixir Tips.

This article comprises another set of 10 Elixir Tips with examples and explanation.

Read if you missed Killer Elixir Tips => #1 #2 #3

1. Running Multiple Mix Tasks

mix do deps.get,compile

You can run multiple tasks by separating them with a comma .

However, you can also create aliases in your mix project in a file called .

The project definition looks like the following way when you create one using a tool.

def project do
[app: :proect_name,
version: "0.1.0",
elixir: "~> 1.4-rc",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps()]

You are also allowed to add some extra fields…

Here you have to add the field.

aliases: aliases()

Don’t forget to add at the end when you add this in the middle of .

The should return the list.

defp aliases do
"ecto.setup": ["ecto.create", "ecto.migrate", "ecto.seed"]

So, whenever you run the the three tasks , and will run one after the other.

You can also add them directly as following unlike I did with private function.

def project do
[app: :project_name,
version: "0.1.0",
aliases: ["ecto.setup": ["ecto.create", "ecto.migrate", "ecto.seed"]]

2. Accessing the Documentation

Elixir stores the documentation inside the in a memory. You can access the documentation with the help of function . This means, the documentation accessed when it is required but not when it is loaded in the virtual machine like

Suppose you defined a module in memory like ones you defined in IEx, cannot have their documentation accessed as they do not have their bytecode written to disk.

Let us check this…

Create a module with name with the following code. You can copy and paste it.

defmodule Test do
@moduledoc """
This is the test module docs
@doc """
This is the documentation of hello function
def hello do
IO.puts "hello"

Now stay in the directory where your file exists and run the command

$ iex test.ex

Now you can access the function definitions but not the documentation.

iex> Test.hello

That means the code is compiled but documentation is not stored in the memory. So, you cannot access the docs. Lets check that…

iex> Code.get_docs Hello,:moduledoc

You will see the output as when you are trying to access the docs of the module you have created so far. This is because the is not available in the disk.
In simple way file is not present. Lets do that...

Press twice so you will come out of the shell and this time you run the command as

$ elixirc test.ex

After running the command, you will see a file with name . Now the for the module is available in memory. Now you can access the documentation as follows...

$ iex
iex> Code.get_docs Test,:moduledoc
{3, "This is the test module docs\n"}

The output is a tuple with two elements. The first element is the line number of the documentation it starts and the second element is the actual documentation in the binary form.

You can read more about this function here

3. Verbose Testing

When you go with it will run all the tests defined and gives you the time of testing. However, you can see more verbose output like which test you are running with the option like following…

mix test --trace

It will list out the all tests with names you defined as here is the name of the test.

4. Dynamic Function Name in Elixir Macro

defmacro gen_function(fun_name) do
quote do
def unquote(:"#{fun_name}")() do
# your code...

To be simple the name of the function should be an atom instead of binary.

5. Run Shell Commands in Elixir

System.cmd(command,args,options \\[])

Executes the given command with args.

  • command is expected to be an executable available in PATH unless an absolute path is given.
  • args must be a list of binaries which the executable will receive as its
    arguments as is. This means that:


iex> System.cmd "echo", ["hello"]
{"hello\n", 0}
iex> System.cmd "echo", ["hello"],into: []
{["hello\n"], 0}

Get help from with

Checkout the documentation about for more information and
also check Erlang os Module.

6. Printing List as List without ASCII-Encoding

You know that when the list contains all the numbers as ASCII values, it will list out those values instead of the original numbers. Lets check that…

iex> IO.inspect [97,98]

The code point of is and is hence it is listing out them as . However you can tell the to list them as list itself with option .

iex> IO.inspect [97,98],charlists: :as_lists
[97, 98]

Open and type , you will see that Elixir does this kind of thing with other values as well, specifically structs and binaries.

7. Accessing file name and line number etc…

defmacro __ENV__()

This macro gives the current environment information. You can get the information like current and others…

iex(4)> __ENV__.file
iex(5)> __ENV__.line

You can also use them for debugging purpose in your lines of code.

8. Creating Manual Pids

You can create the pid manually in Elixir with function. This comes with two flavors.

def pid(string)

Creates the pid from the string.

iex> pid("0.21.32")

def pid(a,b,c)

Creates a PID with 3 non negative integers passed as arguments to the function.

iex> pid(0, 21, 32)

Why do you create the pids manually?

Suppose you are writing a library and you want to test one of your functions for the type pid, then you can create one and test over it.

You cannot create the pid like assigning because # is considered as comment here.

iex(6)> pid = #PID<0.21.32>

When you do like above, iex shell just wait for more input as is treated as comment.

Now you enter another data to complete the expression. The entered value is the value of the pid. Lets check that…

iex(6)> pid = #PID<0.21.32>      # here expression is not complete
...(6)> 23 # here we are giving the value 23
23 # expression is complete
iex(7)> pid

9. Replacing the String with global option

The function will replace the given the pattern with replacing pattern. By default, it replaces all the occurrences of the pattern.
Lets check that…

iex(1)> str = "hello@hi.com, blackode@medium.com"    
"hello@hi.com, blackode@medium.com"
iex(2)> String.replace str,"@","#"
"hello#hi.com, blackode#medium.com

is same as

But, if you want to replace only the first occurrence of the pattern, you need to pass the option . So, it replaces only the first occurrence of . Lets check that…

iex(3)> String.replace str,"@","#",global: false
"hello#hi.com, blackode@medium.com"

Here only first is replaced with .

10.Memory Usage

You can check the memory usage with

iex(1)> :erlang.memory
[total: 16221568, processes: 4366128, processes_used: 4364992, system: 11855440,
atom: 264529, atom_used: 250685, binary: 151192, code: 5845369, ets: 331768]

However, you can pass option like to get the memory usage of atoms.

iex(2)> :erlang.memory :atom

Thanks Giving..
Hey guys thanks for the support you have shown in reading and clapping the previous parts of this category.

to be continued … in 10 Killer Elixir Tips #5

Thanks 4 UR Tm.. Read all parts of Killer Elixir Tips and download file at .
Click on link All Killer Elixir Parts. Contribute with a ★

Sharing is Caring !!

if worth_clapping, do: clap, else: nil

Happy Coding !!


Coding, thoughts, and ideas.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store