.iex.exs
in your ~
home directory and see the magic. You can also download the file HEREx
sigil calls its respective sigil_x
definitiondefprotocol
and defimpl
are used to define Protocols and Protocol implementations respectively for different types in the following example.iex
and executetrue ? "yes" : "no"
. So, the following is suggested.or
operation. For example:||
is only a shortcut for Kernel.||
. We can use Kernel.||
in the pipeline instead to avoid breaking the pipeline.quote
and Macro.to_string
you can see how our code was grouped into.||
operator always returns the first expression which is true. Elixir doesn’t care about the remaining expressions, and won’t evaluate them after a match has been found.nil
is also false in elixir next :blackode
which evaluates to true and its value is returned immediately with out evaluating the :elixir
and :jose
. Similarly if all the statements evaluates to false
the last expression is returned.&&
returns the second expression if the first expression is true
or else it returns the first expression with out evaluating the second expression. In the above examples the last one is the situation where we encounter to use the &&
operator."5" > 4
unknowingly by an accident and to my surprise it returned with true
.number < atom < reference < fun < port < pid < tuple < map < list < bitstring (binary)
"$34.56"
which is a string and I suppose do arithmetic operations. I usually do something like this before binary pattern matching...
to retrieve the data from the keys as map.key
unlike the usual notation like map["key"]
. That really saves on typing. But, I don’t encourage this because, as programmers we should really care about memory..
form which is not present in the map, it will raise a key error instead of returning the nil
unlike the map["key"]
which returns nil
if key
is not present in a map
.>=1.4.0
has ANSI color printing option to console. You can have great fun with colors. You can also provide background colors.when
cannot accept custom defined functions. Consider the following lines of code…Hello
and a function hello
that takes two parameters of name
and age
. So, based on age I am trying IO.puts
accordingly. If you do so you will get an error saying….macros
Lets do that…MyGuards
and make sure the module is top of the module Hello
so, the macros first gets compiled. Now compile and execute you will see the following output..defguard
is also a macro. You can also create private guards with defguardp
. Hope, you got the point here.
Consider the following example.defguard
and defguardp
should reside inside the module like other macros. It raises a compile time error, if some thing that don't fit in the guard clause section when
.three
or five
, you can define the guard as following.boolean
value.ensure_compile
to check whether the module is compiled or not…Elixir.
So in the defmodule Blackode
example Blackode
is equivalent to :"Elixir.Blackode"
String.to_atom "Blackode"
it converts it into :Blackode
But actually we need something like “Blackode” to Blackode. To do that we need to use Module.concat
=
does the pattern match for left and right side. We cannot do [a, b, c] = [1, 2, 3, 4]
this raise a MatchError
destructure/2
to do the job.nil
value for remaining entries..inspect
and label
option. The string of label
is added at the beginning of the data we are inspecting.|>
pipe operations like following……output
&
like following..fn
to define anonymous functions.Kernel.then/2
function since 1.12.0?
operator to retrieve character integer codepoints.,
mix.exs
.mix
tool.aliases
field.,
at the end when you add this in the middle of list
.aliases()
should return the key-value
list.mix ecto.setup
the three tasks ecto.create
, ecto.migrate
and ecto.seed
will run one after the other.bytecode
in a memory. You access the documentation with the help of Code.get_docs/2
function . This means, the documentation accessed when it is required, but not when it is loaded in the virtual machine like iex
test.ex
with the following code. You can copy and paste it.nil
when you are trying to access the docs of the module you have created so far. This is because, the bytecode
is not available in disk. In simple way beam
file is not present. Lets do that...Ctrl+C
twice so you will come out of the shell and this time you run the command asElixir.Test.beam
. Now the bytecode
for the module Test
is available in memory. Now you can access the documentation as follows...a
is 97
and b
is 98
hence it is listing out them as char_list
. However you can tell the IO.inspect
to list them as list itself with option char_lists: :as_list
.iex
and type h Inspect.Opts
, you will see that Elixir does this kind of thing with other values as well, specifically structs and binaries.filename
line
function
and others…pid
function. This comes with two flavors.pid
, then you can create one and test with it.pid = #PID<0.21.32>
because #
is considered as comment here.#PID<0.21.32>
is treated as comment.String.replace
function will replace the given the pattern with replacing pattern. By default, it replaces all the occurrences of the pattern. Lets check that…String.replace str, "@", "#"
is same as String.replace str, "@", "#", global: true
global: false
. So, it replaces only the first occurrence of @
. Lets check that…@
is replaced with #
.:erlang.memory
:erlang.memory :atom
to get the memory usage of atoms.head
and tail
like [head | tail]
. We can use the same principle for picking out the elements in the list like the following way…[maps]
where you have to extract first_name
of the each map, generally we go for enum
. We can also achieve this by using the get_in
and Access.all()
get_in
along with Access.all()
, as the first value in the list of keys like above, the users datatype should be list. If you pass the map, it returns the error.nil
for key which is not in the map
.Access.all()
and inside the list of keys [:books, Access.all(), :name]
, the value of the key :books
should return the list, other wise it raises an error.for x <- [1, 2, 3], do: x + 1
. But we can also add the comprehension along with filter.