part- 5
1. Fetching the default Mix Compilers list
Returns the default compilers used by Mix. The output will look something similar to [:yecc, :leex, :erlang, :elixir, :xref, :app]
.
It can be used in your mix.exs
to prepend or append new compilers to Mix:
2. Picking out the elements in List
We all know that a proper list is a combination of head
and tail
like [head | tail]
. We can use the same principle for picking out the elements in the list like the following way…
We can also use simplified syntax for the same job:
3. get_in /Access.all()
We all know that the get_in function is used to extract the key which is deeper inside the map by providing the list with keys like a following way…
But, if there is a list of maps [maps]
where you have to extract first_name
of the each map, generally we go for enum
. We can also achieve this by using the get_in
and Access.all()
Note: If the key is not present in map, then it returns nil Warning: When you use the get_in
along with Access.all()
, as the first value in the list of keys like above, the users datatype should be list. If you pass the map, it returns the error.
In the above lines of code returns the nil
for key which is not in the map
.
In the above lines of code returns the error for passing map .
However, you can change the position of the Access.all() in the list. But the before key should return the list. Check the following lines of code.
Deep Dive
We can also use the Access.all() with functions like update_in, get_and_update_in, etc.. For instance, given a user with a list of books, here is how to deeply traverse the map and convert all book names to uppercase:
Here, user is not a list unlike in the previous examples where we passed the users as a list. But, we changed the position of Access.all()
and inside the list of keys [:books, Access.all(), :name]
, the value of the key :books
should return the list, other wise it raises an error.
4. Data Comprehension along with filters
We achieve the data comprehension through for x <- [1, 2, 3], do: x + 1
. But we can also add the comprehension along with filter.
General Usage
With filters
Here I am using two lists of numbers and cross product over the lists and filtering out the product which is a odd number.
5. Comprehension with binary strings.
Comprehension with binary is little different. You supposed to wrap inside <<>>
Lets check that…
Did you observe that x <- b_string
is just changed something like << x <- b_string >>
to make the sense.
6. Advanced Comprehension IO.stream
Here we are taking the elixir comprehension to the next level. We read the input from the keyboard and convert that to upcase and after that it should wait for another entry.
Basically IO.stream(:stdio, :line)
will the read a line input from the keyboard.
7. Single Line Multiple module aliasing
We can also alias multiple modules in one line:
8. Importing Underscore Functions
By default the functions with _ are not imported. However, you can do that by importing them with :only
explicitly.
9. Sub string in Elixir
There is no direct sub_str
like function in elixir. However you can achieve that by String.slice/2
10. String Concatenation
We can do the string concatenation in two ways.
I am taking above lines of code for example…
String Interpolation
Using <> operator
This is the best style and recommended one.
If you are having the list of strings ["hello", "blackode"]
then use Enum.join
Last updated