Tuesday, January 31, 2017

Elixir - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. There are a LOT of operators provided by elixir. They are divided in the following categories:
  • Arithmetic operators
  • Comparison operators
  • Boolean operators
  • Misc operators

Arithmetic operators

The following table shows all the arithmetic operators supported by Elixir language. Assume variable A holds 10 and variable B holds 20, then:
Show Examples
Operator Description Example
+ Adds 2 numbers A + B will give 30
- Subtracts second number from first A-B will give -10
* Multiplies two numbers A*B will give 200
/ Divides first number from second. This casts the numbers in floats and gives a float result A/B will give 0.5
div This is a function used to get the quotient on division div(10,20) will give 0
rem This is a function used to get the remainder on division rem(A, B) will give 10

Comparison operators

The comparison operators in elixir are mostly common to those provided in most other languages. The following table sums up comparison operators in Elixir. Assume variable A holds 10 and variable B holds 20, then:
Show Examples
Operator Description Example
== Checks if value on left is equal to value on right(Type casts values if they are not the same type) A == B will give false
!= Checks if value on left is not equal to value on right A != B will give true
=== Checks if type of value on left equals type of value on right, if yes then check the same for value A === B will give false
!== Same as above but checks for inequality instead of equality A !== B will give true
> Checks if the value of left operand is greater than the value of right operand; if yes, then the condition becomes true. A > B will give false
< Checks if the value of left operand is less than the value of right operand; if yes, then the condition becomes true. A < B will give true
>= Checks if the value of left operand is greater than or equal to the value of right operand; if yes, then the condition becomes true. A >= B will give false
<= Checks if the value of left operand is less than or equal to the value of right operand; if yes, then the condition becomes true A <= B will give true

Logical operators

Elixir provides 6 logical operators: and, or, not, &&, || and !. The first three, and or not are strict Boolean operators, meaning that they expect their first argument to be a Boolean. Non Boolean argument will raise an error. While the next three, &&, || and ! are non strict, do not require us to have the first value strictly as a boolean. They work in the same way as their strict counterparts. Assume variable A holds true and variable B holds 20, then:
Show Examples
Operator Description Example
and Checks if both values provided are truthy, if yes then returns the value of second variable. (Logical and) A and B will give 20
or Checks if either value provided is truthy. Returns whichever value is truthy. Else returns false. (Logical or) A or B will give true
not Unary operator which inverts the value of given input. not A will give false
&& Non strict and. Works same as and but doesn't expect first argument to be a boolean. B && A will give 20
|| Non strict or. Works same as or but doesn't expect first argument to be a boolean. B || A will give true
! Non strict not. Works same as not but doesn't expect the argument to be a boolean. !A will give false
NOTE: and, or, && and || are short circuit operators. This means that if first argument of and is false, then it won't check for the second one. And if first argument of or is true, it won't check for the second one. For example,
false and raise("An error")  
#This won't raise an error as raise function wont get executed because of short circuiting nature of and operator.

Bitwise Operators

Bitwise operators work on bits and perform bit by bit operation. Elixir provides bitwise modules as a part of the package Bitwise, so in order to use these, you need to use the bitwise module. To use it, enter the following in your shell:
use Bitwise
Assume A to be 5 and B to be 6 for the following examples:
Show Examples
Operator Description Example
&&& Bitwise and operator copies a bit to result if it exists in both operands A &&& B will give 4
||| Bitwise or operator copies a bit to result if it exists in either operand A ||| B will give 7
>>> Bitwise right shift operator shifts first operand bits to the right by the number specified in second operand A >>> B will give 0
<<< Bitwise left shift operator shifts first operand bits to the left by the number specified in second operand A <<< B will give 320
^^^ Bitwise xor operator copies a bit to result only if it is different on both operands A ^^^ B will give 3
~~~ Unary bitwise not inverts the bits on the given number ~~~A will give -6

Misc operators

Other than the above operators, Elixir also provids a range of other operators that make it quite a powerful language.
Show Examples

Concatenation operator

Elixir provides with a string concatenation operator, '<>'. This is used to concatenate 2 strings. For example,
IO.puts("Hello"<>" "<>"world")
This will print:
Hello World

Match

The match operator, '=' is used to make use of pattern matching feature of the language. We will discuss this operator in depth while discussing about pattern matching in depth.
Please note that = is not only an assignment operator. When we have the left value as a variable and right value as a literal or another variable, value from right is bound to the variable, ie, assignment takes place. But if we have a variable on the right and literal on left, pattern matching happens. The same is the case when both values are literals.

Pin

The pin operator, '^' is a unary operator used by prefixing a variable name. It makes sure that variable when used with the match operator is not assigned a value, but is matched to that value. For example,
a = 12  #assignment
a = 13  #assignment
^a = 13  #Pattern matching

Pipe

The pipe operator, '|>' works like the pipe operator in unix shells. It allows us to pipe the output from one function to the other. For example, if we need to pipe the result of addition in IO.puts, we'll use:
(4+3) |> IO.puts
This will recognize that we have piped the result of addition in IO.puts function. This will print 7 on your console.

String match

The string match operator, '=~', takes a string on the left and either a string or a regular expression on the right. If the string on the right is a substring of left, true is returned. If the regular expression on the right matches the string on the left, true is returned. Otherwise false is returned. For example,
"tutorialspoint" =~ "poi"  #True
"tutorialspoint" =~ ~r/[a-z]*/  #True
"tutorialspoint" =~ ~r/[0-9]*/  #False
Note that regexes start with a '~r' prefix in Elixir.

Code point

It is a unary operator, '?' which returns the UTF-8 codepoint of the character immediately to its right. It can only take one character and accepts Escape Sequences. For example,
?a  #returns 97
?\s  #returns 32

Capture

The capture operator, '&' is used when defining anonymous functions. We'll discuss this in depth in the functions chapter.

Ternary

Elixir doesn't have a ternary operator. We can achieve same functionality using if else:
a = if true, do: "True!", else: "False!"

In

This operator checks if left item exists in the enumerable structure on the right. For example, we can check for an atom in a list, tuple, etc of atoms using this operator:
:yes in [:true, :false, :yes]
The above statement returns true as :yes exists in the list.

No comments:

Post a Comment