Part- 2
Last updated
Last updated
Code grouping stands for something great. It shows you how your code is grouped when you write multiple lines of code in single line with out using braces. It will be more clear with the following example.
If you want to see how this line of code is grouped into, you can check in the following format..
So, by using the quote
and Macro.to_string
you can see how our code is grouped.
This tip came out in discussion with the creator of Ecto MichalMuskala in the Elixir forum.
These replaces the nested complicated conditions. These are my best friends in the situations dealing with more complex comparisons. Trust me you gonna love this.
The ||
operator always returns the first expression which is true. Elixir doesn’t care about the remaining expressions, and won’t evaluate them after a match has been found.
Here if you observe the first expression is false next nil
is also false in elixir next :blackode
which evaluates to true and its value is returned immediately with out evaluating the :elixir
and :jose
. Similarly if all the statements evaluates to false
the last expression is returned.
This &&
returns the second expression if the first expression is true
or else it returns the first expression with out evaluating the second expression. In the above examples the last one is the situation where we encounter to use the &&
operator.
I have self experience with this . When I am novice in elixir, I just compared "5" > 4
unknowingly by an accident and to my surprise it returned with true
.
In Elixir every term can compare with every other term. So one has to be careful in comparisons.
Order of Comparison
number < atom < reference < fun < port < pid < tuple < map < list < bitstring (binary)
When I see this first time, I said to my self “Elixir is Crazy”. This tip really saves time and it resembles your smartness. In Elixir every operator is a macro. So, we can use them as lambda functions.
This is my recent discovery. I always encounter a situation like converting "$34.56"
which is a string and I suppose do arithmetic operations. I usually do something like this before binary pattern matching..
This tip made my day easy. I recently used this is in one of my projects.
At beginning stage, I used to press ^c
^c
twice and restart shell as iex -S mix
whenever I make changes to the project files. If you are doing this now, stop it right now. You can just recompile the project.
Warning: The changes in the config/config.ex
are not reflected. You have to restart the shell again.
Logger is one of my favorite modules. This come in default and starts along with your application. You have to just require
this module. When I am new to Elixir, I always used to write the console outputs as IO.puts "This is value of data"
for code debugging but, those lines get mixed up with other lines of information and It became hard to trace those lines.
This Logger
module solved my problem. It has many features but, I use three definitions very often warn
info
and error
Each definition prints the information with different colors which is more easy to find the statement at a glance.
The best side of this module is it prints along with the time, means it also prints the time while executing your statement. So, you can know the direction of flow of execution.
Before using the Logger
module one has to do require Logger
so all macros will be loaded inside your working module.
This tip is from Anwesh Reddy
We can check the all the applications which are started along with our application. Sometimes we have to check whether a particular application is started or not. So, it helps you in those situations.. If you are a beginner, you don’t feel of using this much. But I am pretty sure of this tip will become handy when you work with multiple applications.
Before I let you to use this tip, I just want to remind you that :atoms are not garbage collected. Atom keys are great! If you have a fixed number of them defined statically in your code, you are in no danger. What you should not do is convert user supplied input into atoms without sanitizing them first because it can lead to out of memory. You should also be cautious if you create dynamic atoms in your code.
But , you can use the .
to retrieve the data from the keys as map.key
unlike the usual notation like map["key"]
. That really saves the typing. But, I don’t encourage this because, as programmers we should really care about memory.
Be sure that when you try to retrieve a key with .
form which is not present in the map, it will raise an key error instead of returning the nil
unlike the map["key"]
which returns nil
if key
is not present in map
Elixir >=1.4.0
has ANSI color printing option to console. You can have great fun with colors. You can also provide background colors.
The red prints in red color, green in green color, yellow in yellow color and normal in white. Have fun with colors…
For more details on color printing check Printex module which I created for fun in Elixir.