Elixir-operators
Elixir-オペレーター
演算子は、特定の数学的または論理的な操作を実行するようコンパイラーに指示する記号です。 elixirが提供する多くの演算子があります。 彼らは次のカテゴリに分かれています-
- 算術演算子
- 比較演算子
- ブール演算子
- その他の演算子
算術演算子
次の表は、Elixir言語でサポートされているすべての算術演算子を示しています。 変数 A が10を保持し、変数 B が20を保持すると仮定します-
リンク:/elixir/elixir_example_arithematic [例を表示]
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 function is used to get the quotient on division. | div(10,20) will give 0 |
rem | This function is used to get the remainder on division. | rem(A, B) will give 10 |
比較演算子
Elixirの比較演算子は、他のほとんどの言語で提供されているものとほとんど共通しています。 次の表は、Elixirの比較演算子をまとめたものです。 変数 A が10を保持し、変数 B が20を保持すると仮定します-
リンク:/elixir/elixir_example_comparision [例を表示]
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 |
論理演算子
Elixirは6つの論理演算子を提供します:and、or、not、&&、||および! 最初の3つの andまたはnot は厳密なブール演算子です。つまり、最初の引数がブールであることを期待しています。 非ブール引数はエラーを発生させます。 次の3つ、* &&、||および!は厳密ではないため、最初の値を厳密にブール値にする必要はありません。 それらは、厳密な対応物と同じように機能します。 変数 *A が真を保持し、変数 B が20を保持すると仮定します-
リンク:/elixir/elixir_example_logical [例を表示]
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 does not expect first argument to be a Boolean. | B && A will give 20 |
Non-strict* or*. Works same as *or *but does not expect first argument to be a Boolean. | B | |
A will give true | ! | Non-strict* not*. Works same as not but does not expect the argument to be a Boolean. |
注- and、_ or 、 && および || _ ||短絡演算子です。 これは、 and の最初の引数がfalseの場合、2番目の引数をさらにチェックしないことを意味します。 or の最初の引数がtrueの場合、2番目の引数はチェックされません。 例えば、
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
ビット演算子
ビットごとの演算子はビットに作用し、ビットごとの操作を実行します。 Elixirはパッケージ Bitwise の一部としてビット単位モジュールを提供します。したがって、これらを使用するには、ビット単位モジュールを_使用_する必要があります。 それを使用するには、シェルで次のコマンドを入力します-
use Bitwise
次の例では、Aを5、Bを6と仮定します-
link:/elixir/elixir_example_bitwise [例を表示]
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 |
その他の演算子
上記の演算子以外に、Elixirは Concatenation演算子、Match演算子、Pin演算子、Pipe演算子、String Match演算子、Code Point演算子、Capture演算子、Ternary Operator などの非常に強力な言語を提供します。
link:/elixir/elixir_example_misc [例を表示]