Part- 6
1 Extracting Project Information
2 Inner Binary Representation of String
This is a common trick in elixir
. You have to concatenate the null byte <<0>>
to a string that you want to see its inner binary representation like in the following way…
3 Initialisation of Multiple with Same value
See this in action here...
4 Not Null implementation in Structs
This is much like adding a not null constraint to the structs. When you try to define the struct with the absence of that key in the struct, it should raise an exception. Lets do that…
You have to use @enforce_keys [<keys>]
while defining the struct…
Warning Keep in mind @enforce_keys
is a simple compile-time guarantee to aid developers when building structs. It is not enforced on updates and it does not provide any sort of value-validation. The above warning is from the ORIGINAL DOCUMENTATION
5 Check Whether Function is Exported or not
Elixir provides function_exported?/3
to achieve this…
6 Splitting the string with Pattern
We all know how to split a string with String.split/2 function
. But you can also pass a pattern to match that over and over and splitting the string whenever it matches the pattern.
If you observe the above string, it comprises of two blank spaces , one exclamation mark !
, two minus — symbols -
and a asterisk *
symbol. Now we are going to split that string with all of those.
The pattern is generated at run time. You can still validate with :binary.compiled
7 Checking the closeness of strings
You can find the distance between the two strings using String.jaro_distance/2
. This gives a float value in the range 0..1
Taking the 0
for no close and 1
is for exact closeness.
For the FUN, you can find your closeness with your name and your partner or lover in case if aren’t married. Hey… ! I am just kidding…. It is just an algorithm which is predefined where our love is undefined. Cheers …….. :)
8 last and first for Strings
We know that first
and last
for lists
gets you the element first and last respectively in the given list. Similarly, the strings give you the first and last graphemes
in the given string.
9 Executing code Immediately after loading a Module
Elixir provides @on_load
which accepts atom
as function name in the same module or a tuple
with function_name and its arity like {function_name, 0}
.
10 Chain of [ or ] ’ s in guards
This is about multiple guards in the same clause and writing or
conditions with out using or
We all know that or
is used as a conjunction for two conditions resulting true if either one of them is true. Many of us writing the or conditions in the guard as following way…
You can also do this in bit more clear format as the following way…
See also Elixir Style Guide
Thanks for Reading.
If you feel they are useful...
Last updated