links: Elixir MOC


Elixir represents true and false values with the boolean type. There are only two values true and false. These values can be combined with boolean operators and/2, or/2, not/1

true_variable = true and true 
false_variable = true and false 
 
true_variable = false or true 
false_variable = false or false 
 
true_variable = not false 
false_variable = not true

The operators and/2, or/2, not/1 are strictly boolean which means their first argument must be a boolean. There are also equivalent boolean operators that work with any type of arguments - &&/2, ||/2 and !/1

Boolean operators use short-circuit evaluation, which means that the expression on the right-hand side of the operator is only evaluated if needed

Each of the operators have a different precedence, where not/1 is evaluated first before and/2 and or/2. Brackets can be used to evaluate one part of the expression before the others:

not true and false # => false 
not (true and false) # => true

when writing a function that returns boolean value, it is idiomatic to end the function name with ?. The same convention can be used for the variables that store boolean values

def either_true?(a?, b?) do
  a? or b?
end

tags: boolean elixir

sources: