If you are bad at reading watch video

YouTube Video on this Article.

1. Multiple [ OR ]

1
2
3
4
5
6
7
# Regular Approach
find = fn(x) when x>10 or x<5 or x==7 -> x end

# Our Hack
hell = fn(x) when true in [x>10,x<5,x==7] -> x en

2. i( term)

Prints information about the data type of any given term. Try that in iexand see the magic.

1
iex> i(1..5)

3. iex Custom Configuration

Save the following file as .iex.exs in your ~ home directory and see the magic.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#.iex.exs

IO.puts IO.ANSI.red_background() <> IO.ANSI.white() <> " ❄❄❄ Good Luck with Elixir ❄❄❄ " <> IO.ANSI.reset
Application.put_env(:elixir, :ansi_enabled, true)
IEx.configure(
 colors: [
   eval_result: [:green, :bright] ,
   eval_error: [[:red,:bright,"Bug Bug ..!!"]],
   eval_info: [:yellow, :bright ],
 ],
 default_prompt: [
   "\e[G",    # ANSI CHA, move cursor to column 1
    :white,
    "I",
    :red,
    "❤" ,       # plain string
    :green,
    "%prefix",:white,"|",
     :blue,
     "%counter",
     :white,
     "|",
    :red,
    "▶" ,         # plain string
    :white,
    "▶▶"  ,       # plain string
      # ❤ ❤-»" ,  # plain string
    :reset
  ] |> IO.ANSI.format |> IO.chardata_to_string

)

iex configuration file

img

iex custom configuration screen-shot

4. Custom Sigils

Each x sigil call respective sigil_x definition

Defining your own sigils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
defmodule MySigils do
  @doc """
  returns the downcasing string if option l is given
  then returns the list of downcase letters
  """
  def sigil_l(string,[]), do: String.Casing.downcase(string)
  def sigil_l(string,[?l]), do: String.Casing.downcase(string) |> String.graphemes

  @doc """
  the upcasing string if option l is given then returns the list of downcase letters
  """
  def sigil_u(string,[]), do: String.Casing.upcase(string)
  def sigil_u(string,[?l]), do: String.Casing.upcase(string) |> String.graphemes
end

usage

Load the module into iex

1
2
3
4
5
6
7
8
9
iex> import MySigils
iex> ~l/HELLO/
"hello"
iex> ~l/HELLO/l
["h", "e", "l", "l", "o"]
iex> ~u/hello/
"HELLO"
iex> ~u/hello/l
["H", "E", "L", "L", "O"]

5. Custom Error Definitions

1
2
3
defmodule BugError do
   defexception message: "BUG BUG .." # message is the default
end

Defining Custom Error

Usage

1
2
3
4
5
$ iex bug_error.ex
iex> raise BugError
** (BugError) BUG BUG ..
iex> raise BugError, message: "I am Bug.."
** (BugError) I am Bug..

6. Get a Value from Nested Maps

1
2
3
4
5
6
7
8
9
10
#Get a value from nested maps

The get_in function can be used to retrieve a nested value in nested maps using a list of keys.

nested_map = %{ name: %{ first_name: "blackode"} }     #Example of Nested Map
first_name = get_in(nested_map, [:name, :first_name])  # Retrieving the Key

# Returns nil for missing value
nil = get_in(nested, [:name, :last_name])              # returns nil when key is not present
Read docs: Kernel.get_in/2

Getting a Value from Nested Maps

7. with statement

The special form with is used to chain a sequence of matches in order and finally return the result of do: if all the clauses match. However, if one of the clauses does not match, its result of the miss matched expression is immediately returned.

1
2
3
4
iex> with 1 <- 1+0,
          2 <- 1+1,
          do: IO.puts "all matched"
"all matched"
1
2
3
4
5
iex> with 1 <- 1+0,
          2 <- 3+1,
          do: IO.puts "all matched"
4
## since  2 <- 3+1 is not matched so the result of 3+1 is returned.

8. Writing Protocols

Define a Protocol

A Protocol is a way to dispatch to a particular implementation of a function based on the type of the parameter. The macros defprotocol and defimpl are used to define Protocols and Protocol implementations for different types in the following example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
defprotocol Triple do    
  def triple(input)  
end  

defimpl Triple, for: Integer do    
  def triple(int) do     
    int * 3   
  end  
end   

defimpl Triple, for: List do
  def triple(list) do
    list ++ list ++ list   
  end  
end

Usage

Load the code into iex and execute

1
2
3
4
iex> Triple.triple(3)
9
Triple.triple([1, 2])
[1, 2, 1, 2,1,2]

9. Ternary Operator

There is no ternary operator like true ? "yes" : "no" . So, the following is suggested.

1
"no" = if 1 == 0, do: "yes", else: "no"

10.????

Purposely I did not write the 10 tip. If you have one Share in the comment section below..

to be continued … in 10 Killer Elixir Tips #2

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 ★