elixir KERNEL 1 Flashcards

1
Q

1 === 1.0

A

=> false

*Note: returns true if items have same value and of the same type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

1 > 2

A

=> false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

3 >= 2

A

=> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

is_atom(:hello)

A

=> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

is_boolean(false)

A

=> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

1 !== 2

A

=> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

1 !== 1.0

A

=> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

1 != 2

A

=> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

1 != 1.0

A

=> false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

1 * 2

A

=> 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

1 * 2.3

A

=> 2.3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

[1] ++ ‘a’

A

=> [1, 97]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

[1] ++ [2]

A

=> [1, 2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

1 - 2

A

=> -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

-2 + 1 - -1

A

=> 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

[1, 2, 3] – [1, 2]

A

=> [3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

1 / 2

A

=> 0.5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

-5 / 1

A

=> -5.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

1 < 2

A

=> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

1 <= 2

A

=> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

“elixir” =~ ~r/l(i)/

A

=> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

“elixir” =~ ~r/b/

A

=> false

23
Q

“elixir” =~ “el”

A

=> true

24
Q

“elixir” =~ “er”

A
25
Q

“elixir” =~ “”

A

=> true

26
Q

!”hello”

A

=> false

27
Q

!nil

A

=> true

28
Q

!!nil

A

=> false

29
Q

1..4

A

=> 1 . . 4

30
Q

is_binary(‘hello’)

A

=> false

31
Q

What error would be returned?

1 / 0

A

**(ArithmeticError)

bad argument in arithmetic expression: 1 / 0

32
Q

is_binary(<<0, 1, 2>>)

A

=> true

33
Q

What type of error would be raised?

x <> “ John” = “bear John”

A

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
Q

Some functions are marked as

inlined by the compiler.

What does this mean?

A

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
Q

How can you tell Elixir not to import the if/2 macro?

A

import Kernel, except: [if: 2]

36
Q

is_atom(Hello)

A

=> true

37
Q

What error would be raised?

“he” ++ “llo”

A

ArgumentError

*Note: You can perform ‘he’ ++ ‘llo’ on codepoints,

but not on a String

38
Q

[1] ++ [2 | 3]

A

=> [1, 2 | 3]

39
Q

[1] ++ 2

A

=> [1 | 2]

*returns improper list

40
Q

[1, 2, 3, 2, 1] – [1, 2, 2]

A

=> [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
Q

1 == 1.0

A

=> true

42
Q

is_binary(“hello”)

A

=> true

43
Q

‘he’ ++ ‘llo’

A

=> ‘hello’

44
Q

is_boolean(nil)

A

=> false

45
Q

defmodule Person do

@name “Keith”

def fun, do: @name

@name “James”

def clever, do: @name

end

[Person.fun, Person.clever]

A

=> [“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
Q

is_boolean(1) &&

is_float(1.2)

A

=> false

47
Q

is_integer(1) &&

is_atom(:a)

A

=> true

48
Q

Map.get(1..4, :first)

A

=> 1

49
Q

Map.get(1..4, :last)

A

=> 4

50
Q

%Range{

first: _a,
last: b

} = 1..4

b

A

=> 4

51
Q

“bear “ <> x = “bear John”

x

A

=> “John”

52
Q

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])

A

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
Q

is_boolean(false) &&

nil &&

is_atom(:hello)

A

=> nil

*Note: returns first falsey value or the last value

54
Q

is_number(2.3) &&

is_list(5) &&

is_float(1.2)

A

=> false

*Note: returns first falsey value or true