elixir KERNEL 1 Flashcards
1 === 1.0
=> false
*Note: returns true if items have same value and of the same type
1 > 2
=> false
3 >= 2
=> true
is_atom(:hello)
=> true
is_boolean(false)
=> true
1 !== 2
=> true
1 !== 1.0
=> true
1 != 2
=> true
1 != 1.0
=> false
1 * 2
=> 2
1 * 2.3
=> 2.3
[1] ++ ‘a’
=> [1, 97]
[1] ++ [2]
=> [1, 2]
1 - 2
=> -1
-2 + 1 - -1
=> 0
[1, 2, 3] – [1, 2]
=> [3]
1 / 2
=> 0.5
-5 / 1
=> -5.0
1 < 2
=> true
1 <= 2
=> true
“elixir” =~ ~r/l(i)/
=> true
“elixir” =~ ~r/b/
=> false
“elixir” =~ “el”
=> true
“elixir” =~ “er”
“elixir” =~ “”
=> true
!”hello”
=> false
!nil
=> true
!!nil
=> false
1..4
=> 1 . . 4
is_binary(‘hello’)
=> false
What error would be returned?
1 / 0
**(ArithmeticError)
bad argument in arithmetic expression: 1 / 0
is_binary(<<0, 1, 2>>)
=> true
What type of error would be raised?
x <> “ John” = “bear John”
ArgumentError
**(ArgumentError) the left argument of <> operator inside a match should always be a literal binary as its size can’t be verified.
got: x
Some functions are marked as
inlined by the compiler.
What does this mean?
Most of the inlined functions can be seen when capturing the function:
iex> &Kernel.is_atom/1
&:erlang.is_atom/1
BIFs (built-in internal functions) in Erlang-land. Some of them are allowed in guards and others are used for compiler optimizations.
How can you tell Elixir not to import the if/2 macro?
import Kernel, except: [if: 2]
is_atom(Hello)
=> true
What error would be raised?
“he” ++ “llo”
ArgumentError
*Note: You can perform ‘he’ ++ ‘llo’ on codepoints,
but not on a String
[1] ++ [2 | 3]
=> [1, 2 | 3]
[1] ++ 2
=> [1 | 2]
*returns improper list
[1, 2, 3, 2, 1] – [1, 2, 2]
=> [3, 1]
The complexity of a–b is proportional to length(a) * length(b), meaning that it will be very slow if both a and b are long lists. In such cases, consider using MapSet.difference/2
1 == 1.0
=> true
is_binary(“hello”)
=> true
‘he’ ++ ‘llo’
=> ‘hello’
is_boolean(nil)
=> false
defmodule Person do
@name “Keith”
def fun, do: @name
@name “James”
def clever, do: @name
end
[Person.fun, Person.clever]
=> [“Keith”, “James”]
It is important to note that reading an attribute takes a snapshot of its current value.
In other words, the value is read at compilation time and not at runtime.
Try to avoid rebinding module attributes.
is_boolean(1) &&
is_float(1.2)
=> false
is_integer(1) &&
is_atom(:a)
=> true
Map.get(1..4, :first)
=> 1
Map.get(1..4, :last)
=> 4
%Range{
first: _a,
last: b
} = 1..4
b
=> 4
“bear “ <> x = “bear John”
x
=> “John”
Which operation is preferable?
def inc([], acc), do: acc
def inc([h | t], acc), do: inc(t, acc ++ [h+1])
vs
def inc([], acc), do: Enum.reverse(acc)
def inc([h | t], acc), do: inc(t, [h + 1 | acc])
The complexity of a ++ b is proportional to length(a), so avoid repeatedly appending to lists of arbitrary length. Instead, consider prepending via [head | tail] and then reversing.
is_boolean(false) &&
nil &&
is_atom(:hello)
=> nil
*Note: returns first falsey value or the last value
is_number(2.3) &&
is_list(5) &&
is_float(1.2)
=> false
*Note: returns first falsey value or true