xsigil calls its respective
defimplare used to define Protocols and Protocol implementations respectively for different types in the following example.
true ? "yes" : "no". So, the following is suggested.
oroperation. For example:
||is only a shortcut for
Kernel.||. We can use
Kernel.||in the pipeline instead to avoid breaking the pipeline.
Macro.to_stringyou 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.
nilis also false in elixir next
:blackodewhich evaluates to true and its value is returned immediately with out evaluating the
:jose. Similarly if all the statements evaluates to
falsethe last expression is returned.
&&returns the second expression if the first expression is
trueor 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
"5" > 4unknowingly by an accident and to my surprise it returned with
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.keyunlike 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
keyis not present in a
>=1.4.0has ANSI color printing option to console. You can have great fun with colors. You can also provide background colors.
whencannot accept custom defined functions. Consider the following lines of code…
Helloand a function
hellothat takes two parameters of
age. So, based on age I am trying
IO.putsaccordingly. If you do so you will get an error saying….
macrosLets do that…
MyGuardsand make sure the module is top of the module
Helloso, the macros first gets compiled. Now compile and execute you will see the following output..
defguardis also a macro. You can also create private guards with
defguardp. Hope, you got the point here. Consider the following example.
defguardpshould 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
five, you can define the guard as following.
ensure_compileto check whether the module is compiled or not…
Elixir.So in the
Blackodeis equivalent to
String.to_atom "Blackode"it converts it into
:BlackodeBut actually we need something like “Blackode” to Blackode. To do that we need to use
=does the pattern match for left and right side. We cannot do
[a, b, c] = [1, 2, 3, 4]this raise a
destructure/2to do the job.
nilvalue for remaining entries..
labeloption. The string of
labelis added at the beginning of the data we are inspecting.
|>pipe operations like following……
fnto define anonymous functions.
?operator to retrieve character integer codepoints.
,at the end when you add this in the middle of
aliases()should return the
mix ecto.setupthe three tasks
ecto.seedwill run one after the other.
bytecodein a memory. You access the documentation with the help of
Code.get_docs/2function . This means, the documentation accessed when it is required, but not when it is loaded in the virtual machine like
test.exwith the following code. You can copy and paste it.
nilwhen you are trying to access the docs of the module you have created so far. This is because, the
bytecodeis not available in disk. In simple way
beamfile is not present. Lets do that...
Ctrl+Ctwice so you will come out of the shell and this time you run the command as
Elixir.Test.beam. Now the
bytecodefor the module
Testis available in memory. Now you can access the documentation as follows...
98hence it is listing out them as
char_list. However you can tell the
IO.inspectto list them as list itself with option
h Inspect.Opts, you will see that Elixir does this kind of thing with other values as well, specifically structs and binaries.
pidfunction. 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.replacefunction 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 :atomto get the memory usage of atoms.
[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_nameof the each map, generally we go for
enum. We can also achieve this by using the
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.
nilfor key which is not in the
Access.all()and inside the list of keys
[:books, Access.all(), :name], the value of the key
:booksshould 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.
x <- b_stringis just changed something like
<< x <- b_string >>to make the sense.
IO.stream(:stdio, :line)will the read a line input from the keyboard.
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…
@enforce_keys [<keys>]while defining the struct…