Part- 3
1. Functions as Guard Clauses
We cannot make use of the functions as guard clauses in elixir. It means, when
cannot accept functions that returns Boolean values as conditions. Consider the following lines of code…
Here we defined a module Hello
and a function hello
that takes two parameters of name
and age
. So, based on age I am trying IO.puts
accordingly. If you do so you will get an error saying….
This is because when cannot accept functions as guards. We need to convert them to macros
Lets do that…
In the above lines of code, we wrapped all our guards inside a module MyGuards
and make sure the module is top of the module Hello
so, the macros first gets compiled. Now compile and execute you will see the following output..
Starting on Elixir v1.6, you can use defguard/1.
The defguard
is also a macro. You can also create private guards with defguardp
. Hope, you got the point here.
Consider the following example.
NOTE: The defguard
and defguardp
should reside inside the module like other macros. It raises a compile time error, if some thing that don't fit in the guard clause section when
.
Suppose, you want to check the given number is either three
or five
, you can define the guard as following.
Usage
You can also use them inside your code logic as they results boolean
value.
Check the following execution screen shot.
2. Finding the presence of Sub-String
Using =~
operator we can find whether the right sub-string present in left string or not..
3. Finding whether Module is loaded or not
Sometimes, we have to make sure that certain module is loaded before making a call to the function. We are supposed to ensure the module is loaded.
Similarly we are having ensure_compile
to check whether the module is compiled or not…
4. Binary to Capital Atom
Elixir provides a special syntax which is usually used for module names. What is called a module name is an uppercase ASCII letter followed by any number of lowercase or uppercase ASCII letters, numbers, or underscores.
This identifier is equivalent to an atom prefixed by Elixir.
So in the defmodule Blackode
example Blackode
is equivalent to :"Elixir.Blackode"
When we use String.to_atom "Blackode"
it converts it into :Blackode
But actually we need something like “Blackode” to Blackode. To do that we need to use Module.concat
In Command line applications whatever you pass they convert it into binary. So, again you suppose to do some casting operations …
5. Pattern match [ vs ] destructure.
We all know that =
does the pattern match for left and right side. We cannot do [a, b, c] = [1, 2, 3, 4]
this raise a MatchError
We can use destructure/2
to do the job.
If the left side is having more entries than in right side, it assigns the nil
value for remaining entries..
6. Data decoration [ inspect with :label ] option
We can decorate our output with inspect
and label
option. The string of label
is added at the beginning of the data we are inspecting.
If you closely observe this it again returns the inspected data. So, we can use them as intermediate results in |>
pipe operations like following……
You will see the following output
7. Anonymous functions to pipe
We can pass the anonymous functions in two ways. One is directly using &
like following..
This is the most weirdest approach. How ever, we can use the reference of the anonymous function by giving its name.
The above style is much better than previous . You can also use fn
to define anonymous functions.
8. Retrieve Character Integer Codepoints — ?
We can use ?
operator to retrieve character integer codepoints.
The following two tips are mostly useful for beginners…
9. Subtraction over Lists
We can perform the subtraction over lists for removing the elements in list.
We can also perform same operations on char lists too..
If the element to subtract is not present in the list then it simply returns the list.
10. Using Previous results in IEx
When you are working with iex
environment , you can see a number increment every time you evaluate an expression in the shell like iex(2)>
iex(3)>
Those numbers helps us to reuse the result with v/1
function which has been loaded by default..
Last updated