elixir-tips
Search…
Part- 6
1
Elixir version 1.5.1 & Erlang otp version 20
Copied!

1 Extracting Project Information

1
Mix.Project.config[:version] # returns the version
2
Mix.Project.config[:app] # returns app name
Copied!
You have to be inside the mix project when you are trying. See this in action…

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…
1
iex> “hello” <> <<0>>
2
<<104, 101, 108, 108, 111, 0>>
Copied!

3 Initialisation of Multiple with Same value

1
iex> x = y = z = 5
2
5
3
iex> x
4
5
5
iex> y
6
5
7
iex> z
8
5
Copied!
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…
1
# Defining struct
2
defmodule Employee do
3
@enforce_keys [:salary]
4
defstruct name: nil, salary: nil
5
end
6
# Execution
7
iex> employee = %Employee{name: "blackode"} # Error
8
iex> employee = %Employee{name: "blackode",salary: 12345}
9
%Employee{name: "john", salary: 12345}
Copied!
See this in action...
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…
1
# Defining the module with one exported function and private one
2
defmodule Hello do
3
def hello name do
4
IO.puts name
5
end
6
defp hellop name do
7
IO.puts name
8
end
9
end
10
# Execution Copy and paste above lines of code in iex>
11
iex> function_exported? Hello, :hello,1
12
true
13
iex> function_exported? Hello, :hellop, 1
14
false
Copied!
See this in action...

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.
1
"Hello Blackode! Medium-is-5*"
Copied!
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.
1
string = "Hello Blackode! Medium-is-5*"
2
String.split string, [" ", "!", "-", "*"]
3
#output
4
["Hello", "Blackode", "", "Medium", "is", "5", ""]
Copied!
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.
1
iex> String.jaro_distance "ping", "pong"
2
0.8333333333333334
3
iex> String.jaro_distance "color", "colour"
4
0.9444444444444445
5
iex> String.jaro_distance "foo", "foo"
6
1.0
Copied!
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.
1
iex> string = "blackode medium"
2
"blackode medium"
3
iex> String.first string
4
"b"
5
iex> String.last string
6
"m"
Copied!
See this in action…

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}.
1
#Hello module
2
defmodule Hello do
3
@on_load :onload # this executes after module gets loaded
4
def onload do
5
IO.puts "#{__MODULE__} is loaded successfully"
6
end
7
end
8
# Execution .... Just copy and paste the code in the iex terminal
9
# You will see the output something like this ....
10
Elixir.Hello is loaded successfully
11
{:module, Hello,
12
<<70, 79, 82, 49, 0, 0, 4, 72, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 130,
13
0, 0, 0, 12, 12, 69, 108, 105, 120, 105, 114, 46, 72, 101, 108, 108, 111, 8,
14
95, 95, 105, 110, 102, 111, 95, 95, 9, ...>>, {:onload, 0}}
Copied!
You can see this in live here…

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…
1
def print_me(thing) when is_integer(thing) or is_float(thing) or is_nil(thing), do: "I am a number"
Copied!
You can also do this in bit more clear format as the following way…
1
def print_me(thing)
2
when is_integer(thing)
3
when is_float(thing)
4
when is_nil(thing) do
5
"I am a number "
6
end
Copied!
See this in action…
See also Elixir Style Guide
Thanks for Reading.
If you feel they are useful...
Prev Next
Last modified 1yr ago