10 Killer Elixir Tips #6

The sixth part of another 10 elixir pro tips.


Hey Guys .. !
Welcome to anther set of 10 Killer Elixir Tips..
Read if you missed Killer Elixir Tips#1 #2 #3 #4 #5

Elixir version 1.5.1 & Erlang otp version 20

10 -> Extracting Project Information

Mix.Project.config[:version] # returns the version
Mix.Project.config[:app] # returns app name

You have to be inside the mix project when you are trying.

See this in action…

9 -> 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…

iex> “hello” <> <<0>>
<<104, 101, 108, 108, 111, 0>>

8 -> Initialisation of Multiple with Same value

iex> x=y=z=5
iex> x
iex> y
iex> z

7 -> 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…

# Defining struct
defmodule Employee do
@enforce_keys [:salary]
defstruct name: nil, salary: nil
# Execution 
iex> employee = %Employee{name: "blackode"} # Error 
iex> employee = %Employee{name: "blackode",salary: 12345}
%Employee{name: "john", salary: 12345}

See this in action…

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

6 -> Check Whether Function is Exported or not

Elixir provides function_exported?/3 to achieve this…

# Defining the module with one exported function and private one
defmodule Hello do
def hello name do
IO.puts name
defp hellop name do
IO.puts name
# Execution Copy and paste above lines of code in iex> 
iex> function_exported? Hello, :hello,1
iex> function_exported? Hello, :hellop, 1

See this in action…

5 -> 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.

 "Hello Blackode! Medium-is-5*"

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.

string = "Hello Blackode! Medium-is-5*"
String.split string,[" ", "!", "-","*"]
["Hello", "Blackode", "", "Medium", "is", "5", ""]

The pattern is generated at run time. You can still validate with :binary.compiled

4 -> 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.

iex> String.jaro_distance "ping","pong"
iex> String.jaro_distance "color","colour"
iex> String.jaro_distance "foo","foo"

For the sake of 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 …….. :)

3 -> 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.

iex> string="blackode medium"
"blackode medium"
iex> String.first string
iex> String.last string

See this in action…

2 -> 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}

#Hello module 
defmodule Hello do
@on_load :onload # this executes after module gets loaded
def onload do
IO.puts "#{__MODULE__} is loaded successfully"
# Execution .... Just copy and paste the code in the iex terminal
# You will see the output something like this ....
Elixir.Hello is loaded successfully  
{:module, Hello,
<<70, 79, 82, 49, 0, 0, 4, 72, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 130,
0, 0, 0, 12, 12, 69, 108, 105, 120, 105, 114, 46, 72, 101, 108, 108, 111, 8,
95, 95, 105, 110, 102, 111, 95, 95, 9, ...>>, {:onload, 0}}

You can see this in live here…

1-> 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…

def print_me(thing) when is_integer(thing) or is_float(thing) or is_nil(thing), do: "I am a number"

You can also do this in bit more clear format as the following way…

def print_me(thing)
when is_integer(thing)
when is_float(thing)
when is_nil(thing) do
"I am a number "

See this in action…

Google Image search

Sharing is Caring !!

Happy Coding !!

Thanks Giving
Hey guys thanks for the support you have shown in reading and clapping the previous parts of this category.

to be continued … in 10 Killer Elixir Tips Part 7 ( Releasing Soon…)

Thanks 4 UR Tm.. Read all parts of Killer Elixir Tips and download README.MD file at GitHub .
 Click on link All Killer Elixir Parts. Contribute with a ★

Show your support

Clapping shows how much you appreciated Blackode’s story.