Little things that saves time in Elixir
It may look small but not weak.
This article comprises of 7 things that matter in Elixir for writing the short lines with similar functionalities that we are doing with long lines of coding so far with out any fail.
Give less strain to fingers and live long…
Sigils in Elixir are greatly used for the data transformation or simply data conversion.
Check the following example…
list_of_binaries = ["hello","I","am","blackode"]
This is the traditional approach to make the list of binaries in Elixir. However, you can do same thing with
list_of_binaries = ~w/hello I am blackode/
["hello", "I", "am", "blackode"]
So, you can save your time while coding. This sigil is default one. You can define your own sigils.
Check the article about CUSTOM SIGILS
2. Multiple Expressions On Single Line
You can use the semi colon
; to represent the end of the expressions in elixir.
x=1; y=2; z=3; a=4; b=5; c=6; d=7; e=8; f=9;
This is highly useful when you suppose to initialize more number of variables. If you write each one of these in single line, your lines of code increase and it looks odd with such lengthy file and more over you can find all the things at a single place by doing this kinda things.
3. Naming Convention of Functions
You can end a function name with either
! . The
? is used to indicate a function return type to be either
! is used to indicate a function may raise an exception.
These are highly when want your lines of code to look more professional. These are just conventions. There is no compulsory to follow them as these are not rules. But, it is a good practice to write healthy code which saves your time when you visit your code after a long time.
defmodule CheckList do
def check? do
def raise_exception! do
4. Pipeline operator in the SHELL/multiple…
|> operator in elixir is used to pass
output of one function as
input to the other as a first argument. Consider the following lines of code…
When you copy and paste above lines of code in
iex , you will get an syntax error.
The first expression in elixir is valid and it executes and the remaining ones are treated as expressions starting with
|> which are considered as nvalid expressions.
You can do that by writing whole lines of code in single line like following…
iex> -3 |> abs |> Integer.to_string |> IO.puts
You can also do the same thing as following…
Now you can copy and paste above lines of code.
The pipeline operator at the end of the line signals to the shell that more input needs to be provided before the expression is complete.
5. If /Else alternative…
You can perform same if else functionality with the help of
&& operator and
In general we do the
if else in single line as follows
if condition, do: :do_expr, else: else_expr
You can do the same thing as follows
condition && :do_expr || :else_expr
check the following working example…
iex(1)> x = 7
iex(3)> greatest = x>y && x || y
iex(4)> smallest = x<y && x || y
6. Single line function definitions
Writing single line function takes less typing and keeps your code more stylish.
def max_length do
def max_length, do: 500
7. Integer to float conversion
You really don’t need a function to do this.
To be continued…
This is all I got so far… I love to hear more from you as a comments …
Happy Long Coding Life….
Sharing is caring…