elixir KERNEL 4 Flashcards

1
Q

defmodule Sum do

def add(pid, x, y) do

send(pid, x + y)

end

end

spawn(Sum, :add, [self(), 1, 2])

receive do val -> val end

A

=> 3

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

pets = %{

“cat” => %{age: 1},

“dog” => %{age: 3}

}

pets[“cat”][:age] |> update_in(&(&1 + 1))

A

=> %{

“cat” => %{age: 2},

“dog” => %{age: 3}

}

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

c = %{age: 5}

v = %{age: 20}

auto = %{“car” => c, “van” => v}

add1 = &(&1 + 1)

auto[“van”].age |> update_in(add1)

A

%{“car” => %{age:5}, “van” => %{age: 21}}

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

e = %{age: 4}

m = %{age: 7}

dogs = %{ellie: e, max: m}

update_in(dogs.max.age, &(&1 + 1))

A

%{ellie: %{age:4}, max: %{age: 8}}

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

b = %{age: 1}

c = %{age: 2}

toys = %{“bear” => b, “car” => c}

plus1 = &(&1 + 1)

toys |> update_in([“car”, :age], plus1)

A

%{“bear” => %{age: 1}, %{“car” => %{age: 3}}

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

defmodule Add do

def inc(pid, x),

do: send(pid, x + 1)

end

spawn_link(Add, :inc, [self(), 1])

receive do

val -> val

end

A

=> 2

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

b = %{age: 1}

a = %{age: 2}

bots = %{“bob” => b, “amy” => a}

bots[“bob”][:age] |> get_and_update_in(&{&1, &1+1})

A

=> %{“bob” => %{age: 2}, “amy” => %{age: 2}}

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

abs(-1.11)

A

=> 1.11

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

abs(28)

A

=> 28

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

ceil(-1.25)

A

=> -1

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

floor(3.55)

A

=> 3

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

1 in {1, 2, 3}

A

** (Protocol.UndefinedError)

protocol Enumerable not implemented for {1, 2, 3}

*Note: Tuple is a basic type,

but in/2 expects an enumerable on the right side

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

1 in [1, 2] == Enum.member?([1, 2], 1)

A

=> true

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

x = 1

case x do

x when x in [2, 3] -> “boo”

x when x in 0..2 -> “foo”

_x => “whatevs”

end

A

=> “foo”

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

c = %{age: 30}

n = %{age:20}

jobs = %{“cop” => c, “nun” => n}

jobs[“cop”].age |> get_and_update_in(&(&1, &1+1))

A

=> {30, “cop” => %{age: 31}, “nun” => %{age: 20}}

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

is_map_key(%{a: 1}, :a)

A

=> true

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

1 not in [1, 2, 3]

A

=> false

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

defmodule Feeling do

def love, do: “love you!”

end

Feeling.love()

A

=> “love you!”

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

is_map_key(:a, %{a: 1})

A

BadMapError

*Note: is_map_key/2 expects a map as a first argument

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

pets = %{amy: %{age: 7}, mia: %{age: 8}}

pets.amy.age |> get_and_update_in(&(&1, &1 + 1))

A

=> {7, %{amy: %{age: 8}, mia: %{age: 8}}}

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

is_struct(URI.parse(“/”))

A

=> true

eg: URI.parse(“https://hexdocs.pm/elixir/Kernel.html#content”) =>

%URI{
authority: “hexdocs.pm”,
fragment: “content”,
host: “hexdocs.pm”,
path: “/elixir/Kernel.html”,
port: 443,
query: nil,
scheme: “https”,
userinfo: nil
}

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

is_struct(%{})

A

=> false

23
Q

defmodule Lang do

def desc(a, b) do

”#{a} #{b}”

end

end

Lang.desc(“elixir”, “cool”)

A

=> “elixir cool”

24
Q

throw “hello”

A

** (throw) “hello”

25
Q

defmodule Inc do

def add(x, inc_by \1) do

x + inc_by

end

end

Inc.add(1)

A

=> 2

*Note: Inc.add(3, 5) => 8

26
Q

defmodule Title do

def name(name, title \ “”)

def name(name, title) do

title <> name

end

end

Title.name(“Smith”, “Dr.”

A

=> “Dr.Smith”

*Note: if only 1 argument is provided, the first def is called and name and default title are fed into the second def because there is no instruction block defined by the first def.

Therefore, Title.name(“Johnson”) => “Johnson”

27
Q

all = fn :get_and_update, data, next -> Enum.map(data, next) end

get_and_update_in(

[%{x: 1, y: 0}, %{x: 0, y: 0}],

[all, :x],

&{&1, &1 +1}

)

A

=> [

{1, %{x: 2, y: 0}},

{0, %{x: 1, y: 0}}

]

28
Q

try do

throw(:catch_this)

catch

:throw, :catch_this -> :it_was_caught

end

A

=> :it_was_caught

29
Q

defmodule Title do

def name(name, title \ “”)

def name(name, title) do

title <> name

end

end

Title.name(“Smith”)

A

=> “Smith”

30
Q

try do

throw(:tantrum)

catch

:throw, value -> value

after

IO.puts “Are you ok?”

end

A

=> :tantrum

*Note: “Are you ok?” would be printed

before returning the value of throw.

31
Q

defmodule End do

def perform(reason) do

exit reason

catch

:exit, _ -> “Or keep going?”

end

end

End.perform(“Give up”)

A

=> “Or keep going?”

32
Q

try do

throw(:ball)

catch

x -> x

end

A

:ball

33
Q

try do

throw(:tantrum)

catch

_value -> __STACKTRACE__

end

A

Returns a stacktrace

Example from my particular iex session:

[
{:erl_eval, :do_apply, 6, [file: ‘erl_eval.erl’, line: 680]},
{:erl_eval, :try_clauses, 8, [file: ‘erl_eval.erl’, line: 914]},
{:elixir, :recur_eval, 3, [file: ‘src/elixir.erl’, line: 278]},
{:elixir, :eval_forms, 3, [file: ‘src/elixir.erl’, line: 263]},
{IEx.Evaluator, :handle_eval, 5, [file: ‘lib/iex/evaluator.ex’, line: 258]},
{IEx.Evaluator, :do_eval, 3, [file: ‘lib/iex/evaluator.ex’, line: 239]},
{IEx.Evaluator, :eval, 3, [file: ‘lib/iex/evaluator.ex’, line: 217]},
{IEx.Evaluator, :loop, 1, [file: ‘lib/iex/evaluator.ex’, line: 103]}
]

34
Q

destructure([x, y], [1, 2, 3])

{x, y}

A

=> {1, 2}

35
Q

destructure([a, b], [“oy”])

“a is #{a} and b is #{b}”

A

=> “a is oy and b is “

36
Q

score = %{rob: %{win: 2}, ann: %{win: 3}}

get_and_update_in(score, [:rob, :win], &{&1, &1 + 1})

A

{2, %{ann: %{win: 3}, rob %{win: 3}}

37
Q

defstructure([x, y], [1])

{x, y}

A

=> {1, nil}

38
Q

a = “a”

destructure([^a, 1], [:a, 1])

A

** (MatchError) no match of right hand side value: [:a, 1]

39
Q

defmodule Car do

defstruct color: :red

end

struct(Car)

A

=> %Car{color: :red}

40
Q

defmodule Car do

defstruct color: :red

end

struct(Car, color: :blue)

A

=> %Car{color: :blue}

41
Q

defmodule Pet do

defstruct name: “Bob”

end

pet = struct(Pet, name: “Mia”)

struct(pet, age: 1)

A

=> %Pet{name: “Mia”}

42
Q

defmodule Lang do

defstruct name: “Eli” <> “xir”

end

struct(Lang, %{“name” => “ruby”})

A

=> %Lang{name: “Elixir”}

43
Q

defmodule Car do

defstruct make: :BMW

end

a = struct(Car)

b = %{make: :BMW, __struct__: Car}

a == b

A

=> true

44
Q

defmodule Car do

defstruct [:make]

end

struct(Car)

A

=> %Car{make: nil}

45
Q

defmodule Car do

@enforce_keys [:make]

defstruct [:make]

end

struct(Car)

A

=> %Car{make: nil}

*Note:

enforce_keys has no effect here.

Use struct!/2 or % to enforce keys

46
Q

defmodule Car do

@enforce_keys [:make]

defstruct [:make]

end

struct!(Car)

A

** (ArgumentError)

The following keys must also be given when building struct Car:

[:make]

47
Q

defmodule Car do

defstruct [:make]

end

struct!(Car)

A

=> %Car{make: nil}

48
Q

defmodule Car do

defstruct [:make]

end

struct!(Car, color: :red)

A

** (KeyError) key :color not found in: %Car{make: nil}

49
Q

defmodule Child do

defstruct name: “El”

end

a = struct!(%Child{})

struct!(a, age: 1)

A

** (KeyError) key :age not found in: %Child{name: “El”}

50
Q

~U[2020-03-01T20:00:07.001+00:00]

A

~U[2020-03-01 20:00:07.001Z]

51
Q

Define a type, t, for given module so its type can be referenced as User.t

defmodule User do

defstruct name: “Bob”, age: 18

end

A

@type t :: %__MODULE__{

name: String.t(),
age: non_neg_integer

}

52
Q

~U[2020-03-01 20:34:00Z]

A

=> ~U[2020-03-01 20:34:00Z]

53
Q

:a in [:”a”]

A

=> true

54
Q

pid = self()

fn -> send(pid, 1 + 1) end

|> spawn_link()

receive do

val -> val

end

A

=> 2