Elixir: why two boolean operators for one operation

Elixir apparently has 2 Boolean operators for each operation:

|| , or
&& , and

etc .. The only difference is that for or, andetc The first argument itself should be logical. What is the meaning of this second set of operators, when || , &&, etc., It seems able to handle everything?

+4
source share
4 answers

According to Elixir's tutorials on Basic Operators :

orand andare short circuit operators. They only do the right thing if the left side is not enough to determine the result.

, ?; , .

, and, or not, . - , &&, || !.

My Take

  • , .
  • Inline , . iex (5) ArgumentError. ​​ , -.

&& and

iex(1)> nil && 13
nil

iex(2)> true && 17
17

iex(3)> true and true
true

iex(4)> false and true
false

iex(5)> 73 and false
** (ArgumentError) argument error: 73
+5

, . :

:

  • : ,
  • , "" ""

.

(, ) , :

.

'', '', "xor" "" . , , C lisp , , (-). . "" "". "" "" " ", " 'OrElse. (bool, bool) orelse (bool, bool) , , . , , , - . .

, " " .

. , . . IMAO /, . , , , . , , , .

, .

+5

, , - ( ) , - , nil false. , (true false) , .

, or, , true false

||, & &,! . , nil false, ().

x || y # gives x if x is truthy else y
x && y # gives y if x is truthy else x
!x # false if x is truthy , otherwise true

iex(5)> not 3     
** (ArgumentError) argument error
    :erlang.not(3)

iex(6)> ! 3
false
+4

, && || Elixir

  • .
  • 1.

, , Elixir 0 true

iex(1)> (0 && true) and true
true

:

iex(2)> (true && 0) and true   
** (ArgumentError) argument error: 0

Elixir 0 and true: Elixir true .

, 0 true && 0 ( !)

0

Source: https://habr.com/ru/post/1622863/


All Articles