10 Killer Elixir Tips #1

Happy Coding !

Photo by Michael Kahl on Unsplash

If you are bad at reading watch video

YouTube Video on this Article.

1. Multiple [ OR ]

This is just the other way of writing Multiple OR conditions. This is not the recommended approach because in regular approach when the condition evaluates to true , it stops executing the remaining conditions which saves time of evaluation unlike this approach which evaluates all conditions first in a list. This is just good for discoveries.

2. i( term)

It prints the information about a data type of given term. Try that in iex and see the magic.

iex> i(1..5)

3. iex Custom Configuration

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

iex configuration file
iex custom configuration

4. Custom Sigils

Each x sigil calls its respective sigil_x definition

Defining your own sigils

5. Custom Error Definitions

Defining Custom Error

Usage

$ 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

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.

iex> with 1 <- 1+0,
2 <- 1+1,
do: IO.puts "all matched"
"all matched"
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

Defining protocols

9. Ternary Operator

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

"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 ★

Like what you read? Give Blackode a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.