Part- 1
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 list. This is just bad but good for discoveries.
2. i( term) Elixir Term Type and Meta Data
Prints information about the data type of any given term. Try that in iex
and see the magic.
3. iex Custom Configuration - iex Decoration
Copy the content into a file and save the file as .iex.exs
in your ~
home directory and see the magic. You can also download the file HERE
4. Creating Custom Sigils and Documenting
Each x
sigil call respective sigil_x
definition
Defining Custom Sigils
usage
Load the module into iex
5. Custom Error Definitions
Define Custom Error
Usage
6. Get a Value from Nested Maps Easily
The get_in
function can be used to retrieve a nested value in nested maps using a list of keys.
Read docs: Kernel.get_in/2
7. With Statement Benefits
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.
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 respectively for different types in the following example.
Usage
Load the code into iex
and execute
9. Ternary Operator
There is no ternary operator like true ? "yes" : "no"
. So, the following is suggested.
10. Advantage of Kernel.||
When using pipelines, sometimes we break the pipeline for or
operation. For example:
Indeed, ||
is only a shortcut for Kernel.||
. We can use Kernel.||
in the pipeline instead to avoid breaking the pipeline.
The code above will be:
This above tip is from qhwa
Last updated