Hey Guys .. !
Welcome to anther set of Elixir Tips

10 -> Extracting Project Information

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

1
2
iex> hello <> <<0>>
<<104, 101, 108, 108, 111, 0>>

8 -> Initialisation of Multiple with Same value

1
2
3
4
5
6
7
8
iex> x=y=z=5
5
iex> x
5
iex> y
5
iex> z
5

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…

1
2
3
4
5
6
7
8
9
10
11
# Defining struct
defmodule Employee do
   @enforce_keys [:salary]
   defstruct name: nil, salary: nil
end

# Execution

iex> employee = %Employee{name: "blackode"} # Error
iex> employee = %Employee{name: "blackode",salary: 12345}
%Employee{name: "john", salary: 12345}

See this in live action.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Defining the module with one exported function and private one
defmodule Hello do
  def hello name do
   IO.puts name
  end
  defp hellop name do
    IO.puts name
  end
end

# Execution Copy and paste above lines of code in iex>

iex> function_exported? Hello, :hello,1
true
iex> function_exported? Hello, :hellop, 1
false

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.

1
 "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.

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

#output
["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.

1
2
3
4
5
6
7
8
iex> String.jaro_distance "ping","pong"
0.8333333333333334

iex> String.jaro_distance "color","colour"
0.9444444444444445

iex> String.jaro_distance "foo","foo"
1.0

For the sake of FUN , you can find your closeness with your name and your partner or lover in case if you 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.

1
2
3
4
5
6
7
8
iex> string="blackode medium"
"blackode medium"

iex> String.first string
"b"

iex> String.last string
"m"

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 }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#Hello module
defmodule Hello do
@on_load :onload     # this executes after module gets loaded
  def onload do
    IO.puts "#{__MODULE__} is loaded successfully"
  end
end

# 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

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

1
2
3
4
5
6
def print_me(thing)
  when is_integer(thing)
  when is_float(thing)
  when is_nil(thing) do
 "I am a number "
end

See this in action

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 ★

Happy Coding !