elixir KERNEL 1 Flashcards
(54 cards)
1
Q
1 === 1.0
A
=> false
*Note: returns true if items have same value and of the same type
2
Q
1 > 2
A
=> false
3
Q
3 >= 2
A
=> true
4
Q
is_atom(:hello)
A
=> true
5
Q
is_boolean(false)
A
=> true
6
Q
1 !== 2
A
=> true
7
Q
1 !== 1.0
A
=> true
8
Q
1 != 2
A
=> true
9
Q
1 != 1.0
A
=> false
10
Q
1 * 2
A
=> 2
11
Q
1 * 2.3
A
=> 2.3
12
Q
[1] ++ ‘a’
A
=> [1, 97]
13
Q
[1] ++ [2]
A
=> [1, 2]
14
Q
1 - 2
A
=> -1
15
Q
-2 + 1 - -1
A
=> 0
16
Q
[1, 2, 3] – [1, 2]
A
=> [3]
17
Q
1 / 2
A
=> 0.5
18
Q
-5 / 1
A
=> -5.0
19
Q
1 < 2
A
=> true
20
Q
1 <= 2
A
=> true
21
Q
“elixir” =~ ~r/l(i)/
A
=> true
22
Q
“elixir” =~ ~r/b/
A
=> false
23
Q
“elixir” =~ “el”
A
=> true
24
Q
“elixir” =~ “er”
A
25
"elixir" =~ ""
=\> true
26
!"hello"
=\> false
27
!nil
=\> true
28
!!nil
=\> false
29
1..4
=\> 1 . . 4
30
is\_binary('hello')
=\> false
31
What error would be returned?
1 / 0
\*\*(ArithmeticError)
bad argument in arithmetic expression: 1 / 0
32
is\_binary(\<\<0, 1, 2\>\>)
=\> true
33
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
34
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.
35
How can you tell Elixir not to import the **if/2** macro?
import Kernel, except: [if: 2]
36
is\_atom(Hello)
=\> true
37
What error would be raised?
"he" ++ "llo"
ArgumentError
\*Note: You can perform 'he' ++ 'llo' on codepoints,
but not on a String
38
[1] ++ [2 | 3]
=\> [1, 2 | 3]
39
[1] ++ 2
=\> [1 | 2]
\*returns improper list
40
[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
41
1 == 1.0
=\> true
42
is\_binary("hello")
=\> true
43
'he' ++ 'llo'
=\> 'hello'
44
is\_boolean(nil)
=\> false
45
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.
46
is\_boolean(1) &&
is\_float(1.2)
=\> false
47
is\_integer(1) &&
is\_atom(:a)
=\> true
48
Map.get(1..4, :first)
=\> 1
49
Map.get(1..4, :last)
=\> 4
50
%Range{
first: \_a,
last: b
} = 1..4
b
=\> 4
51
"bear " \<\> x = "bear John"
x
=\> "John"
52
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.
53
is\_boolean(false) &&
nil &&
is\_atom(:hello)
=\> nil
\*Note: returns first falsey value or the last value
54
is\_number(2.3) &&
is\_list(5) &&
is\_float(1.2)
=\> false
\*Note: returns first falsey value or true